๐ณ Creating a 3-Node Docker Swarm Cluster: A Simple & Fun Guide
If you’ve ever wanted to scale your Docker applications like a pro, then Docker Swarm is your gateway! In this guide, we’ll walk you through creating your own 3-node Docker Swarm cluster โ one manager and two workers โ all from scratch!
Whether you’re running this on local VMs or cloud instances, the steps are clear and beginner-friendly. So grab your terminal and letโs swarm! ๐
๐ง What is Docker Swarm?
Docker Swarm is a built-in orchestration tool that lets you manage a cluster of Docker nodes. Think of it like forming a superhero team of machines that can deploy and scale your app together.
There are two types of nodes in Swarm:
- ๐ง Manager Node: The brain that controls and schedules tasks.
- ๐ช Worker Nodes: The muscles that actually run the containers.
๐ ๏ธ What You Need
- 3 Linux machines (can be local VMs, cloud VMs, or even Raspberry Pis).
- Docker installed on all 3 machines.
- Network access between all nodes.
- Basic terminal and SSH access.
Letโs name our nodes:
-
manager-node
โ the main control center. -
worker-node-1
โ the first worker. -
worker-node-2
โ the second worker.
โ๏ธ Step-by-Step: Create the Swarm Cluster
1๏ธโฃ Install Docker (Skip if already installed)
On all 3 machines, run:
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker
You can verify Docker is working with:
docker --version
2๏ธโฃ Initialize the Swarm on Manager
SSH into the manager node, then run:
docker swarm init --advertise-addr <MANAGER-IP>
๐ Replace <MANAGER-IP>
with the manager’s IP address (you can use ip a
to find it).
After running this, youโll get a command that looks like this:
docker swarm join --token SWMTKN-1-xxxx <MANAGER-IP>:2377
Copy this! Youโll use it to join the worker nodes.
3๏ธโฃ Join Worker Nodes to the Swarm
Now SSH into each worker node, and run the command you copied:
docker swarm join --token SWMTKN-1-xxxx <MANAGER-IP>:2377
Each worker will respond with:
This node joined a swarm as a worker.
๐ Boom! You’re now running a swarm with 1 manager and 2 workers!
4๏ธโฃ Check the Cluster Status
Go back to the manager node and run:
docker node ls
Youโll see something like:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
abcd1234 manager-node Ready Active Leader
efgh5678 worker-node-1 Ready Active
ijkl9012 worker-node-2 Ready Active
Thatโs your healthy 3-node Swarm cluster. โ
๐ Bonus: Deploy a Test Service
Letโs create a simple Nginx service across all nodes:
docker service create
--name web
--replicas 3
-p 8080:80
nginx
This will:
- Deploy 3 replicas of Nginx.
- Load-balance traffic to port 8080 across your nodes.
You can check the service status:
docker service ls
docker service ps web
Try accessing http://<any-node-ip>:8080
in your browser โ Nginx should greet you! ๐
๐งน Cleanup (Optional)
To leave the swarm:
- On workers:
docker swarm leave
- On manager (removes the whole swarm):
docker swarm leave --force
๐ฏ Conclusion
You just created a 3-node Docker Swarm cluster like a boss! This setup is perfect for learning how real-world deployments and scaling work โ and itโs just the beginning.
Now that you have your cluster:
- Try scaling services up/down.
- Deploy your own apps.
- Learn about Swarm secrets, configs, and rolling updates.
Let Docker do the heavy lifting โ your apps deserve a swarm! ๐