Docker 1.12 Swarm Mode - Load balance tasks of the

2020-04-17 04:13发布

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条回答
贼婆χ
2楼-- · 2020-04-17 05:05

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.

查看更多
登录 后发表回答