繁体   English   中英

在 docker swarm 中启动 nginx 期间忽略服务可用性

[英]Ignore the service availability during start nginx in docker swarm

该应用程序由多个服务组成,前面我们使用 nginx 作为 web 服务器。 我们在 docker 群中部署所有这些服务,包括 nginx。

docker-compose.yaml:

version: '3'
services:
  sa:
    image: xx.com/service-a
  sb:
    image: xx.com/service-b
  sc:
    image: xx.com/service-c
  ....
  gateway:
    image: nginx
    volumes:
      - /nginx.conf:/etc/nginx/conf.d/default.conf:ro
networks:
  overlay:

nginx.conf:

  location / {
    proxy_pass http://sa;
  }

  location /sb/ {
    proxy_pass http://sb;
  }

  location /sc/ {
    proxy_pass http://sc;
  }
  ......

到目前为止一切顺利,但是当启动堆栈时,如果其中一项服务(例如sc )无法启动,则会导致nginx无法启动,从而使我们的整个应用程序不可用。

似乎 docker 嵌入的 dns 服务器无法解析主机sc ,因为它尚未启动。

我们不希望单个服务影响整个应用程序,听起来这可以描述为另一个问题:“如何让 nginx 在启动期间忽略上游/代理的可用性”。 虽然搜索后没有解决方案。 任何想法?

您可以延迟 dns 分辨率,直到您需要它。 所以 nginx 可以在不做 dns 分辨率的情况下重启。

你必须在你的proxy_pass指令中使用一个变量。

...
set $backend "http://serviceD" ;
proxy_pass $backend;
...

所以我能够用这个docker-compose.yamldefault.conf来模拟你的问题

在 nginx 镜像中,默认解析器指向 127.0.0.11

resolver 127.0.0.11 valid=30s;
resolver_timeout 5s;

server {
listen 0.0.0.0:80;
server_name localhost;

location / {
    proxy_pass http://serviceA;
}

location /sd/ {
    set $backend "http://serviceD" ;
    proxy_pass $backend;
}

}


version: '3.7'
services:
  serviceA:
    image: debian:stretch-slim
    command: ["sleep","3600" ]
  serviceD:
    image: debian:stretch-slim
    command: ["sleep","3600" ]
  nginx:
    image: nginx
    volumes:
      - ${PWD}/default.conf:/etc/nginx/conf.d/default.conf:ro
    command: ["/bin/sh","-c","exec nginx -g 'daemon off;'"]
    restart: always
    ports:
      - target: 80
        published: 8080
        protocol: tcp
        mode: host
  testD:
    image: alpine:latest
    restart: always
    command: ["/bin/sh","-c","( apk add --no-cache bind-tools && host serviceD && ping -c 8 -i 4 serviceD )" ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM