How to create Ingress rules in Kubernetes using Minikube

Writing Code in Dim Office
Reading Time: 4 minutes

In this blog, we will learn about Ingress and how to create ingress rules, So Stay Tuned!

PREREQUISITES

  • Minikube Cluster
  • kubectl Installed

WHAT IS INGRESS?

Ingress is an API object that allows the external traffic to the Kubernetes cluster based on the routing mechanism. It provides routing rules to manage external users’ access in a Kubernetes cluster via HTTP/HTTPS.

WHY DO WE USE INGRESS?

An ingress is used when you have multiple services on your cluster and want the user request to be routed to the service based on their path.

For Example- If I have to go to geeksforgeeks.com, so the geeksforgeeks domain will point to my cluster and my cluster should know where exactly this request is pointed and that job is done by ingress.

HOW DOES IT WORKS?

In the above diagram,

  • The user visits the URL into the browser that basically gets redirected to the ingress service.
  • Then service, forwards the request to the controller which then checks the rules, and based on these rules it takes a decision as to where the request has to be sent.
  • If it is geeksforgeeks.com, then we will go to this particular service and if it is tutorialspoint.com, then we will go to this service.
  • These services act as a load balancer to the pods and hence your request is being sent to the pod through the service.

Moreover for a better understanding, let’s move to the demo

1. If you don’t have minikube and kubectl installed on your system

Therefore, Refer- minikube & kubectl

2. Start your minikube cluster

minikube start

3. Enable nginx ingress controller

minikube addons enable ingress

4. Check the namespaces

kubectl get namespaces
Here, you will see “ingress-nginx”

5. To verify that it is running

kubectl get pods -n ingress-nginx

Now, let’s deploy a Hello World Application

1. Create a manifest file for deployment

apiVersion: apps/v1

kind: Deployment

metadata:

  name: ingress-demo

spec:

  selector:

    matchLabels:

      app: myapp

  replicas: 1

  template:

    metadata:

      labels:

        app: myapp

    spec:

      containers:

      - name: ingress-cont

        image: gcr.io/google-samples/hello-app:1.0

Here, I created a deployment with the name with 1 replica

2. Apply this file

kubectl apply -f [file-name]

kubectl apply -f ingress-deploy.yaml

3. Check the status of deployments, pods, and replicas

kubectl get deployments

kubectl get pods

kubectl get replicaset

4. Now, expose this deployment

kubectl expose deployment ingress-demo --type=NodePort --port=8080

5. Verify the service

kubectl get service

Access the application via the node-ip and the node port

6. To check the node ip

kubectl get nodes -o wide
Open your browser and type <node-ip>:<node-port> [192.168.49.2:31900] like this
You will see this output

7. Now, let’s create a manifest file for Ingress

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  name: ingress-app

  annotations:

    nginx.ingress.kubernetes.io/rewrite-target: /

spec:

  rules:

    - host: hello-worldapp.com

      http:

        paths:

          - path: /

            pathType: Prefix

            backend:

              service:

                name: ingress-demo

                port:

                  number: 8080

This manifest file is similiar to all other manifest files in kubernetes. Here, kind is Ingress and inside metadata filed, it consists of details about ingress like name, annotations. Inside spec section, we write rules and for rules we will be mentioning the host so that we can access the application with the domain name instead of ip address.

8. Apply this manifest file

kubectl apply -f [file-name]

9. Let’s do the verification

kubectl get ingress

The address filed shoel take couple of minutes

Add the hosts and address inside the hosts(/etc/hosts) file in your system

Go to your browser and type hello-worldapp.com

You will see this output

CONCLUSION

Instead of accessing your applications through ip address and port, create ingress rules and access your applications though domain.

HAPPY LEARNING!

REFERENCES

Introduction

Services in kubernetes