Nginx在Docker服务发现中的http_502上重试相同的端点
我们将 docker swarm 与服务发现一起用于后端 REST 应用程序. swarm中的服务已配置为endpoint_mode: vip
,并在global
模式下运行. Nginx是使用服务发现别名传递的代理.当我们更新后端服务时,nginx有时会抛出502,因为服务发现可能指向更新的服务.
We use docker swarm with service discovery for Backend REST application. The services in swarm are configured with endpoint_mode: vip
and are running in global
mode. Nginx is proxy passed with service discovery aliases. When we update Backend services sometimes nginx throws 502 as service discovery may point to the updating service.
在这种情况下,我们想再次重试相同的端点.我们怎样才能做到这一点?
In such case, We wanted to retry the same endpoint again. How can we achieve this?
如果 http://docker.service1:8080/endpoint1 抛出 502 ,我们要点击 http://docker.service1:8080/endpoint1 .
其他查询:
- 泊坞窗群中是否有任何方法可以使其停止指向服务发现中的更新服务,直到该服务完全启动?
- 由于我们直接使用docker服务发现,因此上游是否必要?
I suggest you add a health check directly at container level (here)
这样做,docker会定期ping您指定的端点,如果发现该端点不健康,它将1)停止将流量路由到该端点2)杀死该容器并重新启动一个新容器.因此,您的上游将被解析为运行状况良好的容器之一.无需重试.
By doing so, docker pings periodically an endpoint you specified, if it's found unhealthy it will 1) stop routing traffic to it 2) kill the container and restart a new one. Therefore you upstream will be resolved to one of the healthy containers. No need to retry.
关于您的其他问题,第一个问题是,docker在健康之前不会开始路由.第二,nginx仍然可用于根据端点url分配流量.但是个人认为nginx + swarm vip模式不是一个很好的选择,因为swarm负载均衡器的文档很少,它不支持粘性会话,并且您不能进行代理级运行状况检查,我会使用traefik
,它有自己的负载均衡器.
As for your additional questions, the first one, docker won't start routing til it's healthy. The second, nginx is still useful to distribute traffic according to endpoint url. But personally nginx + swarm vip mode is not a great choice because swarm load balancer is poorly documented, it doesn't support sticky session and you can't have proxy level health check, I would use traefik
instead, it has its own load balancer.