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

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Create a Minikube cluster

  1. Click Launch Terminal

    Launch Terminal

  2. (Optional) If you installed Minikube locally, run the following command:

    1. minikube start

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 kube-system

    Note: This can take up to a minute.

    Output:

    1. NAME READY STATUS RESTARTS AGE
    2. default-http-backend-59868b7dd6-xb8tq 1/1 Running 0 1m
    3. kube-addon-manager-minikube 1/1 Running 0 3m
    4. kube-dns-6dcb57bcc8-n4xd4 3/3 Running 0 2m
    5. kubernetes-dashboard-5498ccf677-b8p5h 1/1 Running 0 2m
    6. nginx-ingress-controller-5984b97644-rnkrg 1/1 Running 0 1m
    7. storage-provisioner 1/1 Running 0 2m

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

    Output:

    1. deployment.apps/web created
  2. Expose the Deployment:

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

    Output:

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

    1. kubectl get service web

    Output:

    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:

    1. minikube service web --url

    Output:

    1. http://172.17.0.15:31637

    Note: Katacoda environment only: at the top of the terminal panel, click the plus sign, and then click Select port to view on Host 1. Enter the NodePort, in this case 31637, and then click Display Port.

    Output:

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

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

Create an Ingress resource

The following file is an Ingress resource that sends traffic to your Service via hello-world.info.

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

service/networking/example-ingress.yaml Set up Ingress on Minikube with the NGINX Ingress Controller - 图1

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

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

    Output:

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

    1. kubectl get ingress

    Note: This can take a couple of minutes.

    1. NAME CLASS HOSTS ADDRESS PORTS AGE
    2. example-ingress <none> hello-world.info 172.17.0.15 80 38s
  3. Add the following line to the bottom of the /etc/hosts file.

    Note: If you are running Minikube locally, use minikube ip to get the external IP. The IP address displayed within the ingress list will be the internal IP.

    1. 172.17.0.15 hello-world.info

    This sends requests from hello-world.info to Minikube.

  4. Verify that the Ingress controller is directing traffic:

    1. curl hello-world.info

    Output:

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

    Note: If you are running Minikube locally, you can visit hello-world.info from your browser.

Create Second Deployment

  1. Create a v2 Deployment using the following command:

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

    Output:

    1. deployment.apps/web2 created
  2. Expose the Deployment:

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

    Output:

    1. service/web2 exposed

Edit Ingress

  1. Edit the existing example-ingress.yaml and add the following lines:

    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

    Output:

    1. ingress.networking/example-ingress configured

Test Your Ingress

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

    1. curl hello-world.info

    Output:

    1. Hello, world!
    2. Version: 1.0.0
    3. Hostname: web-55b8c6998d-8k564
  2. Access the 2nd version of the Hello World app.

    1. curl hello-world.info/v2

    Output:

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

    Note: If you are running Minikube locally, you can visit hello-world.info and hello-world.info/v2 from your browser.

What’s next