How do you give each node in a kubernetes cluster a random port number

416 Views Asked by At

I am working with a minecraft server image to create a cluster of statefulsets that all should have a random external port. I was told using a nodeport would do the job but not exactly how that is done. I was looking at nodeport but it looks like you would need to specify that exact port name.

I need each replica in a cluster to either have a random external IP or a random external port on the same IP, Is that possible or do I need to create a service for every single port/IP.

1

There are 1 best solutions below

1
On

You need to create a NodePort service for each instance of minecraft server.

A NodePort open a random port up to < 30000 and links it to an internal (set of) based on the selectors.

For instance, let's say there is one instance of the minecraft server with the following resource:

apiVersion: v1
kind: Pod
metadata:
  name: minecraft-instance1
  labels:
    instance: minecraft-1
spec:
...

This is the nodePort description to reach it on port 30007 (on every node of the cluster):

apiVersion: v1
kind: Service
metadata:
  name: service-minecraft-1
spec:
  type: NodePort
  selector:
    instance: minecraft-1
  ports:
    - port: 25565
      targetPort: 25565
      nodePort: 30007