Adding Nginx Ingress controller to your Kubernetes cluster

You’ve created your Kubernetes cluster, added a service, therefore you’ve set up deployments, services and ingress but now you want to expose the cluster to the outside world.

We need to add am ingress controller such as nginx.

  • helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
    
  • helm repo update
    
  • helm install ingress-nginx ingress-nginx/ingress-nginx \
      --create-namespace \
      --namespace ingress-nginx \
      --set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-health-probe-request-path"=/healthz \
      --set controller.service.externalTrafficPolicy=Local
    

Note: In my case my namespace is ingress-nginx, but you can set to what you prefer.

Now I should say, originally I installed using

helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace

and I seemed to not be able to reach this from the web, but including here just for reference.

To get your EXTERNAL-IP, i.e. the one exposed to the web, use the following (replace the -n with the namespace you used).

kubectl get svc ingress-nginx-controller -n ingress-nginx

If you’re using some script to get the IP, you can extract just that using

kubectl get svc ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}'

Now my IP is not static, so upon redeploying the controller it’s possible this might change, so be aware of this. Ofcourse to get around it you could create a static IP with Azure (at a nominal cost).

Still not accessing your services, getting 404’s with Nginx web page displayed ?

Remember that in your ingress (i.e. the services ingress), you might have something similar to below.

Here, we set the host name, hence in this case the service will NOT be accessed via the IP itself, it needs to be used via the domain name so it matches against the ingress for the service.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-ingress
  namespace: development
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: mydomain.com  # Replace with your actual domain
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-service
            port:
              number: 80