How is load balancing done in Docker-Swarm mode

2019-03-17 14:50发布

问题:

I'm working on a project to set up a cloud architecture using docker-swarm. I know that with swarm I could deploy replicas of a service which means multiple containers of that image will be running to serve requests.

I also read that docker has an internal load balancer that manages this request distribution.

However, I need help in understanding the following:

Say I have a container that exposes a service as a REST API or say its a web app. And If I have multiple containers (replicas) deployed in the swarm and I have other containers (running some apps) that talk to this HTTP/REST service.

Then, when I write those apps which IP:PORT combination do I use? Is it any of the worker node IP's running these services? Will doing so take care of distributing the load appropriately even amongst other workers/manager running the same service?

Or should I call the manager which in turn takes care of routing appropriately (even if the manager node does not have a container running this specific service)?

Thanks.

回答1:

when I write those apps which IP:PORT combination do I use? Is it any of the worker node IP's running these services?

You can use any node that is participating in the swarm, even if there is no replica of the service in question exists on that node. So you will use Node:HostPort combination. The ingress routing mesh will route the request to an active container.

One Picture Worth Ten Thousand Words

Will doing so take care of distributing the load appropriately even amongst other workers/manager running the same service?

The ingress controller will do round robin by default.

Now The clients should use dns round robin to access the service on the docker swarm nodes. The classic DNS cache problem will occur. To avoid that we can use external load balancer like HAproxy.