Limit NodePort range for a specific namespace

Viewed 15

I have multiple namespaces, for some of them NodePort range is already reserved from 30000 to 32000. I want to deploy new set of jobs with Helm, and each of the jobs has two NodePort values, e.g. 32014 and 32015. I'm wondering if it is possible to either limit the range from which Kubernetes can choose the NodePort value to 32000-32200, or to add some kind of a function to the Helm template that will increment each new NodePort by 1 depending on how many are already deployed.. Is there any way to do this?

1 Answers

The Kubernetes documentation for NodePort services states:

If you set the type field to NodePort, the Kubernetes control plane allocates a port from a range specified by --service-node-port-range flag (default: 30000-32767). Each node proxies that port (the same port number on every Node) into your Service. Your Service reports the allocated port in its .spec.ports[*].nodePort field.

Reading this back:

  • In the Service spec, you can manually specify a single nodePort value; or
  • If you don't the cluster will allocate one for you, in a range configured by the cluster

There's no way to let the cluster choose a port, but only within a Service-specified range.

In a Helm context, an approach I've seen used is to let the Service type be configurable. If it's a NodePort-type Service, the chart will be able to determine the Service name at deployment time, and the NOTES.txt file can write out the appropriate kubectl get command to find out what the port number actually is. Here's one specific example from the bitnami/harbor chart.

Related