How to expose a range of ports in Kubernetes?

7/28/2019

How can we expose a range of ports in Kubernetes?
My cloud application is using a range of ports when running(40000 ~42000).

How do I specify a range of exposed ports in Kubernetes service yaml file?

-- Valeri
google-cloud-platform
kubernetes

3 Answers

7/29/2019

As @Thomas pointed is not supported yet.

However, as workaround you can try to use Helm templates. Create chart with service template and ports in values.yaml file.

-- PjoterS
Source: StackOverflow

7/28/2019

You can limit the ephemeral ports on the OS. The ephermal port range is specified in /proc/sys/net/ipv4/ip_local_port_range.

# more /proc/sys/net/ipv4/ip_local_port_range
9000    9200

As you can see, I set this value between 9000-9200 and any application which needs to use ephemeral ports will be using ports between this range. After editing this file, you might want to reboot your system though.

In addition to that, if you are using NodePort to expose your services, then you can also set specific ports on your service.yml file.

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30080
      name: http
    - port: 443
      nodePort: 30443
      name: https
  selector:
    name: nginx

hope it helps!

-- Armagan Karatosun
Source: StackOverflow

7/28/2019

Kubernetes services currently do not support port ranges, see https://github.com/kubernetes/kubernetes/issues/23864

-- Thomas
Source: StackOverflow