Kubernetes - Ingress

Kubernetes Service can expose deployments to the external world. However it can’t route requests to services base on HTTP URI. Kubernetes Ingresss solves this problem.

Ingress exposes HTTP and HTTPS routes from outside the cluster to Services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource. Ingress also provides SSL termination.

Ingress Demo

Enable Nginx Ingress controller

1
$ minikube addons enable ingress

Create deployment with name ‘web’ and expose it as a service

1
2
$ kubectl run web --image=gcr.io/google-samples/hello-app:1.0 --port=8080
$ kubectl expose deployment web --target-port=8080 --type=NodePort

Create deployment with name ‘web2’ and expose it as a service

1
2
$ kubectl run web2 --image=gcr.io/google-samples/hello-app:2.0 --port=8080
$ kubectl expose deployment web2 --target-port=8080 --type=NodePort

Execute kubectl get services to verify both services are created.

1
2
3
4
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.107.251.151 <none> 8080:31454/TCP 7m
web2 NodePort 10.101.159.81 <none> 8080:32296/TCP 7m

Create Ingress.yaml that defines an Ingress. Path /v1/* routes to the first service. Path /v2/* routes to the second service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: hello-world.info
http:
paths:
- path: /v1/*
backend:
serviceName: web
servicePort: 8080
- path: /v2/*
backend:
serviceName: web2
servicePort: 8080

get minikube cluster ip

1
2
$minikube ip
192.168.99.112

add the following route to /etc/hosts

1
192.168.99.112 hello-world.info

Create the Ingress resource

1
kubectl apply -f example-ingress.yaml

Access services ‘web’ with url http://hello-world.info/v1

1
2
3
4
$ curl http://hello-world.info/v1
Hello, world!
Version: 1.0.0
Hostname: web-6d4657545d-hrjtv

Access service ‘web2’ with url http://hello-world.info/v2

1
2
3
4
$ curl  http://hello-world.info/v2
Hello, world!
Version: 2.0.0
Hostname: web2-674dd45977-xh72p

Ingress Controller

Ingress resource relies on Ingress controller to work. You can choose ingress controller implementation that works best for you. Kubernetes support GCE, nginx. There are other Ingress Controllers available too. Istio provide its own ingress controller if you are using Istio. see Ingress Controller for more information.

References