一个Kubernetes吊舱中的双nginx

一个Kubernetes吊舱中的双nginx,nginx,kubernetes,google-cloud-platform,Nginx,Kubernetes,Google Cloud Platform,我正在谷歌云中做一个关于kubernetes的实验室,所以我的任务是在一个pod中部署两台nginx服务器,但我有一个问题 其中一个POD无法启动,因为端口或IP正在使用购买另一个nginx容器,我需要在yaml文件中更改它,请给我一个解决方案,提前谢谢 apiVersion: v1 kind: Pod metadata: name: two-containers spec: restartPolicy: Never volumes: - name: shared-data

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

其中一个POD无法启动,因为端口或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()

在kubernetes中,POD中的容器共享单个网络名称空间。为了简化,两个容器不能侦听同一个pod中的同一端口

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

因此,我们将使用默认的nginx配置运行第一个容器,而对于第二个容器,我们将使用下面的配置运行

default.conf 从这个default.conf创建一个configmap 创建一个pod,如下所示。 部署吊舱

现在将exec插入pod并尝试在localhost:80和localhost:81上ping,这样就可以了。 如果你需要更多的帮助,请告诉我


在kubernetes中,POD中的容器共享单个网络名称空间。为了简化,两个容器不能侦听同一个pod中的同一端口

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

因此,我们将使用默认的nginx配置运行第一个容器,而对于第二个容器,我们将使用下面的配置运行

default.conf 从这个default.conf创建一个configmap 创建一个pod,如下所示。 部署吊舱

现在将exec插入pod并尝试在localhost:80和localhost:81上ping,这样就可以了。 如果你需要更多的帮助,请告诉我


为什么完全可以使用docker compose而不可能使用k8s?在docker compose中,我们只需要为第一个容器映射端口:['8000:80'],为第二个容器映射端口:['8001:80']。是的。我有和上面一样的问题。为什么在k8s中使用docker run或docker compose这么容易?docker compose仍然使用唯一的IP地址和网络名称空间运行每个容器,并创建覆盖网络默认值以将它们相互连接。请注意,当您需要连接到同一项目中的另一个容器时,您是如何通过DNS将其称为其服务名称的。而在Kubernetes中,pod中的每个容器都共享本地主机。为什么完全可以使用docker compose,而不可能使用k8s?在docker compose中,我们只需要为第一个容器映射端口:['8000:80'],为第二个容器映射端口:['8001:80']。是的。我有和上面一样的问题。为什么在k8s中使用docker run或docker compose这么容易?docker compose仍然使用唯一的IP地址和网络名称空间运行每个容器,并创建覆盖网络默认值以将它们相互连接。请注意,当您需要连接到同一项目中的另一个容器时,您是如何通过DNS将其称为其服务名称的。而在Kubernetes中,pod中的每个容器都共享本地主机。
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;
    #}
}

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