一个 Kubernetes pod 中的双 nginx

1 nginx google-cloud-platform kubernetes

我在做一个关于实验室kubernetes谷歌的云,所以我的任务是部署两台nginx的一个荚服务器,但是我有一个问题。

其中一个pods无法启动,因为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()
Run Code Online (Sandbox Code Playgroud)

小智 7

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

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

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

  • 默认配置文件
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;
    #}
}

Run Code Online (Sandbox Code Playgroud)
  • 从此创建配置映射 default.conf
kubectl create configmap nginx-conf --from-file default.conf
Run Code Online (Sandbox Code Playgroud)
  • 创建一个 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
Run Code Online (Sandbox Code Playgroud)
  • 部署 Pod。

  • 现在 exec 进入 pod 并尝试 ping localhost:80localhost:81它会工作。如果您需要更多帮助,请告诉我。