2017-08-09 110 views
0

这是我的配置:如何运行两个在kubernetes上同一端口上侦听的容器?

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

我会假设他们是独立的,没有任何港口冲突。我该如何解决这个问题?

跨越荚这是真的,但分离舱,所有容器都共享同一个网络的命名空间 - 这是什么使他们荚一部分。为了实现你所说的“充当负载均衡器”,分开部署nginx Pod,并将其上游指向您将为wordpress Pod创建的服务。或者,当然,您也可以重新定位wordpress容器正在监听的端口,但在执行此操作之前需要考虑以下事项。除非你的“负载平衡器”真的会考虑负载,而不仅仅是一个循环LB,创建一个服务的行为自然会将流量分散到与选择器匹配的所有Pod上在服务中。

+0

您的意思是将nginx容器部署在单独的容器中吗?另外我将如何重新安置港口? –

+0

对于第一个问题,是的;对于第二个问题,我将不得不看看'wordpress:latest'图像来查看它们提供的配置选项,但简短版本将改变(我所假定的)它的嵌入式nginx并将它的'listen 0.0.0.0 :8080;'然后相应地'containerPort:8080' –

相关问题