Load balancing with docker
I have configured a swarm 2 w630-docker with a manager node pi-manager
and a worker node pi-worker
on a stack 2 Raspberry Pi 3 Model 3 B running Raspbian. I created an image that runs a webpage of type helloworld on a local host, which also identifies the container the page is running on. Example:
From the docker node manager, I can create a "flairhello" service and then scale it to 2 continents (we'll call them A and B).
Expected Behavior:
The behavior I expected to get was that when I visited each localhost, I would get a webpage displaying the container id running on that host. For example, if it pi-manager
has a container A
and pi-worker
has a container B
. When I visit the localhost address pi-manager
, I expect to see the container ID A
, and when I find the localhost address from pi-worker
, I expect to see the container ID B
. This is NOT what is happening.
Current behavior:
The behavior I am experiencing is that for some time, if I am in localhost at the pi-manager
or level pi-worker
, I am directed to the container web page A
and then to the next section of time no matter which localhost ( pi-manager
or pi-worker
) I visit , I always head to the container B
. I'm guessing this is Docker's built-in rock load balancing?
Questions:
How can I use a load balancer to get the behavior I want from my swarm?
What tools do I need to use? Compose docker? Haproxy image? (did they see the ones mentioned above on this?
Are there any good tutorials for this process?
UPDATE:
The steps to create Network Load Balancing with HAProxy are in my answer below!
source to share
Yes, that's right. Docker road has a swarm routing grid https://docs.docker.com/engine/swarm/ingress/ and it's not sticky (ipvs) for every design. If you like being sticky you need to deploy a loadbalancer on your stack that supports session stickiness like Traefik ( https://traefik.io/ ).
But you can also go with Nginx like we do. β https://github.com/n0r1sk/border-controller . But this is more difficult.
source to share
Thanks @kleinsasserm for pointing me in the right direction, I found a solution to my problem! I created a load balancer using the base docker image of HAProxy and its Round Robin load balancing algorithm to create a welcome web page that will display the container it is powered on, alternating between containers, each one refreshed! This is a lab project used to demonstrate load balancing with docker.
Steps for this solution:
To set up and configure this load balancer, I did the following:
-
Created HAProxy Docker Image
-
Create a directory for your image
$ mkdir haproxyImage $ cd hapoxyImage
-
Create a docker file with the following content
FROM haproxy:1.7 COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
-
Create your
haproxy.cfg
fileglobal 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 10000 listen 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 a Docker image
$ docker build -t swarm-haproxy .
-
-
Start the Docker Swarm service:
$ docker service create -p 8080:80 --name helloworld --replicas 10 <image name>
-
Start with HAProxy image I am running this on the computer, not the pi stack
$ docker run -d -p 80:80 swarm-haproxy
-
On the machine running the HAproxy image, go to http://0.0.0.0 to refresh the page to show different containers running the same service
Links:
source to share