kubernetes service, Can the same Nodeport value (ie 30150) be used on 3 different Nodes?

2/29/2020

I need to access to two differents Worker nodes for the same nodePort 30150.

Service1.yml

apiVersion: v1
kind: Service
metadata:
  namespace: postgres
  name: postgres-db-node1-service
  labels:
    name: database
    node: node1
    system: postgres
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 30150
  selector:
    name: database
    **node: node1**
    system: postgres

Service2.yml

apiVersion: v1
kind: Service
metadata:
  namespace: postgres
  name: postgres-db-node2-service
  labels:
    name: database
    node: node2
    system: postgres
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 30150
  selector:
    name: database
    **node: node2**
    system: postgres

However, It failed when the service2 is applied:

The Service "postgres-db-node1-service" is invalid: spec.ports[0].nodePort: Invalid value: 30150: provided port is already allocated

Why couldn't I use the same port for different nodes? Or how could I solve that with Kubernetes?

Thank you in advance for your help!

Regards,

-- Javier Richard Quinto Ancieta
kubernetes

1 Answer

2/29/2020

You don't need to create multiple services with the same nodeport for it to be accessible from different nodes. When you create a Nodeport service the same nodeport(ranging from 30000-32767) is opened on all of your nodes and you should be able to access it via any of the nodes IP and the same nodeport.nodeport

-- Arghya Sadhu
Source: StackOverflow