1. Introduction
Docker Swarm is a native clustering and orchestration tool for Docker. It allows users to manage and orchestrate a group of Docker nodes (containers or hosts) as a single virtual system. With Docker Swarm, you can deploy, manage, and scale containerized applications across a cluster of Docker nodes.
2. Key Features of Docker Swarm
- Cluster Management: Swarm enables the grouping of multiple Docker hosts into a cluster, referred to as a Swarm, which can be managed as a single entity.
- Decentralized Design: In a Swarm, managers and workers share the workload. Managers handle orchestration tasks, while workers execute containers. The system is designed to handle failovers by distributing tasks across nodes.
- Service Deployment: It enables users to deploy containerized services across multiple nodes. You can define how services should run, and Swarm ensures the containers are deployed, restarted, or updated as per your specifications.
- Load Balancing: Swarm includes built-in load balancing. Requests coming to the service are distributed evenly across all running containers in the Swarm, improving resource utilization and performance.
- Scalability: Services can be easily scaled up or down. If a particular service needs more capacity, you can scale the number of running containers with a single command.
- High Availability: In a Swarm, if a node fails, the tasks running on that node are reassigned to other nodes in the cluster to ensure high availability.
- Rolling Updates: Docker Swarm supports rolling updates, allowing users to update services without downtime by gradually replacing containers with the new version.
- Swarm uses TLS to encrypt communications, authenticate nodes, and authorize roles.
3. Docker Swarm vs Kubernetes
Docker Swarm and Kubernetes are both tools for orchestrating containers, but they differ in complexity and features:
Docker Swarm is simpler and tightly integrated with Docker, offering easy setup and basic features like service discovery, load balancing, and scaling. It’s ideal for small to medium deployments and teams already using Docker.
Kubernetes is more powerful and complex, designed for large-scale, production-grade environments. It provides advanced features like auto-scaling, persistent storage, and multi-cluster management, making it more suitable for large, distributed systems requiring high scalability and flexibility.
Swarm is easier to use, while Kubernetes offers greater control and functionality.
4. Docker Swarm
From a clustering perspective, a swarm is made up of one or more Docker nodes. These nodes can be physical machines, virtual machines, Raspberry Pi devices, or cloud instances. The main requirement is that each node has Docker installed and can communicate over a stable network connection. A node in a Docker Swarm is either configured as a manager or a worker. Managers look after the state of the cluster and dispatching tasks to the workers. Workers execute the tasks received from the managers.
A Service in Docker Swarm is a higher-level abstraction that defines how Docker containers are deployed and managed across a swarm cluster. It allows you to specify the desired state for your application, such as how many replicas of a container should run. Docker Swarm automatically manages load balancing, scaling, and placement of containers on available nodes. Services can be updated, scaled, and monitored easily, enabling seamless management of containerized applications in a swarm environment.
5. Create a Docker Swarm cluster
The swarm’s configuration and state are stored in a distributed database that is replicated across all manager nodes. This data is kept in-memory and remains highly current. The best part is that no configuration is needed — it’s automatically set up as part of the swarm and manages itself effortlessly.
We’ll now create a Docker Swarm. We’ll use ‘Play With Docker’ to create a Docker Swarm. We’ll create 2 managers and 3 workers.
Step 1:
Login to the ‘Play With Docker’ and create 5 instances (nodes).
Step 2:
Go to the Node 1 terminal and run ‘ifconfig’ command. Check the ‘eth0’ in the result and get the ipaddress. Running the ‘docker swarm init’ on a Docker host will make that host a manager.
$ docker swarm init --advertise-addr 192.168.0.28:2377 --listen-addr 0.0.0.0:2377 Swarm initialized: current node (gp1mkf21gbw6f255e3dbg8m12) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-1ada22l9i48eftxnzz70fa38vuyn4ojhbd9fxdas7r7ly2pz8r-6n8lh5mu395xxv4jnsk8jmaly 192.168.0.28:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Run the ‘docker node ls’ command to check the nodes in swarm.
$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION gp1mkf21gbw6f255e3dbg8m12 * node1 Ready Active Leader 24.0.7
From node1 run the docker swarm join-token
command to extract the commands and tokens required to add new workers and managers to the swarm.
$ docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-1ada22l9i48eftxnzz70fa38vuyn4ojhbd9fxdas7r7ly2pz8r-6n8lh5mu395xxv4jnsk8jmaly 192.168.0.28:2377 $ docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-1ada22l9i48eftxnzz70fa38vuyn4ojhbd9fxdas7r7ly2pz8r-9ypub4lgonttknzayuzwetsxr 192.168.0.28:2377 $ docker swarm join --token SWMTKN-1-1ada22l9i48eftxnzz70fa38vuyn4ojhbd9fxdas7r7ly2pz8r-9ypub4lgonttknzayuzwetsxr 192.168.0.28:2377 This node joined a swarm as a manager.
After you have executed commands on nodes to add nodes as managers and workers, run docker node ls
to check the managers and workers.
$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION gp1mkf21gbw6f255e3dbg8m12 node1 Ready Active Leader 24.0.7 ybt3ptieix2lwx7l3zbr1b0of * node2 Ready Active Reachable 24.0.7 wvj3k8zca2fzfq8rdmrsczqoq node3 Ready Active 24.0.7 l0xduomy4rlzlafnqitzia6d4 node4 Ready Active 24.0.7 clwj5vnr0weu2j14ll058m6w6 node5 Ready Active 24.0.7
The added manager can be seen as Leader
or Reachable
.
The docker swarm init
can be explained as:
docker swarm init
: This command instructs Docker to create a new swarm, designating the current node as the first manager. It also switches the node into swarm mode.--advertise-addr
: This flag specifies the swarm API endpoint that will be shared with other managers and workers. Typically, it is one of the node’s IP addresses but can also be the address of an external load balancer. While optional, it becomes necessary if you need to define a load balancer or choose a particular IP on a node with multiple addresses.--listen-addr
: This is the IP address the node will use to receive swarm traffic. If not specified, it defaults to the same value as--advertise-addr
. However, if--advertise-addr
is set to a load balancer, you must use--listen-addr
to define a local IP or network interface for handling swarm traffic.
Swarm mode uses port 2377 by default, though this can be changed. However, it is standard practice to use 2377/tcp for secure (HTTPS) client-to-swarm communications.
6. Conclusion
In this tutorial, we explored the fundamentals of Docker Swarm, a native orchestration tool for managing containerized applications across multiple Docker nodes. We covered how to initialize a swarm, add nodes, create and manage services, and use important flags such as --advertise-addr
and --listen-addr
. Docker Swarm makes it easy to scale applications and maintain high availability with built-in security features like TLS encryption.
By mastering Swarm, you can efficiently manage distributed applications, simplifying both development and production deployments. It’s a powerful tool for anyone looking to manage containers at scale with minimal overhead.