Deploying and Managing Services at Scale with Docker Swarm
Using an External Load Balancer with Docker Swarm
Usually, a cluster resides in a private network and is not accessible from the outside world. This is a common and good practice as it adds an extra layer of security. It's not recommended to expose all the nodes of your cluster to the outside world. Therefore, it is common to use a front-end load balancer.
For instance, you can use an AWS Application Load Balancer (ALB), Traefik, HAProxy, or Nginx as a front-end load balancer. In this guide, we will focus on setting up an HAProxy load balancer.
To begin, create a new machine outside the cluster that is on the same local network as the cluster. We will refer to this machine as "loadbalancer" and use Ubuntu 22.04 as the operating system.
Next, you need to install HAProxy on the loadbalancer machine:
sudo apt update
sudo apt install -y haproxy
Now, let's configure HAProxy. We need to add the following configuration to the /etc/haproxy/haproxy.cfg file:
# export Wordpress service port
export PORT=8000
export MANAGER_IP=
export WORKER01_IP=
export WORKER02_IP=
# create HAProxy configuration file
cat <> /etc/haproxy/haproxy.cfg
# Configure HAProxy to listen on port 80
frontend lb
bind *:80
stats uri /haproxy?stats
default_backend wordpress
# Route to all nodes in the cluster on port 8000 to reach the wordpress application
backend wordpress Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
