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


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.

BASH
# 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

100%
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: ⭐⭐)

BASH
# 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
BASH
# On Worker nodes: join the swarm
docker swarm join --token SWMTKN-xxx 10.0.0.1:2377

# List all nodes
docker node ls
💻 Output:

TEXT
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: ⭐⭐)

BASH
# 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: ⭐⭐)

BASH
# Scale to 5 replicas
docker service scale web=5

# Verify
docker service ps web

▶ Example: service update—rolling update (Difficulty: ⭐⭐⭐)

BASH
# 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: ⭐⭐)

BASH
# Rollback to previous version
docker service rollback web

6. Secrets and Configuration Management

▶ Example: Creating and Using Docker Secrets (Difficulty: ⭐⭐⭐)

BASH
# 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
🔒 Security: Secrets are stored in the Raft log on the Manager node (encrypted) and are sent only to the Worker running the service. They are accessed within the container via the /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.

100%
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: ⭐⭐⭐)

BASH
# 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

BASH
# ============================================
# 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

Q What is the minimum number of servers required for Swarm?
A It can run on just one server (for development/testing). For production, we recommend 3 or more Managers (an odd number, as Raft consensus requires a majority) plus N Workers. At least one Manager is required, but the cluster becomes unmanageable if a single Manager fails.
Q Where is Swarm data stored?
A In the Raft logs on the Manager nodes (/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.
Q How does Routing Mesh perform load balancing?
A Swarm’s VIP (Virtual IP) mode: Each service is assigned a virtual IP. When a request reaches the VIP, the kernel’s IPVS load balancer distributes it across the various replicas. All nodes listen on the published port, so requests can be routed to a replica on any node.
Q How do I configure TLS for Swarm?
A Swarm has built-in mTLS (mutual TLS), which automatically encrypts communication between nodes. Certificates are automatically generated during initialization, so no manual configuration is required. Communication between Managers and between Managers and Workers is encrypted.
Q What changes are needed to migrate from Compose to Swarm?
A Most docker-compose.yml files can be used as-is 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


📝 Exercises

  1. Basic Exercise (Difficulty ⭐): Initialize Swarm on a single machine, create an Nginx service with 3 replicas, and verify that docker service ps displays the replica distribution.
  2. Advanced Exercise (Difficulty ⭐⭐): Perform a rolling update (nginx: 1.25 → 1.26) and observe the update process in docker service ps.
  3. 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏