Docker Swarm’s load balancing

And one of the reasons it is not working on your system

Arthurio
2 min readAug 23, 2024

So, you’ve deployed a docker swarm service with 2 or more replicas and expect the traffic to be balanced out of the box (like anyone would). But, after testing the load balancing, you notice that all the traffic goes to just 1 container and you try different approaches to try to fix it by changing endpoint_mode, maybe if it’s nginx, you add a resolver, you try different container-specific configs, but nothing works and all the traffic still goes to 1 container.

You open the official documentation, and it says that it has already built-in load balancing and there are no indications at all that in reality, it depends on your kernel’s specific modules.

As with anything, there can be a lot of reasons why load balancing is not working in your environment, but if you check the Docker logs:

journalctl -u docker -n 100

and if you see something along these lines:

error reading the kernel parameter net.ipv4.vs.conn_reuse_mode" error="open /proc/sys/net/ipv4/vs/conn>
error reading the kernel parameter net.ipv4.vs.expire_nodest_conn" error="open /proc/sys/net/ipv4/vs/e>
error reading the kernel parameter net.ipv4.vs.expire_quiescent_template" error="open /proc/sys/net/ip>

(by the way, there is a great Docker checker that checks if you have all required or optional modules installed in your system):

bash <(curl -s https://raw.githubusercontent.com/coreos/docker/master/contrib/check-config.sh)

Then, it means that this kernel module that Docker Swarm is relying on, is lacking on your system, and all you have to do is to upgrade your kernel so that it includes those modules.

I’ve noticed this in a Proxmox environment with the Ubuntu 22.04 OS and the following line helped me to upgrade the kernel:

sudo apt install linux-generic-hwe-22.04

Then, reboot and then run the following to install the module:

 sudo modprobe ip_vs

Now you can run the Docker checker script again to make sure the module was installed correctly and re-check the load balancing.

Now Docker Swarm can successfully load-balance the traffic to your containers.

Let me know if there was another reason why load balancing didn’t work for you and how you fixed it.

--

--