As a DevOps engineer, I have worked extensively with Docker Swarm. While Kubernetes is often the go-to choice for container orchestration, Docker Swarm offers a simpler and more lightweight alternative for managing containerized applications. In this blog, I'll share my insights on Docker Swarm, along with real-world examples to help you understand its capabilities and use cases.
What is Docker Swarm?
Docker Swarm is a native clustering and orchestration tool for Docker containers. It enables you to manage a group of Docker nodes as a single virtual system, simplifying container deployments and scaling. Swarm mode is built into Docker, making it an attractive choice for teams already using Docker. We have to deploy the application on cluster.
What is Cluster?
In Docker Swarm, a cluster is a group of multiple Docker nodes that work together as a single system to run containerized applications.
A Swarm cluster consists of:
Manager Nodes – Control and manage the cluster and services.
Worker Nodes – Run the application containers.
The manager node assigns tasks to worker nodes, ensuring high availability and scalability. This allows applications to run smoothly even if some nodes fail.
How to create a cluster in Docker Swarm?
Let’s walk through a real-world scenario where we deploy a multi-node Docker Swarm cluster.
Launch 3 EC2 Instances (1 Master, 2 Worker Nodes).
Lets install docker on each node
yum install docker -y && systemctl start docker
Step 1: Initialize the Swarm on Master
docker swarm init
This command initializes Swarm mode and generates a token. If we copy the token and paste it on worker node, then the worker node will join in the docker swarm cluster.
worker-1:
worker-2:
Step 2: Verify the Cluster
On the manager node, check the status of the cluster:
docker node ls
You should see a list of all nodes in the cluster.
Deploying a Service in Docker Swarm
Let’s deploy a simple application across the cluster.
docker service create --name mustafa --replicas 3 -p 8081:80 shaikmustafa/dm
This command:
Creates a service named
mustafa
Runs 3 replicas (instances) of the mustafa container
Exposes port 8081 on all nodes
OUPUT FROM MANAGER NODE:
OUPUT FROM WORKER-1 NODE:
OUPUT FROM WORKER-2 NODE:
Checking Service Status
To check if the service is running, use:
docker service ls
To see details of running tasks (containers), use:
docker service ps mustafa
Scaling Services in Docker Swarm
Scaling is effortless in Swarm. If traffic increases, you can scale up with:
docker service scale mustafa=5
This increases the Nginx service replicas to 5. Swarm automatically distributes them across available nodes.
Now lets check the number of containers, previously we have 3 containers now we scale up to 5, it will show 5 containers for your services
docker service ps mustafa
Rolling Updates in Docker Swarm
Imagine you need to update your application to a newer version. Instead of taking everything down, Swarm allows rolling updates:
sudo docker service update --image shaikmustafa/cycle mustafa
Swarm updates containers one at a time, ensuring zero downtime.
Now the output changes to another application with same url
OUPUT FROM MANAGER NODE:
OUPUT FROM WORKER-1 NODE:
OUPUT FROM WORKER-12 NODE:
Rollback in Docker Swarm
docker service rollback mustafa
Now check the output, you can see the old output in browser
OUPUT FROM MANAGER NODE:
OUPUT FROM WORKER-1 NODE:
OUPUT FROM WORKER-2 NODE:
Remove a Service in Docker Swarm
docker service rm mustafa
When we remove a service, it will delete the containers also
Cluster Maintenance in Docker Swarm
Till now we maintained applications on Docker swarm, now let me know you how to maintain cluster in docker swarm. As we know cluster is nothing but a group of servers. It has master node and worker nodes. In the above example we have 3 nodes (1 master & 2 worker nodes). If you want to add or delete a node from cluster here are the commands to follow
To remove a node from cluster, go to that worker node and perform the below command
docker swarm leave
After 10 seconds, this node went to Down state in cluster.
docker node ls
Now remove the node from the cluster
docker node rm <node-id>
Now see we have have only 2 Nodes
-
If you want to add a worker node to our cluster, we need o get a worker node token
docker swarm join-token worker
Now copy the token and paste it on any docker host. It will join as a worker node in your cluster
If you want to add a manager node to our cluster, we need o get a worker node token
docker swarm join-token mnager
Now copy the token and paste it on any docker host. It will join as a manager node in your cluster
Conclusion
Docker Swarm is a powerful yet simple tool for container orchestration. If you need a lightweight alternative to Kubernetes for managing containers, Swarm is a great choice.
If you're just starting, I recommend setting up a small cluster and experimenting with deployments. Understanding Docker Swarm will give you a solid foundation in container orchestration and help you scale applications effortlessly.
Give me your heart 💖
If you found this blog helpful for your interviews or in learning Docker troubleshooting, please hit a heart for 10 times and drop a comment! Your support motivates me to create more content on DevOps and related topics. ❤️
If you'd like to connect or discuss more on this topic, feel free to reach out on LinkedIn.
Linkedin: linkedin.com/in/musta-shaik