How to setup Domain name or path based routing on ingress controller for kubernetes

Snehalhingane
5 min readNov 28, 2020

hey guys, perform this operation using kubernetes in simple and easy way…so lets get started!!!!!!

What is a domain name?

A domain name is a string of text that maps to a numeric IP address, used to access a website from client software. In plain English, a domain name is the text that a user types into a browser window to reach a particular website. For instance, the domain name for Google is ‘google.com’.

The actual address of a website is a complex numerical IP address (e.g. 103.21.244.0), but thanks to DNS, users are able to enter human-friendly domain names and be routed to the websites they are looking for. This process is known as a DNS lookup.

What is an Ingress?

In Kubernetes, an Ingress is an object that allows access to your Kubernetes services from outside the Kubernetes cluster. You configure access by creating a collection of rules that define which inbound connections reach which services.

This lets you consolidate your routing rules into a single resource. For example, you might want to send requests to example.com/api/v1/ to an api-v1 service, and requests to example.com/api/v2/ to the api-v2 service. With an Ingress, you can easily set this up without creating a bunch of LoadBalancers or exposing each service on the Node.

Kubernetes Ingress vs LoadBalancer vs NodePort

In Kubernetes, there are three general approaches to exposing your application.

  • Using a Kubernetes service of type NodePort, which exposes the application on a port across each of your nodes
  • Use a Kubernetes service of type LoadBalancer, which creates an external load balancer that points to a Kubernetes service in your cluster
  • Use a Kubernetes Ingress Resource

NodePort

A nodeport is an open port on every node of your cluster. Kubernetes transparently routes incoming traffic on the nodeport to your service, even if your application is running on a different node.

nodeport is a configuration setting you declare in a service’s YAML. Set the service spec’s type to nodeport. Then, Kubernetes will allocate a specific port on each Node to that service, and any request to your cluster on that port gets forwarded to the service.

This is cool and easy, it’s just not super robust. You don’t know what port your service is going to be allocated, and the port might get re-allocated at some point.

LoadBalancer

Using a LoadBalancer service type automatically deploys an external load balancer. This external load balancer is associated with a specific IP address and routes external traffic to a Kubernetes service in your cluster.

The exact implementation of a LoadBalancer is dependent on your cloud provider, and not all cloud providers support the LoadBalancer service type. Moreover, if you’re deploying Kubernetes on bare metal, you’ll have to supply your own load balancer implementation. That said, if you’re in an environment that supports the LoadBalancer service type, this is likely the safest, simplest way to route your traffic.

Every time you want to expose a service to the outside world, you have to create a new LoadBalancer and get an IP address.

Ingress

Kubernetes supports a high level abstraction called Ingress, which allows simple host or URL based HTTP routing. An ingress is a core concept (in beta) of Kubernetes, but is always implemented by a third party proxy. These implementations are known as ingress controllers. An ingress controller is responsible for reading the Ingress Resource information and processing that data accordingly.

Ingress is a completely independent resource to your service. You declare, create and destroy it separately to your services.

This makes it decoupled and isolated from the services you want to expose. It also helps you to consolidate routing rules into one place.

The one downside is that you need to configure an Ingress Controller for your cluster. But that’s pretty easy — in this example, we’ll use the Nginx Ingress Controller.

How to Use Nginx Ingress Controller

pre-requisites -

  1. install minikube
  2. install VM ware
  3. install kubernetes

Follow these steps to set up the Nginx Ingress Controller on your local Minikube cluster.

so firstly, start your minikube

Then, enable the ingress add-on for Minikube.

Creating a Kubernetes Ingress

figure 1
  1. so here, I have created namespace as dnsconfig. Inside the Ingress config i have define some rules i.e. host name for DNS configuration, service name, port and path. you can give any host name that you want.
figure 2

Now here, I have declare the image as nginx and created 2 replicas with container port 8080.

figure 3

service where ingress controller forwording the traffic.

after creating this yml file we have to apply so let apply it by using kubectl command

kubectl apply -f dnsconf.yml

so here, you will get namepsace that we define in yml file

Inside the dnsconfig namespace you will get all the information that you define in yml file.

Now here you will get all info related to ingress like port ,address,Host etc.

if you wants to describe your ingress then use following command

Perfect! Let’s check that it’s working. If you’re using Minikube, you might need to replace localhost with 192.168.99.100.(C:\Windows\System32\drivers\etc → for windows) go to this path and setup the hostname.

Yuupp!!!! now let try to ping from your DNS name

and its successfully pinging to the host…………..

Thanks for reading!!!!!

--

--