2018-10-23T13:34:00.000Z

How to use Ingress in Kubernetes

Kubernetes is a awesome platform for easily deploying and managing services and configuring with configs and secrets. However you will quickly hit that you will need to route traffic to your services and it is not so dynamically, manually updating DNS with IP's from different cloud loadbalancers.

Kubernetes has the answer and it is called Ingress! It is a different kind of resource compared to the other kinds. Ingress don't do anything by default, but it has one thing clear, the interface and how you define routes. You then have to choose a Ingress Controller which job is to read these Ingress resources and configure the traffic accordingly.

Ingress Controller

These ingress controllers will provide different kinds of features, the most noticable is the "nginx ingress controller", which is also maintained by the Kubernetes core team. It takes the best of the stable nginx configuration that has been battled tested and can handle any kind of workload, and makes it dynamically based on changes to the Kubernetes config. This is something complex and only something bigger companies had the time to configure before!

NGiNX Ingress Controller

The nginx ingress controller is super easy to install, they have a basic deployment you have to apply and the based on your setup you will apply some ekstra config.

They support ingress for Docker for Mac, minikube, AWS, GKE, Azure and Bare-metal, which is basicly everything.

https://kubernetes.github.io/ingress-nginx/deploy/

Go ahead and apply the config you need!

Remember to apply the mandatory first and then the custom afterwards!

See that it worked

You should see a native load balancer been deployed if you ran it in Google Cloud or AWS, or if you did Docker for Mac, simply have port 80 and 443 redirect to your local docker installation.

You can see it by running:

$ kubectl get svc -o wide --all-namespaces

Your first Ingress resource

We first need a deployment, if you don't already have one, you can deploy a nginx static webpage quickly by doing:

kubectl run --image nginx --port 80 mywebsite

and it has to have a Kubernetes Service in front of it, that you can quickly make it:

kubectl expose deploy mywebsite

It will take the exposed port from the deployment and use that for the service. That will only expose the deployment internally and give it a Cluster-IP.

Now you can write your Ingress and it will look like this, save it in a file called ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /custompath
        backend:
          serviceName: nginx
          servicePort: 80
$ kubectl apply -f ingress.yaml

Congrats, now you should be able to visit the LoadBalancer from above and go to /custompath!

This will direct resources to nginx if the url is /custompath and rewrite/redirect any requests that has /custompath as the start like /custompath/my-route and forward it.

Only respond to a specific hostname

The above ingress resource will respond to any domain, you can limit it to be more specific if you add host, next to the http yaml dictionary.

  ...
  - host: foo.bar.com
    http:
      paths:
  ...
Tags:#kubernetes
Previous:Spoof you mac address and get wifi
Next:How to use Prometheus with Kubernetes on Docker for Mac