Ingress in Action
In our previous tutorial we learnt good amount of theory part on the Kubernetes Ingress. Let's work with it in this tutorial.
Pre-requisties
- A Deployment running in the cluster; we already have the Nginx deployment
- A Service; we have the NodePort Service which we can leverage
From our previous tutorials, we have both of these. However, the NodePort cluster internal IP, node IPs, and any other names may change from what was there in the previous tutorials as I have deleted and recreated them.
Ingress Manifest
We learnt that with Ingress we can acheive host-based routing. Til now we accessed our Nginx deployment
using IP addresses. Let's try accessing it with a domain ingress-test.com.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- host: "ingress-test.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-np-svc # name of our NodePort Service from previous tutorial
port:
number: 8080 # port that the NodePort Service listens on internally
kubectl apply -f ingress.yaml
Let's apply and try to access it.
vagrant@vagrant:~/kubernetes-tutorial$ kubectl apply -f ingress.yaml ingress.networking.k8s.io/nginx-ingress created vagrant@vagrant:~/kubernetes-tutorial$ vagrant@vagrant:~/kubernetes-tutorial$ curl ingress-test.com curl: (6) Could not resolve host: ingress-test.com vagrant@vagrant:~/kubernetes-tutorial$
But we got an error?? Remember the note from the previous Ingress tutorial. An Ingress has no effect without an Ingress Controller. Also notice that the Ingress is not assigned an IP address, which will happen with the Ingress Controller creation.
vagrant@vagrant:~/kubernetes-tutorial$ kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE nginx-ingress <none> ingress-test.com 80 92s
As we saw, there are many third part Ingress Controllers like Nginx, Apache, Kong etc.
Since we are on minikube, there is an easy way to setup Nginx Ingress Controller on minikube.
Minikube Ingress
To enable the NGINX Ingress controller, run:
minikube addons enable ingress
vagrant@vagrant:~/kubernetes-tutorial$ minikube addons enable ingress 💡 ingress is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub. You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS ▪ Using image registry.k8s.io/ingress-nginx/controller:v1.14.3 ▪ Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.6.7 ▪ Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.6.7 🔎 Verifying ingress addon... 🌟 The 'ingress' addon is enabled vagrant@vagrant:~/kubernetes-tutorial$
The Nginx Ingress Controller is added. Try to access again.
vagrant@vagrant:~/kubernetes-tutorial$ curl ingress-test.com curl: (6) Could not resolve host: ingress-test.com
Verify if the IP is assigned for the Ingress.
vagrant@vagrant:~/kubernetes-tutorial$ kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE nginx-ingress <none> ingress-test.com 192.168.49.2 80 46m vagrant@vagrant:~/kubernetes-tutorial$
Yes, but it's still the same. Wait a minute! Now, our Ubuntu host is not able to resolve the ingress-test.com.
It's obvious that our Ubuntu host does not know what IP to resolve for ingress-test.com.
For this, we need to edit the /etc/hosts on the host as below.
- Get the Ingress IP
- Add entry in the /etc/hosts file
kubectl get ingress
sudo cat "192.168.49.2 ingress-test.com" >> /etc/hosts
curl ingress-test.com
vagrant@vagrant:~/kubernetes-tutorial$ curl ingress-test.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
vagrant@vagrant:~/kubernetes-tutorial$
Great! That's working! Let's not dwelve in the details for now.
So, we saw how we can acheive the host-based routing with Ingress. Similarly, it can route requests to multiple backend services without having the need for multiple external cloud load balancers.
This way we can acheive all the features that we discussed in the previous tutorial with the help of Ingress.