How is load balancing done in Docker-Swarm mode How is load balancing done in Docker-Swarm mode docker docker

How is load balancing done in Docker-Swarm mode


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

enter image description here

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.

enter image description here


An important additional information to the existing answer

The advantage of using a proxy (HAProxy) in-front of docker swarm is, swarm nodes can reside on a private network that is accessible to the proxy server, but that is not publicly accessible. This will make your cluster secure.

If you are using AWS VPC, you can create a private subnet and place your swarm nodes inside the private subnet and place the proxy server in public subnet which can forward the traffic to the swarm nodes.

When you access the HAProxy load balancer, it forwards requests to nodes in the swarm. The swarm routing mesh routes the request to an active task. If, for any reason the swarm scheduler dispatches tasks to different nodes, you don’t need to reconfigure the load balancer.

For more details please read https://docs.docker.com/engine/swarm/ingress/