Load balancing with docker swarm

Viewed 2741

I have configured a 2 node docker swarm with a manager node pi-manager and a worker node pi-worker on a stack of 2 Raspberry Pi 3 Model 3 B running Raspbian. I have created an image that runs a helloworld type webpage on a local host that also identifies the container that the page is running on. Example:

example of docker image webpage

From the docker manager node I can create the service “flairhello” and then scale it to 2 continers (we will call them A and B) .

Expected Behavior:

The behavior I expected to get was that when I visited each localhost I would get the webpage displaying the container ID running on that host. For example if pi-manager has container A and pi-worker has container B. When I visit localhost address of pi-manager I would expect to see the container ID of A and when I visit the localhost address of pi-worker I would expect to see the container ID of B. This is NOT what is happening.

Current Behavior:

The behavior I am experiencing is that for some amount of time if I visit the localhost at either pi-manager or pi-worker I am directed to container A’s web page and then for the next section of time no matter which localhost (pi-manager or pi-worker) I visit I am always directed to Container B. I guess this is the built in load balancing of Docker swarm?

Questions:

How can I use a load balancer to get the behavior I want from my swarm?

What tools do I have to use? Docker compose? Haproxy image? (seen these mentioned from reading on this?

Are there any good tutorials for this process?


UPDATE:

Steps to create a Network Load Balancer with HAProxy in my answer below!

2 Answers
Related