在 Google 容器引擎中公开两个端口

Exposing two ports in Google Container Engine

是否可以在公开两个端口的 Google 容器引擎中创建一个 Pod:端口 8080 正在侦听传入的内容,端口 80 将此内容分发给客户端?

以下创建 Pod 的命令由 Google 作为示例给出:

kubectl run hello-node --image=gcr.io/${PROJECT_ID}/hello-node --port=8080

我似乎无法定义监听端口,添加第二个“--port=”开关时,只有一个端口暴露。 有没有办法公开第二个端口,或者我是否仅限于每个容器一个端口?

不,您不能在 kubectl run 中指定多个端口。但是你可以使用kubectl create创建一个replication controller,并为容器指定多个端口。

https://github.com/kubernetes/examples/blob/master/cassandra/cassandra-statefulset.yaml有个例子:

ports:
- containerPort: 7000
  name: intra-node
- containerPort: 7001
  name: tls-intra-node
- containerPort: 7199
  name: jmx
- containerPort: 9042
  name: cql

Kubernetes 支持目标端口:

kubectl expose deployment example --type=LoadBalancer --port 8080 --target-port 80

你可以两次使用--port参数 kubectl 运行 hello-node --image=gcr.io/${PROJECT_ID}/hello-node --port=8080 --port=8081

在命令行中,可以使用 --overrides 选项指定多个端口:

此示例公开了端口 80 和 8080:

    export APP_NAME=app-hello
    export IMAGE=gcr.io/google-samples/hello-app:1.0
    kubectl run $APP_NAME \
                --image=$IMAGE \
                --overrides='{"spec": {"template":  {"spec": {"containers": [{"name": "'$APP_NAME'", "image": "'$IMAGE'",
 "ports": [{"containerPort": 8080, "protocol": "TCP"}, {"containerPort": 80, "protocol": "TCP"}]}]}}}}'

在另一个答案中指出使用 kubernetes 允许定位,但也允许多个端口:

kubectl expose deployment example --type=LoadBalancer --port 8080,8081 --target-port 80

如果需要通过helm,可以实现为:

deployment.yaml

        ports:
        - containerPort: {{ .Values.containerport1 }}
          #name: containerport1
        - containerPort: {{ .Values.containerport2 }}
          #name: containerport2
        - containerPort: {{ .Values.containerport3 }}
          #name: containerport3

服务文件需要有端口名,否则会出现渲染错误。

service.yaml

  ports:
  - name: containerport1
    protocol: TCP
    port: {{ .Values.exposedport1 }}
    targetPort: {{ .Values.containerport1 }}
  - name: containerport2
    protocol: TCP
    port: {{ .Values.exposedport2 }}
    targetPort: {{ .Values.containerport2 }}
  - name: containerport3
    protocol: TCP
    port: {{ .Values.exposedport3 }}
    targetPort: {{ .Values.containerport3 }}

可以在安装 helm chart 时使用 --set 参数设置这些值。

values.yaml

containerport1: 8001
containerport2: 8002
containerport3: 8003
exposedport1: 8004
exposedport2: 8005
exposedport3: 8006