How do networking and load balancer work in docker swarm mode? How do networking and load balancer work in docker swarm mode? docker docker

How do networking and load balancer work in docker swarm mode?


Not sure I can be clearer than the docs, but maybe rephrasing will help.

First, the doc you're following here uses what is called the swarm mode of docker.

What is swarm mode?

A swarm is a cluster of Docker engines, or nodes, where you deploy services. The Docker Engine CLI and API include commands to manage swarm nodes (e.g., add or remove nodes), and deploy and orchestrate services across the swarm.

From SO Documentation:

A swarm is a number of Docker Engines (or nodes) that deploy services collectively. Swarm is used to distribute processing across many physical, virtual or cloud machines.

So, with swarm mode you have a multi host (vms and/or physical) cluster a machines that communicate with each other through their docker engine.

Q1. What is webnet?

webnet is the name of an overlay network that is created when your stack is launched.

Overlay networks manage communications among the Docker daemons participating in the swarm

In your cluster of machines, a virtual network is the created, where each service has an ip - mapped to an internal DNS entry (which is service name), and allowing docker to route incoming packets to the right container, everywhere in the swarm (cluster).

Q2. So, by default, overlay network is load balanced in docker cluster ?

Yes, if you use the overlay network, but you could also remove the service networks configuration to bypass that. Then you would have to publish the port of the service you want to expose.

Q3. What is load balancing algo used ?

From this SO question answered by swarm master bmitch ;):

The algorithm is currently round-robin and I've seen no indication that it's pluginable yet. A higher level load balancer would allow swarm nodes to be taken down for maintenance, but any sticky sessions or other routing features will be undone by the round-robin algorithm in swarm mode.

Q4. Actually it is not clear to me why do we have load balancing on overlay network

Purpose of docker swarm mode / services is to allow orchestration of replicated services, meaning that we can scale up / down containers deployed in the swarm.

From the docs again:

Swarm mode has an internal DNS component that automatically assigns each service in the swarm a DNS entry. The swarm manager uses internal load balancing to distribute requests among services within the cluster based upon the DNS name of the service.

So you can have deployed like 10 exact same container (let's say nginx with you app html/js), without dealing with private network DNS entries, port configuration, etc... Any incoming request will be automatically load balanced to hosts participating in the swarm.

Hope this helps!