Search code examples
kubernetesnginx-ingress

how to add another listening port to the nginx ingress controller kubernetes?


by default, nginx ingress listens to two ports 80 and 443, how to add listening on port 9898 I tried to change it in daemon set, but nothing came out , I don 't even know where else to dig


Solution

  • I'm not sure what will work exactly for you, but here's a few things you can try to approach this (read carefully because nginx is confusing):

    1. Define service for your deployment, and make sure it covers port routes you want and support on deployment end:

       apiVersion: v1
       kind: Service
       metadata:
         name: web-app
         namespace: web
         labels:
           app: web-app
       spec:
         ports:
         - port: 80
           targetPort: 1337
           protocol: TCP
         selector:
           app: web-app
      
    2. Refer to it in nginx ingress:

       rules:
       - host: mycoolwebapp.com
         http:
           paths:
           - path: /
             pathType: Prefix
             backend:
               service:
                 name: web-app
                 port:
                   number: 80
      

    The catch here is that you can route ALL services via port 80 but use any target port you want, so that you can, say, add 50 ingress hosts/routes over a morning routing to port 80 and only difference they'll have is target port in service.
    3. If you are specifically unhappy with ports 80 and 443, you are welcome to edit ingress-nginx-controller (service one, because as I said nginx is confusing).
    4. Alternatively, you can find example of ingress-nginx-controller service on the web, customize it and apply, then connect ingress to it... but I advise against this because if nginx doesn't like anything you set up as custom service, it's easier to just reinstall whole helm release of it and try again.