如何在kubernetes上运行两个侦听同一端口的容器?

时间:2017-08-09 14:57:14

标签: kubernetes google-cloud-platform

这是我的配置:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - image: wordpress:latest
          name: wordpress
          imagePullPolicy: "Always"
          env:
            - name: WORDPRESS_HOST
              value: localhost
            - name: WORDPRESS_DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: username
          volumeMounts:
            - name: wordpress-persistent-storage
              mountPath: /var/www/html
        - image: nginx:latest
          name: nginx
          ports:
            - containerPort: 80
              name: nginx
        - image: gcr.io/cloudsql-docker/gce-proxy:1.09
          name: cloudsql-proxy
          command: ["/cloud_sql_proxy", "--dir=/cloudsql",
                    "-instances=abcxyz:europe-west1:wordpressdb=tcp:3306",
                    "-credential_file=/secrets/cloudsql/credentials.json"]
          volumeMounts:
            - name: cloudsql-instance-credentials
              mountPath: /secrets/cloudsql
              readOnly: true
            - name: ssl-certs
              mountPath: /etc/ssl/certs
            - name: cloudsql
              mountPath: /cloudsql
      volumes:
        - name: wordpress-persistent-storage
          gcePersistentDisk:
            pdName: wordpress-disk
            fsType: ext4

        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
        - name: cloudsql
          emptyDir:

我只想公开Nginx的端口80(充当负载均衡器)。但它无法启动,来自容器的日志:

2017/08/09 14:39:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/08/09 14:39:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/08/09 14:39:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/08/09 14:39:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/08/09 14:39:50 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2017/08/09 14:39:50 [emerg] 1#1: still could not bind()
nginx: [emerg] still could not bind()

我猜这是因为Wordpress容器已经在端口80上侦听。

我原以为他们会独立,没有任何端口冲突。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

  

我原以为他们会独立,没有任何端口冲突。我该如何解决这个问题?

Pod Pod 一个Pod,所有容器共享相同的网络命名空间 - 这是它们的一部分一个Pod。要完成您所说的"充当负载均衡器",请单独部署nginx Pod,并将其上游指向您将为wordpress Pod创建的服务。或者,当然,您也可以重新定位wordpress容器正在侦听的端口,但在执行此操作之前需要考虑以下内容。

除非你的负载均衡器"真的要考虑加载,而不仅仅是循环LB,创建服务的行为自然会分散与服务中选择器匹配的所有Pod的流量。 / p>