Kubernetes ingress controller same path multiple ports

6/11/2021

I have an application in node js that expose 2 ports, 80 for web and 5000 for a notification service with websockets. I want to deploy in azure kubernetes service and I followed the tutorial https://docs.microsoft.com/en-us/azure/aks/ingress-tls. Everything works fine but websockets don't.

This is the yaml of the ingress controller:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dih-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
  - hosts:
    - www.mydomain.com
    secretName: tls-secret
  rules:
  - host: www.mydomain.com
    http:
      paths:
            
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: dihkub-9865
            port:
              number: 80

And this is the port configuration in the service yaml:

spec:
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
    - name: websocket
      protocol: TCP
      port: 5000
      targetPort: 5000
  selector:
    app: dihkub-9865
  clusterIP: 10.0.147.128
  type: ClusterIP

I am new to this and sorry for my bad english, thanks

Edit: This is the new yaml file of the ingress controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dih-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
    cert-manager.io/cluster-issuer: letsencrypt

 
spec:
  tls:
  - hosts:
    - www.mydomain.com
    
    secretName: tls-secret
  rules:
  - host: www.mydomain.com
    http:
      paths:
      - path: /socket.io/(.*)
        backend:
          service: 
            name: dihkub-9865
            port: 
              number: 5000
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: dihkub-9865
            port:
              number: 80

Requests with the url /socket.io/ return error 502, now I put the services with loadbalancer so now I have 2 public IPs, the ingress controller handles requests to port 80 and the service handles requests from the websocket . This is not right but for now it works :(

Although the certificate with which the websockets work is not valid, since they do not work with the domain configured in the ingress controller, and having 2 public IPs is a bit more expensive.

-- cmb1197
azure-aks
kubernetes
nginx-ingress

1 Answer

6/11/2021

You probably need something like:

      - path: /websockets
        pathType: Prefix
        backend:
          service:
            name: dihkub-9865
            port:
              number: 5000

Or whatever path you want to use for the websockets server.

-- coderanger
Source: StackOverflow