Skip to main content

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:

FunctionDescription
SchedulingPlacement of containers on nodes
ScalingAdjustment of the number of replicas
NetworkingCommunication between containers
Load BalancingTraffic distribution
Health ChecksMonitoring and restart
Rolling UpdatesUpdates 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

CharacteristicDescription
NativeIntegrated into Docker Engine
SimpleNo additional installation
DeclarativeDefine the desired state
SecureTLS by default
ScalableFrom 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:

TypeDescriptionExample
ReplicatedN distributed replicasWeb servers
Global1 instance per nodeMonitoring 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

AspectDocker SwarmKubernetes
InstallationIntegratedComplex
Learning curveGentleSteep
ScalabilityThousands of nodesVery large scale
EcosystemLimitedVery rich
Auto-healingBasicAdvanced
ConfigurationDocker ComposeSpecific YAML
CommunitySmallVery 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 managersFault toleranceRecommendation
10Development only
31Minimal production
52Standard production
73Large production
Important

More than 7 managers is not recommended because it slows down the consensus.


6 - Ports and communication

Required ports

PortProtocolUse
2377/tcpTCPCluster management communication
7946/tcp+udpTCP/UDPCommunication between nodes
4789/udpUDPOverlay 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 (docker with subcommands)
  • Same concepts (images, containers, networks, volumes)

Security by default

  • Automatic mutual TLS
  • Automatic certificate rotation
  • Control plane traffic encryption
  • Native secret management

Summary

ConceptDescription
SwarmDocker's native orchestration mode
NodeMachine in the cluster (Manager or Worker)
ServiceDefinition of an application to deploy
TaskContainer instance of a service
StackGroup 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

  1. Explain the difference between a manager and a worker
  2. Why have an odd number of managers?
  3. What is a replicated service vs a global one?
  4. Which ports must be opened for Swarm?

← Table of contents | Installation and cluster →