简体   繁体   English

Docker 1.12群模式-单个节点上相同服务的负载均衡任务

[英]Docker 1.12 Swarm Mode - Load balance tasks of the same service on single node

On Docker 1.12 Swarm Mode, if i have more than one task of the same service running in a single node and publishing the same port, is possible to do any kind of load balance between the tasks? 在Docker 1.12集群模式下,如果我在同一个节点上运行多个同一个服务的任务并发布相同的端口,则可以在这些任务之间进行任何形式的负载平衡吗? Or, whats the purpose of having more instances of a service than the number of nodes? 或者,拥有更多服务实例而不是节点数量的目的是什么?
Eg. 例如。

node swarm init
node service create --name web --replicas=2 --publish=80:80 nginx

Now if i open the browser and access http://localhost/ (refreshing the page many times) all connections seems to be handled by the same task, as could be seem by doing: 现在,如果我打开浏览器并访问http:// localhost / (多次刷新页面),所有连接似乎都由同一任务处理,就像这样做:

docker logs [container1]
docker logs [container2]

PS: Ok, i know that makes no sense having a swarm of a single node, but the same seems to occur if i have 10 nodes on the swarm (with a service scaled to 10 replicas), and then i lose one of those nodes (2 tasks of the service will be running on the same node and one of them will never receive connections). PS:好的,我知道只有一个节点集群是没有意义的,但是如果我在集群中有10个节点(服务扩展到10个副本),似乎也会发生同样的情况,然后我失去了其中一个节点(该服务的2个任务将在同一节点上运行,其中一个将永远不会收到连接)。

Thanks. 谢谢。

No, the routing mesh does distribute requests among all the containers, even if several containers are running on the same node. 不,即使在同一节点上运行多个容器,路由网格也确实在所有容器之间分配请求。

You don't see that with the stock nginx image because it's configured with a high keep-alive setting, so your client keeps returning to the same container when you refresh. 在股票nginx图像中看不到它,因为它配置了高保活设置,因此您的客户端在刷新时会不断返回相同的容器。

Try this custom Nginx image instead: 请尝试使用此自定义Nginx图像:

docker service create --name nginx --replicas 10 -p 80:80 sixeyed/nginx-with-hostname

( sixeyed/nginx-with-hostname is an automated build, you can check the source on GitHub .) sixeyed / nginx-with-hostname是自动构建的,您可以在GitHub上检查源代码。)

There's a 1-second keep-alive specified, and a custom response header X-Host which tells you the hostname of the server - in this case it will be the container ID. 指定了一个1秒钟的保持活动状态,并使用一个自定义响应标头X-Host告诉您服务器的主机名-在这种情况下,它将是容器ID。

I made three successive requests, which all got served by different containers: 我提出了三个连续的请求,这些请求均由不同的容器提供:

> curl -k http://my-swarm.com/ | grep X-Host
X-Host: 5920bc3c7659 

> curl -k http://my-swarm.com/ | grep X-Host    
X-Host: eb228bb39f58 

> curl -k http://my-swarm.com/ | grep X-Host
X-Host: 891bafd52c90  

Those containers all happen to be running on the manager node in a 2-node swarm. 这些容器都恰好在2节点群集中的管理器节点上运行。 Other requests got served by containers on the worker, so Docker is distributing them around all the tasks. 其他请求由工作器上的容器满足,因此Docker将它们分配给所有任务。

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

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