6.6 KiB
| title | content_type | weight | min-kubernetes-server-version |
|---|---|---|---|
| Set up Ingress on Minikube with the NGINX Ingress Controller | task | 110 | 1.19 |
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.
{{% heading "prerequisites" %}}
This tutorial assumes that you are using minikube to run a local Kubernetes cluster.
Visit Install tools to learn how to install minikube.
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} 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
-
To enable the NGINX Ingress controller, run the following command:
minikube addons enable ingress -
Verify that the NGINX Ingress controller is running
kubectl get pods -n ingress-nginx{{< note >}} It can take up to a minute before you see these pods running OK. {{< /note >}}
The output is similar to:
NAME READY STATUS RESTARTS AGE ingress-nginx-admission-create-g9g49 0/1 Completed 0 11m ingress-nginx-admission-patch-rqp78 0/1 Completed 1 11m ingress-nginx-controller-59b45fb494-26npt 1/1 Running 0 11m
Deploy a hello, world app
-
Create a Deployment using the following command:
kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0The output should be:
deployment.apps/web created -
Expose the Deployment:
kubectl expose deployment web --type=NodePort --port=8080The output should be:
service/web exposed -
Verify the Service is created and is available on a node port:
kubectl get service webThe output is similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web NodePort 10.104.133.249 <none> 8080:31637/TCP 12m -
Visit the Service via NodePort:
minikube service web --urlThe output is similar to:
http://172.17.0.15:31637The output is similar to:
Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564You 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.info.
-
Create
example-ingress.yamlfrom the following file:{{< codenew file="service/networking/example-ingress.yaml" >}}
-
Create the Ingress object by running the following command:
kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yamlThe output should be:
ingress.networking.k8s.io/example-ingress created -
Verify the IP address is set:
kubectl get ingress{{< note >}} This can take a couple of minutes. {{< /note >}}
You should see an IPv4 address in the
ADDRESScolumn; for example:NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> hello-world.info 172.17.0.15 80 38s -
Verify that the Ingress controller is directing traffic:
curl --resolve "hello-world.info:80:$( minikube ip )" -i http://hello-world.infoYou should see:
Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564You can also visit
hello-world.infofrom your browser.-
Optionally Look up the external IP address as reported by minikube:
minikube ipAdd line similar to the following one to the bottom of the
/etc/hostsfile on your computer (you will need administrator access):172.17.0.15 hello-world.info{{< note >}} Change the IP address to match the output from
minikube ip. {{< /note >}}After you make this change, your web browser sends requests for
hello-world.infoURLs to Minikube.
-
Create a second Deployment
-
Create another Deployment using the following command:
kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0The output should be:
deployment.apps/web2 created -
Expose the second Deployment:
kubectl expose deployment web2 --port=8080 --type=NodePortThe output should be:
service/web2 exposed
Edit the existing Ingress
-
Edit the existing
example-ingress.yamlmanifest, and add the following lines at the end:- path: /v2 pathType: Prefix backend: service: name: web2 port: number: 8080 -
Apply the changes:
kubectl apply -f example-ingress.yamlYou should see:
ingress.networking/example-ingress configured
Test your Ingress
-
Access the 1st version of the Hello World app.
curl --resolve "hello-world.info:80:$( minikube ip )" -i http://hello-world.infoThe output is similar to:
Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564 -
Access the 2nd version of the Hello World app.
curl --resolve "hello-world.info:80:$( minikube ip )" -i http://hello-world.info/v2The output is similar to:
Hello, world! Version: 2.0.0 Hostname: web2-75cd47646f-t8cjk{{< note >}} If you did the optional step to update
/etc/hosts, you can also visithello-world.infoandhello-world.info/v2from your browser. {{< /note >}}
{{% heading "whatsnext" %}}
- Read more about Ingress
- Read more about Ingress Controllers
- Read more about Services