首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双nginx在一个Kubernetes荚中的应用

双nginx在一个Kubernetes荚中的应用
EN

Stack Overflow用户
提问于 2019-01-21 12:15:30
回答 1查看 2.8K关注 0票数 1

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

由于端口或IP正在使用购买另一个nginx容器,我需要在yaml文件中更改它,请给我一个解决方案,谢谢

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2019-01-22 11:10:50

在kubernetes中,pods中的容器共享单个网络命名空间。为了简化,两个容器不能听同一个港口,在同一个吊舱。

因此,为了使两个nginx容器位于同一个吊舱内,您需要在不同的端口上运行它们。一个nginx可以在80上运行,另一个可以在81上运行。

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

  • default.conf
代码语言:javascript
复制
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创建一个configmap
代码语言:javascript
复制
kubectl create configmap nginx-conf --from-file default.conf
  • 创建一个吊舱如下所示。
代码语言:javascript
复制
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
  • 展开吊舱。
  • 现在,执行人员进入吊舱,并尝试在localhost:80localhost:81上切换,它将工作。如果你需要更多的帮助,请告诉我。
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54289786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档