Module 12 Lesson 2: Introduction to Docker Swarm
Orchestration without the headache. Learn how to turn multiple Docker hosts into a single cluster using Docker Swarm Mode.
Module 12 Lesson 2: Introduction to Docker Swarm
Docker Swarm is Docker’s built-in orchestration tool. It’s powerful enough for most production needs but uses the same commands and logic you already know from the individual Docker CLI.
1. Key Concepts: Managers and Workers
A Swarm cluster is made of two types of nodes (servers):
- Managers: The "Brain." They maintain the cluster state, handle scheduling, and serve the HTTP API. (You should have an odd number, like 3 or 5, for reliability).
- Workers: The "Muscle." Their only job is to run the containers (tasks) assigned to them by the manager.
2. Bootstrapping the Cluster
Creating a cluster takes only two commands.
-
On Server A (The Manager):
docker swarm init --advertise-addr <SERVER_A_IP>Docker will give you a "Token" (a long string of text).
-
On Server B and C (The Workers):
docker swarm join --token <TOKEN> <SERVER_A_IP>:2377
Result: You now have a 3-node cluster! You can manage all of them from the manager node.
3. Why Choose Swarm Over Kubernetes?
- Zero Installation: It’s already in your Docker binary.
- Ease of Use: If you know
docker run, you already know 90% ofdocker service. - Low Resource Overhead: Swarm use very little RAM/CPU compared to Kubernetes.
- Excellent for SMBs: If you are managing 5 to 50 containers, Kubernetes is often "Overkill." Swarm is the "Just Right" solution.
4. Declarative vs. Imperative
- Imperative: "Docker, run this one container right now."
- Declarative (Swarm): "Docker, I want 5 copies of this app running at all times. You figure out where and how."
The orchestrator’s job is to continually move the system towards your "Declared" state.
Exercise: Creating a Virtual Cluster
- On your laptop, run
docker swarm init. You are now a cluster of one! - Run
docker node ls. You should see your computer as the "Leader." - Now, try to run a "Service" instead of a container:
docker service create --replicas 3 --name my-web nginx - Run
docker service ps my-web. Note the "Node" column. Since you only have one node, all three are running on your laptop. - What would happen if you had 3 servers? Where would those 3 Nginx containers be?
- How would you "Leave" the swarm cluster? (Research:
docker swarm leave).
Summary
Docker Swarm is the "Gateway Drug" to orchestration. It provides the essential features of high availability and rolling updates without the massive complexity of Kubernetes. For many teams, it is the perfect production environment.
Next Lesson: Managing the stack: Services, stacks, and nodes in Swarm.