Use case: deploy your custom ingress controller

In this page we show how to deploy a custom ingress controller (nginx ingress controller) in our own k8s namespace.

We also show how to create an ingress resource and test the custom ingress controller.

Prerequisites

Helm 3 is used to deploy the ingress controller.

Quick way to install Helm for Ubuntu users:

$ sudo snap install helm --classic

Official Doc: https://helm.sh/docs/intro/install/

Furthermore, helm needs a valid kubeconfig file (named config and located in the ~/.kube folder) to work properly. Read how to create a valid kubeconfig and how to connect to our k8s cluster here: https://cloud.garr.it/containers/#getting-started

1. Add the ingress-nginx helm repository

$ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
$ helm repo update

2. Install ingress-nginx chart in our k8s namespace

$ helm install nginx-controller-custom ingress-nginx/ingress-nginx --set controller.ingressClass=nginx-controller-custom --set controller.publishService.enabled=true --set controller.scope.enabled=true --set controller.scope.namespace=<your-namespace> --set rbac.create=true --set rbac.scope=true --set controller.admissionWebhooks.enabled=false --version 2.16.0

Quick explanation of the most significant parameters and values used with the helm install command:

  • controller.ingressClass=nginx-controller-custom: assigns a class named nginx-controller-custom to this ingress controller. Ingress resources need to reference this class in the metadata.annotations field of their own yaml file. Assigning a class to an ingress controller is useful to differentiate this ingress controller from others.

  • controller.scope.enabled=true: configures the ingress controller so that it listens for ingress resources exclusively in the current namespace.

  • rbac.scope=true: allows the chart to create role and rolebinding resources for the ingress controller exclusively in the current namespace (it will not create cluesterrole and clusterrolebindings resources).

  • version 2.16.0: we use the 2.16 version of the chart in order to be able to install this chart with Kubernetes v1.13. The latest version of the chart is compatible with Kubernetes >=v1.16 only.

Helm install command for most recent Kubernetes versions:

$ helm install ingress-nginx ingress-nginx/ingress-nginx --namespace <your-namespace> --set controller.ingressClassResource.name=nginx-controller-custom --set controller.ingressClassResource.controllerValue="k8s.io/nginx-controller-custom" --set controller.ingressClassResource.enabled=true --set controller.ingressClassByName=true --set controller.scope.enabled=true --set controller.scope.namespace=<your-namespace> --set rbac.create=true --set rbac.scope=true -set controller.publishService.enabled=true

3. Test our custom ingress controller

Deploy a test application (eg: wordpress):

$ helm repo add bitnami https://charts.bitnami.com/bitnami

$ helm install mywordpress bitnami/wordpress

Create an ingress resource that references both our custom ingress controller and wordpress app:

$ vim ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx-controller-custom"
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: mywordpress
          servicePort: 80`


$ kubectl apply -f ingress.yaml

NOTE: we use the annotation kubernetes.io/ingress.class: “nginx-controller-custom” to tell k8s that this ingress resource will be managed by the ingress controller tagged with the ‘nginx-controller-custom’ ingress class. Also, we tell the ingress controller to point to our test wordpress application in the ‘rules’ section of the file.

Ingress resource for most recent Kubernetes versions

$ vim ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  ingressClassName: nginx-controller-custom
  rules:
  - http:
      paths:
      - backend:
          serviceName: mywordpress
          servicePort: 80`


$ kubectl apply -f ingress.yaml

Test if it is all working properly:

$ kubectl get ingress`

Take note of the ingress public ip address. Open a browser and enter the public ip address in the address bar. Enjoy your new ingress controller.