Load balancing with docker swarm Load balancing with docker swarm docker docker

Load balancing with docker swarm


Yes this is correct. The Docker swarm has swarm routing mesh https://docs.docker.com/engine/swarm/ingress/, and this is not sticky(ipvs) per design. If you like to be sticky, you have to deploy a loadbalancer in your stack which supports session stickiness, for example Traefik (https://traefik.io/).

But you can also go with Nginx, for example, like we do. -> https://github.com/n0r1sk/border-controller. But this is more sophisticated.


Thanks to @kleinsasserm for pointing me in right direction I found a solution to my problem! I created a load balancer using a base HAProxy docker image and its Round Robin load balancing algorithm to create a hello world web page that will display the container it is on, alternating the container each refresh! This is a lab project used to demonstrate load balancing using docker.

Steps for this solution:

In order to set up and configure this Load balancer I did the following:

  1. Created HAProxy Docker Image

    • Create a directory for your image

      $ mkdir haproxyImage$ cd hapoxyImage
    • Create you're docker file with the following contents

      FROM haproxy:1.7COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
    • Create your haproxy.cfg file

      global    daemon                           log 127.0.0.1 local0 notice     maxconn 256                 defaults    log     global    mode    http    option  httplog    option  dontlognull    retries 3    option redispatch    timeout connect  5000    timeout client  10000    timeout server  10000listen stats    bind 0.0.0.0:80 # This is the page you will access    mode http    stats enable    stats uri /haproxy?stats    stats realm Strictly\ Private    stats auth A_Username:user    stats auth Another_User:password    balance roundrobin # Defines our balancing algorithm as round robin.    option httpclose    option forwardfor    server pi-manager <ip address>:8080 check # Docker node    server pi-worker1 <ip address>:8080 check # Docker node    # Add more docker nodes here
    • Build your Docker image

      $ docker build -t swarm-haproxy .
  2. Start your Docker Swarm Service:

    $ docker service create -p 8080:80 --name helloworld --replicas 10 <image name>
  3. Start you HAProxy image I am running this on a computer not the pi stack

    $ docker run -d -p 80:80 swarm-haproxy
  4. On the Machine running the HAproxy image go to http://0.0.0.0 refresh the page to show the different containers running the same service


    References: