Introduction to Docker Swarm
Chapter objectives
- Understand container orchestration
- Discover Docker Swarm and its concepts
- Compare Swarm with other orchestrators
- Understand the Swarm architecture
1 - What is orchestration?
The problem
In production, managing containers manually quickly becomes impossible:
Défis de la production
┌────────────────────────────────────────────────────────────┐
│ │
│ ❓ Comment déployer sur plusieurs serveurs ? │
│ ❓ Comment gérer les pannes de serveurs ? │
│ ❓ Comment mettre à l'échelle automatiquement ? │
│ ❓ Comment router le trafic vers les conteneurs ? │
│ ❓ Comment mettre à jour sans interruption ? │
│ │
└────────────────────────────────────────────────────────────┘
The solution: orchestration
An orchestrator automates:
| Function | Description |
|---|---|
| Scheduling | Placement of containers on nodes |
| Scaling | Adjustment of the number of replicas |
| Networking | Communication between containers |
| Load Balancing | Traffic distribution |
| Health Checks | Monitoring and restart |
| Rolling Updates | Updates without interruption |
2 - What is Docker Swarm?
Definition
Docker Swarm is Docker's native orchestration mode. It turns a group of Docker hosts into a single "virtual host".
┌─────────────────────────────────────────────────────────────────┐
│ Docker Swarm Cluster │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Manager │ │ Manager │ │ Manager │ │
│ │ (Leader) │───│ (Reachable)│───│ (Reachable)│ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │
│ ┌──────┴─────────────────┴─────────────────┴──────┐ │
│ │ Raft Consensus │ │
│ └──────┬─────────────────┬─────────────────┬── ────┘ │
│ │ │ │ │
│ ┌──────▼──────┐ ┌──────▼──────┐ ┌──────▼──────┐ │
│ │ Worker │ │ Worker │ │ Worker │ │
│ │ Node │ │ Node │ │ Node │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
Main characteristics
| Characteristic | Description |
|---|---|
| Native | Integrated into Docker Engine |
| Simple | No additional installation |
| Declarative | Define the desired state |
| Secure | TLS by default |
| Scalable | From 1 to thousands of nodes |
3 - Fundamental concepts
Nodes
A node is a Docker Engine instance in the cluster.
Manager Nodes:
- Manage the cluster state
- Schedule tasks
- Serve the Swarm API
- Use the Raft consensus
Worker Nodes:
- Run the containers
- Receive tasks from managers
- Report the state of tasks
┌───── ────────────────────────────────────────────────────────┐
│ Manager Node │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌──────────────────┐ │
│ │ API Server │ │ Scheduler │ │ Orchestrator │ │
│ └─────────────┘ └─────────────┘ └──────────────────┘ │
│ ┌─────────────┐ ┌─────────────┐ ┌──────────────────┐ │
│ │ Raft │ │ Dispatcher │ │ Worker (opt.) │ │
│ └─────────────┘ └─────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Worker Node │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────────────────────────────┐ │
│ │ Agent │ │ Container Runtime │ │
│ └─────────────┘ └─────────────────────────────────────┘ │
└───────────────────────────────────────────────────────────── ┘
Services
A service is the definition of an application to run on the Swarm.
# Créer un service
docker service create --name web --replicas 3 -p 80:80 nginx
Types of services:
| Type | Description | Example |
|---|---|---|
| Replicated | N distributed replicas | Web servers |
| Global | 1 instance per node | Monitoring agents |
Tasks
A task is the unit of work in Swarm = 1 container.
Service "web" (3 replicas)
│
├── Task 1 → Conteneur (Node 1)
├── Task 2 → Conteneur (Node 2)
└── Task 3 → Conteneur (Node 3)
Stacks
A stack is a group of related services (the Docker Compose equivalent for Swarm).
# stack.yml
version: "3.9"
services:
web:
image: nginx
deploy:
replicas: 3
db:
image: postgres
4 - Swarm vs Kubernetes
Comparison
| Aspect | Docker Swarm | Kubernetes |
|---|---|---|
| Installation | Integrated | Complex |
| Learning curve | Gentle | Steep |
| Scalability | Thousands of nodes | Very large scale |
| Ecosystem | Limited | Very rich |
| Auto-healing | Basic | Advanced |
| Configuration | Docker Compose | Specific YAML |
| Community | Small | Very large |
When to use Swarm?
✅ Use Swarm if:
- You already know Docker
- You need a simple solution
- Your infrastructure is medium-sized
- You already use Docker Compose
❌ Prefer Kubernetes if:
- You need advanced features
- You manage very large infrastructures
- You need a rich ecosystem
- You are in a multi-cloud environment
5 - Detailed architecture
Internal components
┌─────────────────────────────────────────────────────────────┐
│ Swarm Manager │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Cluster Store │ │
│ │ (Distributed State - Raft) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────┼──────────────────┐ │
│ │ │ │ │
│ ┌──────▼──────┐ ┌──────▼──────┐ ┌───────▼─────┐ │
│ │ Orchestrator│ │ Allocator │ │ Scheduler │ │
│ │ (état désiré│ │ (IP, ports) │ │ (placement) │ │
│ │ vs actuel) │ │ │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │
│ ┌───────▼───────┐ │
│ │ Dispatcher │ │
│ │ (assign tasks)│ │
│ └───────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Raft Consensus
The Raft consensus ensures that all managers share the same state:
┌─────────┐
│ Leader │ ◄── Reçoit les commandes
└────┬────┘
│ Réplique
┌────────┼────────┐
▼ ▼ ▼
┌───────┐ ┌───────┐ ┌───────┐
│Follower│ │Follower│ │Follower│
└───────┘ └───────┘ └───────┘
Quorum = (N/2) + 1 managers
3 managers → quorum = 2
5 managers → quorum = 3
Recommendations for managers
| Number of managers | Fault tolerance | Recommendation |
|---|---|---|
| 1 | 0 | Development only |
| 3 | 1 | Minimal production |
| 5 | 2 | Standard production |
| 7 | 3 | Large production |
Important
More than 7 managers is not recommended because it slows down the consensus.
6 - Ports and communication
Required ports
| Port | Protocol | Use |
|---|---|---|
| 2377/tcp | TCP | Cluster management communication |
| 7946/tcp+udp | TCP/UDP | Communication between nodes |
| 4789/udp | UDP | Overlay network traffic |
Firewall configuration
# Sur tous les nœuds
sudo ufw allow 2377/tcp
sudo ufw allow 7946/tcp
sudo ufw allow 7946/udp
sudo ufw allow 4789/udp
7 - Advantages of Docker Swarm
Simplicity
# Initialiser un cluster : 1 commande
docker swarm init
# Rejoindre le cluster : 1 commande
docker swarm join --token <token> <ip>:2377
# Déployer un service : 1 commande
docker service create --name web nginx
Native integration
- Uses the same Docker images
- Compatible with Docker Compose (with adaptations)
- Same CLI (
dockerwith subcommands) - Same concepts (images, containers, networks, volumes)
Security by default
- Automatic mutual TLS
- Automatic certificate rotation
- Control plane traffic encryption
- Native secret management
Summary
| Concept | Description |
|---|---|
| Swarm | Docker's native orchestration mode |
| Node | Machine in the cluster (Manager or Worker) |
| Service | Definition of an application to deploy |
| Task | Container instance of a service |
| Stack | Group of services (multi-service) |
Key points
- Docker Swarm is simple and integrated into Docker
- Managers manage the state, workers run the tasks
- The Raft consensus ensures consistency between managers
- Recommended: 3 or 5 managers for production
Practical exercises
- Explain the difference between a manager and a worker
- Why have an odd number of managers?
- What is a replicated service vs a global one?
- Which ports must be opened for Swarm?