Kubernetes IngrePath Base Routing Using Kubernetes Ingress Controller

Posted By : Shivanshu Prasher | 04-Oct-2021

 

 

Kubernetes IngrePath Base Routing

 

Kubernetes Ingress is not a part of the Kubernetes service. Based on the rules defined to the Ingress, it  routes the traffic to the different services in Kubernetes. Here, we have to define everything inside the Yaml script i.e the traffic we want to send towards which service of kubernetes.
 
To Demonstrate the use of K8s Ingress controller, I am going to perform a task as :-
 
I have achieved Path Base routing in this task using k8s ingress controller.
 
In this task, I am using my own custom image having web services configured.
 

 

STEP-1: Here I create a deployment named search with replicas 3 and use my own custom image available on docker hub having a web server already configured.
As I set replica=3, that is why three pods launched.

 

Now, I want the traffic from outside of the minikube cluster to access my web services and I also want a load balancing facility.
 
To achieve this I exposed my deployment. --port 80 means I am exposing port number 80 of pods and --type=NodePort means traffic from outside the minikube cluster can access the services.By default type is ClusterIp which means only within the cluster the services can be accessed.
 
After exposing you can see a service is created with name search and port 80 is mapped with port 31038 of the base machine( minikube cluster Node).

 

 

Now, Just for demo purposes I am providing some dummy content to my webserver.
So, to do so I need to go inside the Pods. We can do this with the help of the kubectl exec command.

 

Inside /var/www/html/ we have index.html file I am putting dummy content here as webserver will read data from here.

In all 3 pods I have Put some dummy content.

192.168.99.109 is the IP of Minikube Cluster Node.And we expose port 80 of pod as they don;t have direct connectivity with outside world.

Here In the below picture you can see I am hitting my website And auto load balancing is happening as I have created the NodePort Service type.

 

Same Way I am creating one more deployment named mail with replicas 3 and using my custom image having web server already configured.

 

Now I am exposing the deployment or creating the SERVICE of type NodePort So that traffic from outside world can access minikube cluster services.

After exposing we can see a service with name mail has been created.

 

 

Now, For the deployment mail we have also three pods having web services running.

Putting dummy content to the default location from where webserver will read data.

 

With the help of Curl command I am hitting The webserver and we can se auto load balancing is happening as I created the service with type NodePort.

 

Now, instead of typing IP address and port number, I want to route traffic to load balancer according to the path mentioned within the domain.
 
To achieve this, I have to make entries in my hosts file for the local domain as the host file is a file that almost all computers and operating systems can use to map a connection between an IP address and domain names.
In Windows The Path of Hosts file is: C:\Windows\System32\drivers\etc\hosts
Make a entry here as shown below:

 

I am binding The Minikube cluster Node with domain abc.com

Now, to achieve Path base routing I have to write script in yml format to create ingress.

The Ingress Script is :

 

 

Now , Apply the script as :

 

Now, The traffic will be route according to the path.

curl abc.com/search ---> This will send traffic to the service named search because of the rule mention in the script. Firstly host name will be checked if matched then protocol if matched then path and then traffic will be route to service whichever mention in script. PathType prefix means ignore case sensation for path.

 

curl abc.com/mail ---> This will send traffic to the service named mail because of the rule mention in the script. Firstly host name will be checked if matched then protocol if matched then path and then traffic will be route to service whichever mention in script. PathType prefix means ignore case sensation for path.

 

We are an ERP development company that offers complete enterprise solutions for diverse business use-cases. Our development team specializes in using the latest tech stack and next-gen technologies like AI, machine learning, big data, IoT, and cloud computing. To learn more about our custom ERP development services, contact us at [email protected]


Please remove URL from text

Comment is required

Sending message..
Connect with us