Introduction to Swarm
Imagine running a food truck. At first, one truck (host) is enough. But as demand grows, you add more trucks in different locations. Now you need a manager to coordinate them - ensuring they serve the same menu, balance workloads, and stay consistent. That manager is Docker Swarm: it orchestrates multiple Docker hosts into a single, unified cluster.
Swarm Foundations
1. What is Docker Swarm?
- Docker Swarm is Docker’s native clustering and orchestration tool.
- It turns multiple Docker hosts into a single Swarm cluster.
- Provides features like service scaling, load balancing, and fault tolerance.
2. Key Concepts in Swarm
- Node: A Docker host participating in the Swarm.
- Manager Node: Controls the cluster, schedules tasks, maintains state.
- Worker Node: Executes tasks assigned by managers.
- Service: A definition of how containers should run in the Swarm.
- Task: A single running container instance of a service.
- Overlay Network: Enables communication between containers across nodes.
3. Why Use Swarm?
- Scalability: Run containers across multiple hosts.
- High Availability: If one node fails, tasks are rescheduled on others.
- Load Balancing: Requests are distributed across replicas.
- Simplicity: Integrated into Docker, easy to set up compared to Kubernetes.
4. Swarm Workflow
- Initialize a Swarm cluster.
- Add worker nodes to the cluster.
- Deploy services to the Swarm.
- Scale services across nodes.
- Swarm manages scheduling, networking, and failover automatically.
Things to Remember
- Swarm transforms Docker from single‑host management into multi‑host orchestration.
- Manager and worker nodes form the backbone of the cluster.
- Services and tasks define how workloads are distributed.
Hands‑On Lab
Step 1: Initialize a Swarm
docker swarm init
Step 2: Add a Worker Node
On another host:
docker swarm join --token <worker_token> <manager_ip>:2377
Step 3: Deploy a Service
docker service create --name web --replicas 3 nginx
Step 4: Verify Services
docker service ls
docker service ps web
Step 5: Scale the Service
docker service scale web=5
Practice Exercise
- Initialize a Swarm cluster with one manager and one worker.
- Create a service
frontendwith 2 replicas. - Scale
frontendto 4 replicas. - Observe how tasks are distributed across nodes.
- Reflect on how Swarm ensures high availability.
Visual Learning Model
Swarm Cluster
├── Manager Node
│ └── Controls scheduling, state
└── Worker Nodes
└── Run tasks (containers)
Services → Tasks → Distributed across nodes
The Hackers Notebook
Docker Swarm is Docker’s native orchestration tool, enabling clusters of hosts to act as one system. With managers, workers, services, and tasks, Swarm provides scalability, high availability, and load balancing. It’s simple to set up and ideal for learners transitioning from single‑host Docker to distributed environments.
