Docker Swarm is a clustering and orchestration tool for managing Docker containers. It provides native clustering capability for Docker, allowing you to create and manage a swarm of Docker nodes. The nodes can be physical servers or virtual machines.
Docker Swarm can be deployed on any Linux system, making it a platform-independent tool. It is an excellent tool for managing large-scale containerized applications and is widely used in production environments.
Pre-requisites
- Linux system with pre-installed Docker
Why use Docker Swarm?
Docker Swarm is designed to manage containerized applications at scale. It provides a unified interface for managing a cluster of Docker nodes, making it easy to deploy and manage containerized applications. Docker Swarm can be used for various applications, including web applications, micro-services, and big data processing.
You're probably wondering, "Why not Kubernetes?" It's because Kubernetes adds cost and management overhead. It is not feasible for small teams that don't need the entire Kubernetes package but only require some like scaling, healing, rollbacks, etc.
Setup Docker Swarm Cluster
Let's start deploying an application via Docker Swarm. In the following sections, we'll walk through the steps required to deploy a container image using Docker Swarm. I suppose you have Docker installed in your system and a basic understanding of Docker and containerization.
For this demo, we'll use two virtual machines. One will act as a manager node, and the other as a worker node.
The IP of my manager node is 192.168.1.2
, and the worker node is 192.168.1.3
. Note that your IP address will be different from the ones we're using.
Ensure that the following ports are opened in both nodes for communication.
In the manager node, run the following command.
docker swarm init --advertise-addr 192.168.1.2
You will now be presented with a token which you can use to connect the worker node with the manager node.
docker swarm join --token SWMTKN-1-5pfyqhf9zolbudgkxqmmrivx2ebfz9wqyvxs1ntv494h9096ww-anpgsk82gwtyy78xg8wajhbkz 192.168.1.2:2377
docker swarm init
command.Run the command below to check if our worker node has successfully joined the swarm.
docker info | grep -w -A 5 Swarm
On the manager node, run the command below to get information about nodes that have joined the specific swarm cluster.
docker node ls
Deploy Application to Docker Swarm
Services in Docker Swarm are like deployments in Kubernetes. It deploys the image and takes care of everything. The service name can be used to query all kinds of details of the application.
docker service create --name webserver_app --replicas 1 --publish published=8080,target=80,mode=host nginx:latest
docker login
before entering the above command. Also, add the --with-registry-auth
flag to provide the registry auth to the worker node during deployment only.As I have taken an Nginx web server image to create the service, we can simply access it by entering the worker node's IP address.
Access the application in a web browser by entering the following URL
The above command displays the default page of the Nginx web server, as shown below.
Using the given command, you can check which nodes the workload is running on.
docker service ps webserver_app
Advantages of Docker Swarm
Scalability
Docker Swarm provides a scalable platform for deploying containerized applications. It can easily handle thousands of nodes and containers, making it an ideal choice for large-scale applications.
Easy-to-Use
It is easy to use, with a simple and intuitive user interface. This makes managing and deploying containers effortless, even for developers new to Docker.
High availability
It provides high availability for containerized applications and automatically detects and recovers from node and container failures. This assures you about the status of your applications – Always up and running!
Security
Docker Swarm provides built-in security features, such as TLS encryption and access controls, protecting your applications and data from unauthorized access.
Cost-effective
By running multiple containers on a single machine, Docker Swarm can help reduce infrastructure costs. This optimizes resource utilization and reduces the need for additional hardware.
Disadvantages of Docker Swarm
Complex setup process
Docker Swarm can be difficult to set up and manage, especially for large-scale applications. It requires a good understanding of Docker, containerization concepts, networking, and security.
Limited features
Docker Swarm is a relatively new tool with fewer features than some more established container orchestration tools like Kubernetes.
Learning curve
If you're new to Docker and containerization, a steep learning curve can be associated with using Docker Swarm. It requires a good understanding of the Docker ecosystem and the principles of containerization.
Conclusion
In this article, we discussed Docker Swarm, Docker Swarm cluster set up and deployed a web server application to our Docker Swarm cluster. Next, we'll explore the concept of services in depth.
Thank you for reading! Please comment below if you have any queries. I try to update my articles periodically to ensure legibility.