Kubernetes: NGINX Ingress Controller on GKE


Updated: 2020-06-17

What is an ingress?

An Ingress gives you a way to route requests to services based on the request host or path, centralizing a number of services into a single entrypoint. With an Ingress, there is no need to create a bunch of Load Balancers or exposing each service on the Node. You can simply consolidate your routing rules into a single resource.

Ingress Options

There are many Ingress options, but I am going to focus on the community NGINX version.

What Network Layer is an Ingress configured?

Ingress configures a Layer 7 (Application Layer) HTTP load balancer for Services and provides the following:

  • TLS (Transport Layer Security)
  • Name-based virtual hosting 
  • Path-based routing
  • Custom rules

Install via Helm

Add repo

helm repo add nginx-stable https://helm.nginx.com/stable

Update repo

helm repo update

I installed the Ingress controller in the itsmetommy namespace and set controller.scope.enabled=true & controller.scope.namespace=itsmetommy so that the controller only watches my namespace instead of the default all namespaces.

  • controller.scope.enabled — limit the scope of the ingress controller
  • controller.scope.namespace — namespace to watch for ingress
helm install nginx-ingress \
  --set rbac.create=true \
  --set controller.publishService.enabled=true \
  --set controller.scope.enabled=true \
  --set controller.scope.namespace=itsmetommy \
  --namespace itsmetommy \
  nginx-stable/nginx-ingress

Verify.

kubectl get svc nginx-ingress-nginx-ingress -n itsmetommy
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ingress-nginx-ingress LoadBalancer 10.1.27.241 34.105.68.166 80:30113/TCP,443:31675/TCP 2m6s

Deployments

Create deployments.

{
kubectl create deploy service1 --image=itsmetommy/helloworld -n itsmetommy
kubectl create deploy service2 --image=itsmetommy/helloworld -n itsmetommy
kubectl create deploy service3 --image=itsmetommy/helloworld -n itsmetommy
kubectl create deploy circle --image=itsmetommy/shape-circle -n itsmetommy
kubectl create deploy heart --image=itsmetommy/shape-heart -n itsmetommy
kubectl create deploy infinity --image=itsmetommy/shape-infinity -n itsmetommy
kubectl create deploy lock --image=itsmetommy/shape-lock -n itsmetommy
kubectl create deploy moon --image=itsmetommy/shape-moon -n itsmetommy
kubectl create deploy pac-man --image=itsmetommy/shape-pac-man -n itsmetommy
kubectl create deploy space-invader --image=itsmetommy/shape-space-invader -n itsmetommy
kubectl create deploy square --image=itsmetommy/shape-square -n itsmetommy
kubectl create deploy star --image=itsmetommy/shape-star -n itsmetommy
kubectl create deploy triangle --image=itsmetommy/shape-triangle -n itsmetommy
kubectl create deploy yin-yang --image=itsmetommy/shape-yin-yang -n itsmetommy
}

Services

Create services.

{
kubectl expose deploy service1 --port=80 -n itsmetommy
kubectl expose deploy service2 --port=80 -n itsmetommy
kubectl expose deploy service3 --port=80 -n itsmetommy
kubectl expose deploy circle --port=80 -n itsmetommy
kubectl expose deploy heart --port=80 -n itsmetommy
kubectl expose deploy infinity --port=80 -n itsmetommy
kubectl expose deploy lock --port=80 -n itsmetommy
kubectl expose deploy moon --port=80 -n itsmetommy
kubectl expose deploy pac-man --port=80 -n itsmetommy
kubectl expose deploy space-invader --port=80 -n itsmetommy
kubectl expose deploy square --port=80 -n itsmetommy
kubectl expose deploy star --port=80 -n itsmetommy
kubectl expose deploy triangle --port=80 -n itsmetommy
kubectl expose deploy yin-yang --port=80 -n itsmetommy
}

Ingress

Download ingress-example.yaml. This is an example file that you can use as a starting point. I also added the annotation for cert-manager.

Ingress Examples

I’m not going to go over every example, but I will go over a few.

Create ingress.

kubectl apply -f ingress-example.yaml

Get.

kubectl get ingress -n itsmetommy

Describe.

kubectl describe ingress ingress-example -n itsmetommy

Test URLs.

curl -L service1.your-domain.com
Hello World! 3.0.0

curl -L www.service1.your-domain.com
Hello World! 3.0.0

curl -L service1.your-domain.com/foo
foo

curl -L service1.your-domain.com/v1
v1

curl -L service2.your-domain.com
Hello World! 3.0.0

curl -L www.service2.your-domain.com
Hello World! 3.0.0

curl -L service2.your-domain.com/bar
bar

curl -L service2.your-domain.com/v2
v2

https://circle.your-domain.com

You should see a red circle.

https://heart.your-domain.com

You should see a red heart.

https://infinity.your-domain.com

You should see a red infinity.

https://lock.your-domain.com

You should see a red lock.

https://moon.your-domain.com

You should see a red moon.

https://pac-man.your-domain.com

You should see a red pack-man.

https://space-invader.your-domain.com

You should see a red space invader.

https://square.your-domain.com

You should see a red square.

https://star.your-domain.com

You should see a red star.

https://triangle.your-domain-.com

You should see a red triangle.

https://yin-yang.your-domain.com

You should see a red yin yang.

Clean up

{
kubectl delete deploy circle -n itsmetommy
kubectl delete deploy heart -n itsmetommy
kubectl delete deploy infinity -n itsmetommy
kubectl delete deploy lock -n itsmetommy
kubectl delete deploy moon -n itsmetommy
kubectl delete deploy pac-man -n itsmetommy
kubectl delete deploy space-invader -n itsmetommy
kubectl delete deploy square -n itsmetommy
kubectl delete deploy star -n itsmetommy
kubectl delete deploy triangle -n itsmetommy
kubectl delete deploy yin-yang -n itsmetommy
kubectl delete svc service1 -n itsmetommy
kubectl delete svc service2 -n itsmetommy
kubectl delete svc service3 -n itsmetommy
kubectl delete svc circle -n itsmetommy
kubectl delete svc heart -n itsmetommy
kubectl delete svc infinity -n itsmetommy
kubectl delete svc lock -n itsmetommy
kubectl delete svc moon -n itsmetommy
kubectl delete svc pac-man -n itsmetommy
kubectl delete svc space-invader -n itsmetommy
kubectl delete svc square -n itsmetommy
kubectl delete svc star -n itsmetommy
kubectl delete svc triangle -n itsmetommy
kubectl delete svc yin-yang -n itsmetommy
kubectl delete ingress ingress-example -n itsmetommy
}

Delete Helm chart.

helm uninstall nginx-ingress
,