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! π
Top comments (0)