In this blog, we will learn about the Kubernetes Ingress controller and the need for an Ingress controller.
In our previous blog, we learned about the Service and its offerings like service discovery, load balancing, and exposure to the external world. Now why do we need the Ingress controller? Before Kubernetes released v1.1, there was no Ingress so people used to use Kubernetes with service only. Even before people migrated to Kubernetes, they used the legacy system like they use virtual machines or physical servers and on top of it they installed their applications and to access these applications they used enterprise-level load balancers like nginx.
The enterprise-level load balancing offered different capabilities like ratio-based load balancing, sticky session(if a request is going to a particular pod, then all the requests should go to the same pod), path-based load balancing, domain-based load balancing, whitelisting (allow specific IP range), black listing(block specific IP range) and so on.
How does Ingress solve the problem?
The load balancing offered by the Kubernetes service was a simple and round-robin mechanism i.e. simply distributing the traffic request. Kubernetes service was missing following services:
Enterprise and TLS load balancing
For every load balancer type service, the cloud provider would charge for each static public IP address
Ingress is solving the problem that Kubernetes service did not have enterprise-level load balancing capabilities. Kubernetes users will create a resource called an Ingress controller and the load balancing services like Nginx, will implement the Ingress controller and as a Kubernetes user, will deploy the ingress controller in the Kubernetes cluster using Helm chart or YAML manifest. Once it is deployed then the developer will create an Ingress yaml resource for their Kubernetes service. So the Ingress controller watches on Ingress resources.
Let's dive practically and understand how to deploy Ingress controller that watches on our Ingress resources.
Start the Minikube
minikube start
Previously in Kubernetes service, we had created the pods we will use the same pods. If you don't know how to do it please click here to create the pods.
Now let's create an Ingress resource and setup a host based load balancing
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-example
spec:
rules:
- host: "foo.bar.com"
http:
paths:
- pathType: Prefix
path: "/bar"
backend:
service:
name: python-django-service
port:
number: 80
To apply
kubectl apply -f ingress.yml
Our ingress resource is created.
We can the address field is empty.
We encountered the error as we could not reach our application because for this ingress resource we have not created the Ingress controller.
So let's install the INgress controller. IN the market there are lots of ingress controller, I am going to install the nginx ingress controller since it is very light-weight.
To enable the NGINX Ingress controller, run the following command:
minikube addons enable ingress
Verify that the NGINX Ingress controller is running
kubectl get pods -n ingress-nginx
Let's check whether the INgress controller identifies the ingress resources that we had created.
kubectl logs ingress-nginx-controller-6cc5ccb977-tq2ll -n ingress-nginx
So the ingress controller has identified our ingress resource and has successfully synced also.
If you noticed previously there was no IP address in the Address field but now there is IP allocated and updated. Now, we can access the ingress resources in foo.bar.com/bar since we have used the ingress controller and it has updated the configuration .
Now update the /etc/hosts configuration file so that whenever we hit the foo.bar.com the 192.168.58.2 will be accessed.
We will try to access the foo.bar.com
We are successfully able to send the request to the foo.bar.com. We have done it successfully.
By using Ingress controllers, Kubernetes users can efficiently manage external access to their applications, benefiting from features like host-based load balancing and improved traffic routing. As Kubernetes continues to evolve, understanding and using Ingress controllers will be important for optimizing application deployment and accessibility within a Kubernetes cluster.
Happy Learning!!