Docker Swarm
Is a single Docker instance struggling to handle the traffic? Swarm lets you scale from 1 to 3 instances and deploy new versions with zero downtime.
1. What You'll Learn
- Swarm Cluster Architecture and Initialization
- The Difference Between a Service and a Container
- Rolling Updates and Rollback Strategies
- Secrets and Configuration Management
- Multi-node network routing
2. A Story of E-commerce Operations
(1) Pain Point: A single server can't handle the traffic
Traffic to Charlie’s e-commerce business skyrocketed, and a single Docker instance couldn’t handle it—during peak hours, CPU usage hit 100%, API responses timed out, and user complaints poured in. He needed to scale to multiple servers, but manually starting containers on three separate servers was too complicated.
(2) Solutions for Docker Swarm Clusters
Charlie initialized a 3-node Swarm cluster, scaled the API to 5 replicas, and deployed the new version using rolling updates with zero downtime.
# Initialize Swarm cluster
docker swarm init --advertise-addr 10.0.0.1
# Deploy service with 5 replicas
docker service create --replicas 5 --name api -p 8080:80 myapp:v2
# Rolling update to v3
docker service update --image myapp:v3 api
(3) Benefits: From a single machine to a cluster—done in 5 minutes
Three servers were combined into a cluster, with the API automatically distributed across five replicas and rolling updates performed with zero downtime. The transition from a single server to a cluster took only five minutes.
3. Swarm Architecture
(1) Node Roles
graph TB
MGR1["Manager Node 1<br/>Leader"] --- MGR2["Manager Node 2<br/>Standby"]
MGR1 --- MGR3["Manager Node 3<br/>Standby"]
MGR1 --> WRK1["Worker Node 1"]
MGR2 --> WRK2["Worker Node 2"]
MGR3 --> WRK3["Worker Node 3"]
| Role | Responsibilities | Number | Runs Containers |
|---|---|---|---|
| Manager | Cluster management, scheduling, API | Odd number (3/5/7) | ✅ Optional |
| Worker | Executes tasks, runs containers | Any | ✅ Required |
(1) Service vs Container
| Dimension | Container | Service |
|---|---|---|
| Creation Method | docker run |
docker service create |
| Number of Replicas | 1 | Configurable replicas |
| Self-healing | Requires restart policy | ✅ Automatically maintains the number of replicas |
| Load Balancing | None | ✅ Routing Mesh |
| Rolling Updates | Manual | ✅ Built-in |
| Use Cases | Single-machine development | Cluster production |
4. Swarm Initialization
▶ Example: Initializing with swarm init (Difficulty: ⭐⭐)
# On Manager node: initialize the swarm
docker swarm init --advertise-addr 10.0.0.1
# Output includes the join token for workers
# docker swarm join --token SWMTKN-xxx 10.0.0.1:2377
# On Worker nodes: join the swarm
docker swarm join --token SWMTKN-xxx 10.0.0.1:2377
# List all nodes
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
abc123 * manager1 Ready Active Leader
def456 worker1 Ready Active
ghi789 worker2 Ready Active
5. Service Management
▶ Example: service create deployment (Difficulty: ⭐⭐)
# Create a service with 3 replicas
docker service create \
--name web \
--replicas 3 \
--publish 80:80 \
nginx:1.25-alpine
# List services
docker service ls
# Check service tasks (containers)
docker service ps web
▶ Example: Service Scale Scaling (Difficulty: ⭐⭐)
# Scale to 5 replicas
docker service scale web=5
# Verify
docker service ps web
▶ Example: service update—rolling update (Difficulty: ⭐⭐⭐)
# Rolling update: update 2 containers at a time with 30s delay
docker service update \
--image nginx:1.26-alpine \
--update-parallelism 2 \
--update-delay 30s \
--update-failure-action rollback \
web
# Check update status
docker service inspect web --format='{{.UpdateStatus.State}}'
(1) Rolling Update Strategy
| Parameter | Default Value | Description |
|---|---|---|
--update-parallelism |
1 | How many dungeons are added with each update? |
--update-delay |
0s | Wait time between batches |
--update-failure-action |
pause | Failure Action(pause/rollback/continue) |
--update-monitor |
5s | Monitoring time after update |
▶ Example: Service Rollback (Difficulty: ⭐⭐)
# Rollback to previous version
docker service rollback web
6. Secrets and Configuration Management
▶ Example: Creating and Using Docker Secrets (Difficulty: ⭐⭐⭐)
# Create a secret from a file
echo "my_super_secret_password" | docker secret create db_password -
# Use the secret in a service
docker service create \
--name api \
--secret db_password \
-e DB_PASSWORD_FILE=/run/secrets/db_password \
myapp:1.0
/run/secrets/<name> file and are not exposed in environment variables.**
7. Routing Mesh
(1) Network Routing Mechanisms
Swarm's Routing Mesh allows any port on any node to route to any replica of a service—even if that replica is not on the current node.
graph LR
A["Node 1<br/>:80"] -->|"request"| B["Routing Mesh"]
C["Node 2<br/>:80"] --> B
B -->|"round-robin"| D["Replica 1<br/>Node 1"]
B --> E["Replica 2<br/>Node 2"]
B --> F["Replica 3<br/>Node 3"]
| Pattern | Syntax | Description |
|---|---|---|
| VIP (Default) | --publish 80:80 |
Virtual IP + Load Balancing |
| DNSRR | --endpoint-mode dnsrr |
DNS round-robin, no VIP |
8. Swarm vs Compose Comparison
| Dimension | Docker Compose | Docker Swarm |
|---|---|---|
| Number of Nodes | Single-Node | Multi-Node Cluster |
| High Availability | ❌ | ✅ Manager Redundancy |
| Service Self-Healing | Restart Policy | ✅ Maintain Number of Replicas |
| Rolling Updates | Manual | ✅ Built-in |
| Load Balancing | Nginx/DNS Round-Robin | ✅ Routing Mesh |
| Secrets | Environment Variables | ✅ Encrypted Storage |
| Deployment method | docker compose up |
docker stack deploy |
▶ Example: Deploying a Compose file to Swarm using stack deploy (Difficulty: ⭐⭐⭐)
# Deploy a compose file as a stack
docker stack deploy -c docker-compose.yml mystack
# List stacks
docker stack ls
# List services in a stack
docker stack services mystack
# Remove a stack
docker stack rm mystack
9. Complete Example: Deploying a Three-Node Cluster
# ============================================
# Complete walkthrough: 3-node Swarm cluster
# Covers: init, join, service, scale, update
# ============================================
# --- On Manager (10.0.0.1) ---
# 1. Initialize Swarm
docker swarm init --advertise-addr 10.0.0.1
# 2. Get worker join token
docker swarm join-token worker
# --- On Workers (10.0.0.2, 10.0.0.3) ---
# 3. Join the swarm (paste the token from step 2)
docker swarm join --token SWMTKN-xxx 10.0.0.1:2377
# --- On Manager ---
# 4. Verify cluster
docker node ls
# 5. Deploy web service with 5 replicas
docker service create \
--name web \
--replicas 5 \
--publish 80:80 \
--restart-condition on-failure \
nginx:1.25-alpine
# 6. Check distribution across nodes
docker service ps web
# 7. Rolling update to v1.26
docker service update \
--image nginx:1.26-alpine \
--update-parallelism 2 \
--update-delay 30s \
web
# 8. Simulate node failure
docker node update --availability drain worker1
docker service ps web # Replicas rescheduled
# 9. Restore node
docker node update --availability active worker1
# 10. Scale down
docker service scale web=2
# 11. Clean up
docker service rm web
docker swarm leave --force # On manager
❓ FAQ
/var/lib/docker/swarm/). These logs contain the cluster state, service definitions, Secrets, and more. If a Manager node fails, the other Manager nodes elect a new Leader using the Raft protocol. It is recommended to back up the Raft logs regularly.docker stack deploy. Important notes: ① Replace build with image (Swarm does not support build); ② volumes only support named volumes and NFS; ③ depends_on only supports started—it does not support service_healthy; ④ Add a deploy block to configure the replicas and update strategies.📖 Summary
- Swarm = Docker's built-in container orchestration: Managers handle scheduling, while Workers run containers
- Service instead of Container: Specify the number of replicas, automatic self-healing, built-in load balancing
- Live Updates:
--update-parallelism+--update-delaySet the Pace - Routing Mesh: Ports on any node are automatically routed to all replicas of a service
- Secret encrypted storage: Not exposed in environment variables; read from a file within the container
docker stack deploy -c compose.ymlCompose files can be deployed directly to Swarm
📝 Exercises
- Basic Exercise (Difficulty ⭐): Initialize Swarm on a single machine, create an Nginx service with 3 replicas, and verify that
docker service psdisplays the replica distribution. - Advanced Exercise (Difficulty ⭐⭐): Perform a rolling update (nginx: 1.25 → 1.26) and observe the update process in
docker service ps. - Challenge (Difficulty: ⭐⭐⭐): Set up a Swarm cluster on three virtual machines, deploy an API service with five replicas, simulate the failure of a worker node, and verify that the replicas automatically migrate to other nodes.



