Setting up a three-node Docker Swarm for production

Kubernetes is too much for many small teams. Docker Swarm is the Docker you already know plus a few commands, and it handles dozens of services well. Here we build a stable three-node cluster.
Why three nodes?
Swarm decides with the Raft algorithm, and a majority of managers must always be alive. With three managers, the cluster keeps working if one fails. With two that isn't true: lose one and the cluster locks up.
Prerequisites
- Three Linux servers with Docker Engine installed. Say their IPs are 192.0.2.11, 192.0.2.12 and 192.0.2.13.
- These ports open only between the nodes, never to the internet:
2377/tcp,7946/tcp+udpand4789/udp. - All three clocks in sync via NTP.
Setting up Docker Swarm
On the first node, run:
docker swarm init --advertise-addr 192.0.2.11
docker swarm join-token manager
Run the command it prints on the other two nodes:
docker swarm join --token SWMTKN-1-xxxx 192.0.2.11:2377
Now docker node ls should show three nodes, all Reachable and one of them Leader.
The first stack
services:
web:
image: nginx:1.27
ports: ["80:80"]
deploy:
replicas: 3
update_config: { order: start-first, parallelism: 1 }
restart_policy: { condition: on-failure }
docker stack deploy -c stack.yml demo
order: start-first makes an update start the new container first and stop the old one after, so updates happen with no downtime.
A few production notes
- Secrets: create passwords with
docker secret create, not env. Secrets are stored encrypted in Raft. - Databases: pin a stateful service to one node with a constraint and set
endpoint_mode: dnsrr, so long-lived connections don't go through the VIP. - Images: pin images by digest so every node runs exactly the same version.
- Raft backup: back up
/var/lib/docker/swarmon a manager.
The next article covers database backups, without which no cluster counts as production.
How I got from cPanel to Docker Swarm is in nineteen years of keeping servers up, and database backups on this cluster are in PostgreSQL backups with PITR. The full reference is the Docker Swarm documentation.
Comments
Sign in to leave a comment.
Sign inLoading…



