【问题标题】:Dual nginx in one Kubernetes pod一个 Kubernetes pod 中的双 nginx
【发布时间】:2019-06-14 19:58:36
【问题描述】:

我正在 google cloud 中做一个关于 kubernetes 的实验室,所以我的任务是在一个 pod 中部署两个 nginx 服务器,但是我有一个问题。

其中一个 pod 无法启动,因为 PORT 或 IP 正在使用购买另一个 nginx 容器,我需要在 yaml 文件中更改它,请给我一个解决方案,提前谢谢

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: first-container
    image: nginx
  - name: second-container
    image: nginx

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: still could not bind()

E  nginx: [emerg] still could not bind()

【问题讨论】:

    标签: nginx kubernetes google-cloud-platform


    【解决方案1】:

    在 kubernetes 中,pod 中的容器共享单个网络命名空间。为简化起见,两个容器不能在同一个 pod 中监听同一个端口。

    所以为了在同一个 pod 中运行两个 nginx 容器,你需要在不同的端口上运行它们。一个 nginx 可以在 80 上运行,另一个在 81 上运行。

    所以我们将使用默认的 nginx 配置运行 first-container,对于 second-container,我们将使用以下配置运行。

    • default.conf
    server {
        listen       81;
        server_name  localhost;
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    
    • 从此default.conf 创建一个配置映射
    kubectl create configmap nginx-conf --from-file default.conf
    
    • 如下创建一个 pod。
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: two-containers
    spec:
      restartPolicy: Never
      volumes:
      - name: config
        configMap:
          name: nginx-conf
      containers:
      - name: first-container
        image: nginx
        ports:
        - containerPort: 80
      - name: second-container
        image: nginx
        ports:
        - containerPort: 81
        volumeMounts:
        - name: config
          mountPath: /etc/nginx/conf.d
    
    • 部署 pod。

    • 现在执行到 pod 并尝试 ping localhost:80localhost:81 它将工作。 如果您需要更多帮助,请告诉我。

    【讨论】:

    • 为什么完全可以用 docker-compose 而不能用 k8s?在 docker-compose 中,我们只需要为第一个容器映射ports: ['8000:80'] 等端口,为第二个容器映射ports: ['8001:80']
    • 是的。我有和上面一样的问题。为什么使用 docker rundocker-compose 这么容易,但在 k8s 中却不行?
    • Docker compose 仍然使用唯一的 IP 地址和网络命名空间运行每个容器,并创建一个覆盖网络 (default) 将它们相互连接。请注意,当您需要连接到同一项目中的另一个容器时,您如何通过 DNS 将其称为其服务名称。而在 Kubernetes 中,pod 中的每个容器共享 localhost
    猜你喜欢
    • 2020-02-10
    • 2016-09-28
    • 1970-01-01
    • 2020-07-27
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多