Exposing two ports in Google Container Engine

眉间皱痕 提交于 2019-12-04 19:49:12

问题


Is it possible to create a Pod in the Google Container Engine where two ports are exposed: port 8080 is listening for incoming content and port 80 distributes this content to clients?

The following command to create a Pod is given as example by Google:

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

I can't seem to define a listening port, and when adding a second "--port=" switch only one port is exposed. Is there a way to expose a second port or am I limited to one port per container?


回答1:


No, you cannot specify multiple ports in kubectl run. But you can use kubectl create to create a replication controller, and specify multiple ports for the container.

https://github.com/kubernetes/examples/blob/master/cassandra/cassandra-statefulset.yaml has an example:

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



回答2:


From the command line, it is possible to specify multiple ports using the --overrides option:

This example exposes both ports 80 and 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"}]}]}}}}'



回答3:


Kubernetes supports a target port:

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




回答4:


Pointed out in another answer using kubernetes allows targeting, but also multiple ports:

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



回答5:


If required through the helm, it can be achieved as:

deployment.yaml

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

The service file needs to have a port name else it will give a render error.

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 }}

The values can be set using the --set parameter while installing helm chart.

values.yaml

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



回答6:


You can use --port paramater two times kubectl run hello-node --image=gcr.io/${PROJECT_ID}/hello-node --port=8080 --port=8081



来源:https://stackoverflow.com/questions/34502022/exposing-two-ports-in-google-container-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!