一个Kubernetes吊舱中的双Nginx [英] Dual nginx in one Kubernetes pod

查看:129
本文介绍了一个Kubernetes吊舱中的双Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在针对 google cloud 中的 kubernetes 做一个实验,所以我的任务是在一个吊舱中部署两台 nginx 服务器,但是我有一个问题.

I am doing a lab about kubernetes in google cloud, so my task is deploy two nginx servers in one pod, however I have a issue.

其中一个Pod无法启动,因为PORT或IP正在使用购买另一个Nginx容器,我需要在yaml文件中对其进行更改,请给我一个解决方案,谢谢您

One of the pods can not starts, as PORT or IP is using buy another nginx container, I need to change it in yaml file, please give me a solution, thank you in advance

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()

推荐答案

在kubernetes中,pod中的容器共享单个网络名称空间.为简化起见,两个容器无法在同一吊舱中监听相同的端口.

In kubernetes the container in pods share single network namespace. To simplify, two container cannot listen to same port, in same pod.

因此,要使两个Nginx容器位于同一容器中,您需要在不同的端口上运行它们.一个nginx可以在80上运行,另一个可以在81上运行.

So in order to two nginx container within same pod, you need to run them on different port. One nginx can run on 80 and other on 81.

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

So we will run first-container with default nginx config and for second-container we will be running with below config.

  • 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
  • 创建一个配置映射

    • Create a configmap from this default.conf
    • kubectl create configmap nginx-conf --from-file default.conf
      

      • 按如下所示创建广告连播.
      • 
        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.

          • Deploy the pod.

            现在将exec放入pod并尝试对localhost:80localhost:81进行ping操作. 让我知道,如果您需要更多帮助.

            Now exec into the pod and try to ping on localhost:80 and localhost:81 it will work. Let me know, if you need any more help in it.

            这篇关于一个Kubernetes吊舱中的双Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆