Ingress Controller

In order for ingress controller to operate we must have an ingress controller running. With the help of ingress we can expose multiple services using a single endpoint or single load balancer.
For this we need to expose our deployments without loadbalancer as a type using the below command:

kubectl expose deployment <deployment-name> --port <port no.>

Once we are done with the above command it will expose our deployments and will create a service for our deployment.

After that we need to deploy nginx ingress controller which is a ingress controller provided by kubernetes. Now suppose we are having three services that are need to be exposed or need to be accessed from outside of the cluster, we just simply need to pass the path along with the service name and port number which needs to be exposed. Later on we can access those particular services by hitting the endpoint of our ingress controller along with the path.

We will deploy the ingress controller using helm https://salesforcedrillers.com/learn-devops/helm/
First of all we need to add the kubernetes ingress controller stable chart repository to our helm repositories by using the below command:

helm repo add stable https://kubernetes-charts.storage.googleapis.com/

Once added then execute the below command to deploy the helm chart

helm install nginx-ingress stable/nginx-ingress	

After this it will create two pods of nginx-controller along with two services

ngnix-controller

Then we need to create two or more deployments so that we can expose them with single ingress endpoint. You can use the below yamls for creating deployments

NGINX DEPLOYMENT MAIN

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-main
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-main
  template:
    metadata:
      labels:
        run: nginx-main
    spec:
      containers:
      - image: nginx
        name: nginx

NGINX DEPLOYMENT GREEN

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-green
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-green
  template:
    metadata:
      labels:
        run: nginx-green
    spec:
      volumes:
      - name: webdata
        emptyDir: {}
      initContainers:
      - name: web-content
        image: busybox
        volumeMounts:
        - name: webdata
          mountPath: "/webdata"
        command: ["/bin/sh", "-c", 'echo "<h1>I am <font color=green>GREEN</font></h1>" > /webdata/index.html']
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: webdata
          mountPath: "/usr/share/nginx/html"

NGINX DEPLOYMENT BLUE

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-blue
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-blue
  template:
    metadata:
      labels:
        run: nginx-blue
    spec:
      volumes:
      - name: webdata
        emptyDir: {}
      initContainers:
      - name: web-content
        image: busybox
        volumeMounts:
        - name: webdata
          mountPath: "/webdata"
        command: ["/bin/sh", "-c", 'echo "<h1>I am <font color=blue>BLUE</font></h1>" > /webdata/index.html']
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: webdata
          mountPath: "/usr/share/nginx/html"

Now simply do kubectl apply -f then it will create 3 deployments based on the above yamls and then further expose the deployments with kubectl expose command that has been shown above in this section.

Now finally we will deploy our ingress.yaml with the help of yaml stated below:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: salesforcedrillers-ingress
spec:
  rules:
  - host: demo.salesforcedrillers.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-deploy-main
          servicePort: 80
      - path: /blue
        backend:
          serviceName: nginx-deploy-blue
          servicePort: 80
      - path: /green
        backend:
          serviceName: nginx-deploy-green
          servicePort: 80

NOTE:- REMEMBER TO REPLACE THE INGRESS HOST NAME WITH YOUR INGRESS ENDPOINT, AND IF YOU ARE USING MINIKUBE THEN YOU NEED TO DO THE ENTRY IN YOUR HOSTS FILE

After applying when you will simply hit the host name you will be redirected to the nginx-deploy-main service, when you will hit the endpoint with /blue you will be redirected to nginx-deploy-blue service and when you will hit the endpoint with /green you will be redirected to nginx-deploy-green service.

Subscribe Now