Kubernetes : Micro services running on same port?

7/4/2020

I am building a microservice full stack web application as (so far) :

ReactJS (client microservice) : listens on 3000

Authentication (Auth microservice) : listens on 3000 // accidently assigned the same port

Technically, what I have heard/learned so far is that we cannot have two Pods running on the same port. I am really confused how am I able to run the application (perfectly) like this with same ports on different applications/pods?

ingress-nginx config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
    name: ingress-service 
    annotations: 
        kubernetes.io/ingress.class: nginx 
        nginx.ingress.kubernetes.io/use-regex: 'true'
spec: 
    ## our custom routing rules  
    rules: 
        - host: ticketing.dev 
          http: 
            paths:
                - path: /api/users/?(.*) 
                  backend: 
                    serviceName: auth-srv 
                    servicePort: 3000
                - path: /?(.*)
                  backend: 
                    serviceName: client-srv
                    servicePort: 3000 

I am really curious, am I missing something here?

-- Karan Kumar
docker
google-cloud-platform
kubernetes
microservices

1 Answer

7/4/2020

Each Pod has its own network namespace and its own IP address, though the Pod-specific IP addresses aren't reachable from outside the cluster and aren't really discoverable inside the cluster. Since each Pod has its own IP address, you can have as many Pods as you want all listening to the same port.

Each Service also has its own IP address; again, not reachable from outside the cluster, though they have DNS names so applications can find them. Since each Service has its own IP address, you can have as many Services as you want all listening to the same port. The Service ports can be the same or different from the Pod ports.

The Ingress controller is reachable from outside the cluster via HTTP. The Ingress specification you show defines HTTP routing rules. If I set up a DNS service with a .dev TLD and define an A record for ticketing.dev that points at the ingress controller, then http://ticketing.dev/api/users/anything gets forwarded to http://auth-srv.default.svc.cluster.local:3000/ within the cluster, and http://ticketing.dev/otherwise goes to http://client-srv.default.svc.cluster.local:3000/. Those in turn will get forwarded to whatever Pods they're connected to.

There's no particular prohibition against multiple Pods or Services having the same port. I tend to like setting all of my HTTP Services to listen on port 80 since it's the standard HTTP port, even if the individual Pods are listening on port 3000 or 8000 or 8080 or whatever else.

-- David Maze
Source: StackOverflow