2019-02-18 02:36:33 +00:00
---
title: Set up Ingress on Minikube with the NGINX Ingress Controller
2020-05-30 19:10:23 +00:00
content_type: task
2019-02-18 02:36:33 +00:00
weight: 100
2021-08-03 13:33:43 +00:00
min-kubernetes-server-version: 1.19
2019-02-18 02:36:33 +00:00
---
2020-05-30 19:10:23 +00:00
<!-- overview -->
2019-02-18 02:36:33 +00:00
2020-08-06 02:56:33 +00:00
An [Ingress ](/docs/concepts/services-networking/ingress/ ) is an API object that defines rules which allow external access
2020-04-02 13:01:50 +00:00
to services in a cluster. An [Ingress controller ](/docs/concepts/services-networking/ingress-controllers/ ) fulfills the rules set in the Ingress.
2019-02-18 02:36:33 +00:00
This page shows you how to set up a simple Ingress which routes requests to Service web or web2 depending on the HTTP URI.
2020-05-30 19:10:23 +00:00
## {{% heading "prerequisites" %}}
2019-02-18 02:36:33 +00:00
{{< include " task-tutorial-prereqs . md " > }} {{< version-check > }}
2021-08-03 13:33:43 +00:00
If you are using an older Kubernetes version, switch to the documentation
for that version.
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
### Create a Minikube cluster
2020-05-30 19:10:23 +00:00
2021-08-03 13:33:43 +00:00
Using Katacoda
: {{< kat-button > }}
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
Locally
: If you already [installed Minikube ](/docs/tasks/tools/#minikube )
locally, run `minikube start` to create a cluster.
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
<!-- steps -->
2019-02-18 02:36:33 +00:00
## Enable the Ingress controller
1. To enable the NGINX Ingress controller, run the following command:
```shell
minikube addons enable ingress
```
2020-08-06 02:56:33 +00:00
2019-02-18 02:36:33 +00:00
1. Verify that the NGINX Ingress controller is running
2021-09-09 03:10:39 +00:00
2021-08-03 13:33:43 +00:00
{{< tabs name = "tab_with_md" > }}
{{% tab name="minikube v1.19 or later" %}}
2021-06-09 16:55:53 +00:00
```shell
kubectl get pods -n ingress-nginx
```
2021-08-03 13:33:43 +00:00
{{< note > }}It can take up to a minute before you see these pods running OK.{{< / note > }}
2021-06-09 16:55:53 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2021-06-09 16:55:53 +00:00
```
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
```
2021-08-03 13:33:43 +00:00
{{% /tab %}}
{{% tab name="minikube v1.18.1 or earlier" %}}
2021-06-09 16:55:53 +00:00
```shell
kubectl get pods -n kube-system
```
2021-08-03 13:33:43 +00:00
{{< note > }}It can take up to a minute before you see these pods running OK.{{< / note > }}
2021-06-09 16:55:53 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2021-06-09 16:55:53 +00:00
```
NAME READY STATUS RESTARTS AGE
default-http-backend-59868b7dd6-xb8tq 1/1 Running 0 1m
kube-addon-manager-minikube 1/1 Running 0 3m
kube-dns-6dcb57bcc8-n4xd4 3/3 Running 0 2m
kubernetes-dashboard-5498ccf677-b8p5h 1/1 Running 0 2m
nginx-ingress-controller-5984b97644-rnkrg 1/1 Running 0 1m
storage-provisioner 1/1 Running 0 2m
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
Make sure that you see a Pod with a name that starts with `nginx-ingress-controller-` .
{{% /tab %}}
{{< / tabs > }}
2021-09-09 03:10:39 +00:00
2019-02-18 02:36:33 +00:00
## Deploy a hello, world app
1. Create a Deployment using the following command:
2021-08-03 13:33:43 +00:00
```shell
kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
The output should be:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
deployment.apps/web created
```
2019-02-18 02:36:33 +00:00
2020-08-06 02:56:33 +00:00
1. Expose the Deployment:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
kubectl expose deployment web --type=NodePort --port=8080
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
The output should be:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
service/web exposed
```
2020-08-06 02:56:33 +00:00
2019-02-18 02:36:33 +00:00
1. Verify the Service is created and is available on a node port:
```shell
2021-08-03 13:33:43 +00:00
kubectl get service web
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.104.133.249 < none > 8080:31637/TCP 12m
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
1. Visit the Service via NodePort:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
minikube service web --url
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
http://172.17.0.15:31637
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
{{< 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** .{{< / note > }}
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
Hello, world!
Version: 1.0.0
Hostname: web-55b8c6998d-8k564
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
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.
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
## Create an Ingress
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
The following manifest defines an Ingress that sends traffic to your Service via hello-world.info.
2019-02-18 02:36:33 +00:00
1. Create `example-ingress.yaml` from the following file:
2021-08-03 13:33:43 +00:00
{{< codenew file = "service/networking/example-ingress.yaml" > }}
2019-02-27 23:15:30 +00:00
2021-08-03 13:33:43 +00:00
1. Create the Ingress object by running the following command:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
The output should be:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
ingress.networking.k8s.io/example-ingress created
```
2019-02-18 02:36:33 +00:00
2020-08-06 02:56:33 +00:00
1. Verify the IP address is set:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
kubectl get ingress
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
{{< note > }}This can take a couple of minutes.{{< / note > }}
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
You should see an IPv4 address in the ADDRESS column; for example:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```
NAME CLASS HOSTS ADDRESS PORTS AGE
example-ingress < none > hello-world.info 172.17.0.15 80 38s
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
1. Add the following line to the bottom of the `/etc/hosts` file on
2021-11-02 05:38:41 +00:00
your computer (you will need administrator access):
2019-06-19 19:56:18 +00:00
2019-02-27 23:15:30 +00:00
```
172.17.0.15 hello-world.info
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
{{< 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.{{< / note > }}
After you make this change, your web browser sends requests for
hello-world.info URLs to Minikube.
2019-02-18 02:36:33 +00:00
2019-02-27 23:15:30 +00:00
1. Verify that the Ingress controller is directing traffic:
2019-02-18 02:36:33 +00:00
2019-02-27 23:15:30 +00:00
```shell
2019-02-18 02:36:33 +00:00
curl hello-world.info
2019-02-27 23:15:30 +00:00
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
You should see:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
2019-02-18 02:36:33 +00:00
Hello, world!
Version: 1.0.0
Hostname: web-55b8c6998d-8k564
```
2019-02-27 23:15:30 +00:00
{{< note > }}If you are running Minikube locally, you can visit hello-world.info from your browser.{{< / note > }}
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
## Create a second Deployment
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
1. Create another Deployment using the following command:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0
```
The output should be:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
deployment.apps/web2 created
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
1. Expose the second Deployment:
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
```shell
kubectl expose deployment web2 --port=8080 --type=NodePort
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
The output should be:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
service/web2 exposed
```
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
## Edit the existing Ingress {#edit-ingress}
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
1. Edit the existing `example-ingress.yaml` manifest, and add the
following lines at the end:
2019-02-18 02:36:33 +00:00
2019-05-09 11:38:48 +00:00
```yaml
2022-03-21 20:15:38 +00:00
- path: /v2
pathType: Prefix
backend:
service:
name: web2
port:
number: 8080
2019-05-09 11:38:48 +00:00
```
2019-02-18 02:36:33 +00:00
1. Apply the changes:
2021-08-03 13:33:43 +00:00
```shell
kubectl apply -f example-ingress.yaml
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
You should see:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
ingress.networking/example-ingress configured
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
## Test your Ingress
2019-02-18 02:36:33 +00:00
1. Access the 1st version of the Hello World app.
2021-08-03 13:33:43 +00:00
```shell
curl hello-world.info
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
Hello, world!
Version: 1.0.0
Hostname: web-55b8c6998d-8k564
```
2019-02-18 02:36:33 +00:00
1. Access the 2nd version of the Hello World app.
2021-08-03 13:33:43 +00:00
```shell
curl hello-world.info/v2
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
The output is similar to:
2020-08-06 02:56:33 +00:00
2021-08-03 13:33:43 +00:00
```
Hello, world!
Version: 2.0.0
Hostname: web2-75cd47646f-t8cjk
```
2019-02-18 02:36:33 +00:00
2021-08-03 13:33:43 +00:00
{{< note > }}If you are running Minikube locally, you can visit hello-world.info and hello-world.info/v2 from your browser.{{< / note > }}
2019-02-18 02:36:33 +00:00
2020-05-30 19:10:23 +00:00
## {{% heading "whatsnext" %}}
2019-02-18 02:36:33 +00:00
* Read more about [Ingress ](/docs/concepts/services-networking/ingress/ )
2019-02-27 23:15:30 +00:00
* Read more about [Ingress Controllers ](/docs/concepts/services-networking/ingress-controllers/ )
2019-02-18 02:36:33 +00:00
* Read more about [Services ](/docs/concepts/services-networking/service/ )