Set up Ingress on Minikube with the NGINX Ingress Controller

An Ingress is an API object that defines rules which allow external access to services in a cluster. An Ingress controller fulfills the rules set in the Ingress.

This page shows you how to set up a simple Ingress which routes requests to Service ‘web’ or ‘web2’ depending on the HTTP URI.

Before you begin

This tutorial assumes that you are using minikube to run a local Kubernetes cluster. Visit Install tools to learn how to install minikube.

Note:

This tutorial uses a container that requires the AMD64 architecture. If you are using minikube on a computer with a different CPU architecture, you could try using minikube with a driver that can emulate AMD64. For example, the Docker Desktop driver can do this.

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Your Kubernetes server must be at or later than version 1.19. To check the version, enter kubectl version. If you are using an older Kubernetes version, switch to the documentation for that version.

Create a minikube cluster

If you haven’t already set up a cluster locally, run minikube start to create a cluster.

Enable the Ingress controller

  1. To enable the NGINX Ingress controller, run the following command:

    1. minikube addons enable ingress
  2. Verify that the NGINX Ingress controller is running

    1. kubectl get pods -n ingress-nginx

    Note:

    It can take up to a minute before you see these pods running OK.

    The output is similar to:

    1. NAME READY STATUS RESTARTS AGE
    2. ingress-nginx-admission-create-g9g49 0/1 Completed 0 11m
    3. ingress-nginx-admission-patch-rqp78 0/1 Completed 1 11m
    4. ingress-nginx-controller-59b45fb494-26npt 1/1 Running 0 11m

Deploy a hello, world app

  1. Create a Deployment using the following command:

    1. kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0

    The output should be:

    1. deployment.apps/web created

    Verify that the Deployment is in a Ready state:

    1. kubectl get deployment web

    The output should be similar to:

    1. NAME READY UP-TO-DATE AVAILABLE AGE
    2. web 1/1 1 1 53s
  2. Expose the Deployment:

    1. kubectl expose deployment web --type=NodePort --port=8080

    The output should be:

    1. service/web exposed
  3. Verify the Service is created and is available on a node port:

    1. kubectl get service web

    The output is similar to:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. web NodePort 10.104.133.249 <none> 8080:31637/TCP 12m
  4. Visit the Service via NodePort, using the minikube service command. Follow the instructions for your platform:

    1. minikube service web --url

    The output is similar to:

    1. http://172.17.0.15:31637

    Invoke the URL obtained in the output of the previous step:

    1. curl http://172.17.0.15:31637
    1. # The command must be run in a separate terminal.
    2. minikube service web --url

    The output is similar to:

    1. http://127.0.0.1:62445
    2. ! Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

    From a different terminal, invoke the URL obtained in the output of the previous step:

    1. curl http://127.0.0.1:62445

    The output is similar to:

    1. Hello, world!
    2. Version: 1.0.0
    3. Hostname: web-55b8c6998d-8k564

    You can now access the sample application via the Minikube IP address and NodePort. The next step lets you access the application using the Ingress resource.

Create an Ingress

The following manifest defines an Ingress that sends traffic to your Service via hello-world.example.

  1. Create example-ingress.yaml from the following file:

    1. service/networking/example-ingress.yaml
    1. apiVersion: networking.k8s.io/v1
    2. kind: Ingress
    3. metadata:
    4. name: example-ingress
    5. spec:
    6. ingressClassName: nginx
    7. rules:
    8. - host: hello-world.example
    9. http:
    10. paths:
    11. - path: /
    12. pathType: Prefix
    13. backend:
    14. service:
    15. name: web
    16. port:
    17. number: 8080
  2. Create the Ingress object by running the following command:

    1. kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml

    The output should be:

    1. ingress.networking.k8s.io/example-ingress created
  3. Verify the IP address is set:

    1. kubectl get ingress

    Note:

    This can take a couple of minutes.

    You should see an IPv4 address in the ADDRESS column; for example:

    1. NAME CLASS HOSTS ADDRESS PORTS AGE
    2. example-ingress nginx hello-world.example 172.17.0.15 80 38s
  4. Verify that the Ingress controller is directing traffic, by following the instructions for your platform:

    Note:

    The network is limited if using the Docker driver on MacOS (Darwin) and the Node IP is not reachable directly. To get ingress to work you’ll need to open a new terminal and run minikube tunnel.
    sudo permission is required for it, so provide the password when prompted.

    1. curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    1. minikube tunnel

    The output is similar to:

    1. Tunnel successfully started
    2. NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    3. The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    4. sudo permission will be asked for it.
    5. Starting tunnel for service example-ingress.

    From within a new terminal, invoke the following command:

    1. curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example

    You should see:

    1. Hello, world!
    2. Version: 1.0.0
    3. Hostname: web-55b8c6998d-8k564
  5. Optionally, you can also visit hello-world.example from your browser.

    Add a line to the bottom of the /etc/hosts file on your computer (you will need administrator access):

    Look up the external IP address as reported by minikube

    1. minikube ip
  1. ```
  2. 172.17.0.15 hello-world.example
  3. ```
  4. #### Note:
  5. Change the IP address to match the output from `minikube ip`.
  6. ```
  7. 127.0.0.1 hello-world.example
  8. ```
  9. After you make this change, your web browser sends requests for `hello-world.example` URLs to Minikube.

Create a second Deployment

  1. Create another Deployment using the following command:

    1. kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0

    The output should be:

    1. deployment.apps/web2 created

    Verify that the Deployment is in a Ready state:

    1. kubectl get deployment web2

    The output should be similar to:

    1. NAME READY UP-TO-DATE AVAILABLE AGE
    2. web2 1/1 1 1 16s
  2. Expose the second Deployment:

    1. kubectl expose deployment web2 --port=8080 --type=NodePort

    The output should be:

    1. service/web2 exposed

Edit the existing Ingress

  1. Edit the existing example-ingress.yaml manifest, and add the following lines at the end:

    1. - path: /v2
    2. pathType: Prefix
    3. backend:
    4. service:
    5. name: web2
    6. port:
    7. number: 8080
  2. Apply the changes:

    1. kubectl apply -f example-ingress.yaml

    You should see:

    1. ingress.networking/example-ingress configured

Test your Ingress

  1. Access the 1st version of the Hello World app.

    1. curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    1. minikube tunnel

    The output is similar to:

    1. Tunnel successfully started
    2. NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    3. The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    4. sudo permission will be asked for it.
    5. Starting tunnel for service example-ingress.

    From within a new terminal, invoke the following command:

    1. curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example
  1. The output is similar to:
  2. ```
  3. Hello, world!
  4. Version: 1.0.0
  5. Hostname: web-55b8c6998d-8k564
  6. ```
  1. Access the 2nd version of the Hello World app.

    1. curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2
    1. minikube tunnel

    The output is similar to:

    1. Tunnel successfully started
    2. NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...
    3. The service/ingress example-ingress requires privileged ports to be exposed: [80 443]
    4. sudo permission will be asked for it.
    5. Starting tunnel for service example-ingress.

    From within a new terminal, invoke the following command:

    1. curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example/v2

    The output is similar to:

    1. Hello, world!
    2. Version: 2.0.0
    3. Hostname: web2-75cd47646f-t8cjk

    Note:

    If you did the optional step to update /etc/hosts, you can also visit hello-world.example and hello-world.example/v2 from your browser.

What’s next