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

霸气de小男生 提交于 2019-12-04 04:49:47

问题


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? 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:

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).

Thanks.


回答1:


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.

Try this custom Nginx image instead:

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.)

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.

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. Other requests got served by containers on the worker, so Docker is distributing them around all the tasks.



来源:https://stackoverflow.com/questions/39647064/docker-1-12-swarm-mode-load-balance-tasks-of-the-same-service-on-single-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!