All posts
Tutorials

Setting up a three-node Docker Swarm for production

17 Nov 20262 min read5 views
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+udp and 4789/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/swarm on 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 in

Loading…

Related posts

How to install Claude Code and run your first sessionTutorials

How to install Claude Code and run your first session

Claude Code is a coding agent that runs in your terminal: it reads files, edits them, runs tests and commits. This guide covers installing it on macOS, Linux and Windows, your first session, and a few habits that pay off from day one.

25 Nov 20265 min