DEV Community

SOVANNARO
SOVANNARO

Posted on

🐳 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
Enter fullscreen mode Exit fullscreen mode

You can verify Docker is working with:

docker --version
Enter fullscreen mode Exit fullscreen mode

2️⃣ Initialize the Swarm on Manager

SSH into the manager node, then run:

docker swarm init --advertise-addr <MANAGER-IP>
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Each worker will respond with:

This node joined a swarm as a worker.
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ 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
Enter fullscreen mode Exit fullscreen mode

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         
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • On manager (removes the whole swarm):
  docker swarm leave --force
Enter fullscreen mode Exit fullscreen mode

🎯 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)