I’m doing the Linux Foundation Kubernetes Fundamentals course at the moment, and I was very disappointed in the chapter on Ingress Controllers. To be honest it feels like an after thought — there is no lab, and the provided examples don’t work if you re-type them into Kubernetes (you can’t cut and paste of course, just to add to the fun).
I found this super annoying, so I thought I’d write up my own notes on how to get nginx working as an Ingress Controller on Kubernetes.
First off, the nginx project has excellent installation resources online at github. The only wart with their instructions is that they changed the labels used on the pods for the ingress controller, which means the validation steps in the document don’t work until that is fixed. That is reported in a github issue and there was a proposed fix that didn’t have an associated issue that pre-dates the creation of the issue.
The basic process, assuming a baremetal Kubernetes install, is this:
$ NGINX_GITHUB="https://raw.githubusercontent.com/kubernetes/ingress-nginx" $ kubectl apply -f $NGINX_GITHUB/master/deploy/mandatory.yaml $ kubectl apply -f $NGINX_GITHUB/master/deploy/provider/baremetal/service-nodeport.yaml
Wait for the pods to fetch their images, and then check if the pods are healthy:
$ kubectl get pods -n ingress-nginx NAME READY STATUS RESTARTS AGE default-http-backend-6586bc58b6-tn7l5 1/1 Running 0 20h nginx-ingress-controller-79b7c66ff-m8nxc 1/1 Running 0 20h
That bit is mostly explained by the Linux Foundation course. Well, he links to the github page at least and then you just read the docs. The bit that isn’t well explained is how to setup ingress for a pod. This is partially because kubectl doesn’t have a command line to do this yet — you have to POST an API request to get it done instead.
First, let’s create a target deployment and service:
$ kubectl run ghost --image=ghost deployment.apps/ghost created $ kubectl expose deployments ghost --port=2368 service/ghost exposed
The YAML to create an ingress for this new “ghost” service looks like this:
$ cat sample_ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ghost spec: rules: - host: ghost.10.244.2.13.nip.io http: paths: - path: / backend: serviceName: ghost servicePort: 2368
Where 10.244.2.13 is the IP that my CNI assigned to the nginx ingress controller. You can look that up with a describe of the nginx ingress controller pod:
$ kubectl describe pod nginx-ingress-controller-79b7c66ff-m8nxc -n ingress-nginx | grep IP IP: 10.244.2.13
Now we can create the ingress entry for this ghost deployment:
$ kubectl apply -f sample_ingress.yaml ingress.extensions/ghost created
This causes the nginx configuration to get re-created inside the nginx pod by magix pixies. Now, assuming we have a route from our desktop to 10.244.2.13, we can just go to http://ghost.10.244.2.13.nip.io in a browser and you should be greeted by the default front page for the ghost installation (which turns out to be a publishing platform, who knew?).
To cleanup the ingress, you can use the normal “get”, “describe”, and “delete” verbs that you use for other things in kubectl, with the object type of “ingress”.