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

后端 未结 1 1672
野的像风
野的像风 2021-01-21 14:08

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 bet

相关标签:
1条回答
  • 2021-01-21 14:43

    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.

    0 讨论(0)
提交回复
热议问题