如何在 Kubernetes 中公开一系列端口?

Val*_*eri 10 google-cloud-platform kubernetes

我们如何在 Kubernetes 中公开一系列端口?
我的云应用程序在运行时使用一系列端口(40000 ~42000)。

如何在 Kubernetes 服务 yaml 文件中指定公开的端口范围?

小智 9

一种选择是使用 go-templates 为所有端口生成 yaml,然后部署该 yaml,然后用于gomplate编译 yaml

很多端口.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: some-service
  labels:
    name: some-service
spec:
  ports:
  {{- range seq 0 2000}}
  - port: {{add 40000 .}}
    targetPort: {{add 40000 .}}
    name: exposed-port-{{add 40000 .}}
  {{- end -}}
  selector:
    name: some-service
  sessionAffinity: None
  type: ClusterIP
Run Code Online (Sandbox Code Playgroud)

然后编译yaml: gomplate -f lotsofports.yaml > service.yaml


Tho*_*mas 6

Kubernetes 服务目前不支持端口范围,请参见https://github.com/kubernetes/kubernetes/issues/23864

更新:截至 2021 年,有一个针对此要求的 kubernetes 增强提案字段:https ://github.com/kubernetes/enhancements/pull/2611