What is Docker Swarm?

Docker Swarm is a container orchestration tool that can help you manage multiple containers across multiple host systems by forming a 'swarm.' In this article, we'll explain what Docker Swarm is, how it works, and what benefits it offers. 

Development
May 1, 2020
Ruud van Falier

What is a Docker Swarm?

A Docker Swarm is a container orchestration tool that helps you manage multiple containers across multiple hosts. The cluster of hosts or the collection of physical machines is called a swarm. A swarm can also consist of multiple virtual machines running on a single host and pooled together into a cluster.

Once machines are clustered, you can execute Docker commands on the machines that form a cluster.

Since Docker Swarm is included as part of the Docker Engine, you can use the Docker command-line interface to create a swarm, deploy services to the swarm, and manage how the swarm behaves.

Two types of nodes in a Swarm

A node is simply an instance of the Docker engine that effectively participates in a swarm.

Typically, the operations of a cluster are managed by a swarm manager (manager node) that controls a set of machines called worker nodes.

  • Manager node: A manager node distributes, schedules, and assigns tasks to the worker nodes in a swarm. It also optionally runs services for the worker nodes. Docker recommends a maximum of seven manager nodes in a swarm. You can create a swarm with only manager nodes without a worker node, but the reverse is not possible. 
  • Worker node: A worker node receives instructions from a manager node and runs containers and services. By default, every manager node is also a worker node and can execute its tasks if it has the necessary resources.
     


How does Docker Swarm work?

Think back to the definition of a service from the YAML file we created for Docker Compose. A service is nothing but defining the executable tasks for nodes, including information such as which containers to use and which commands to execute within a container.

Each time you deploy a service to the swarm, the manager node considers your definition of the service as the desired state. It then schedules this service accordingly on each worker node in the form of similar tasks or replica tasks. Each replica task spawns a container and runs independently. For example, when you define a service instructing Docker Swarm to run three instances of a database, Docker Swarm creates three tasks.

Why Docker Swarm?

One of the reasons Docker Swarm was created is to reduce a major vulnerability provided by a single host system.

Imagine a situation where a solution is deployed on a single host and this host becomes unhealthy, leading to the failure of the entire solution. Unless you have a failover environment, you cannot use the service anymore for its intended purpose.

This is where Docker Swarm helps, as it provides a multi-node environment that allows us to distribute our services across multiple hosts. Even if one host fails, the other hosts can continue to operate, keeping the service uninterrupted.

For example, in the swarm below, if worker node 1 fails, Docker Swarm automatically reassigns the containers from node 1 to node 2.


In the above scenario, you've seen what happens when a worker node fails, but what if the manager node fails, especially if there is only one manager node? In such cases, the containers will continue to run on their respective worker nodes; however, you will need to create a separate cluster to restore the services from the failed manager node to their original state. This will result in unnecessary downtime.

To avoid downtime in the recovery process, Docker recommends implementing an odd number of manager nodes. For example, configure 3 manager nodes to tolerate the loss of one manager node without the need for downtime. Similarly, configure 7 manager nodes to tolerate 2 manager nodes, and so on.


There are several other benefits to using Docker Swarm. Docker Swarm helps leverage containers and all the advantages they offer. Similarly, load balancing is another significant benefit of using Docker Swarm. Docker Swarm schedules tasks in such a way that each container has adequate resources to operate. The swarm manager ensures that container workloads are assigned to run on the most optimal host for optimal efficiency. 

Here are some basic commands in Docker Swarm that will help you create a swarm, add nodes to it, promote a node, and leave the swarm.

To begin, to create a swarm, use the following command in Docker Desktop CLI:

docker swarm init --advertise-addr 192.168.20.12

This command instructs Docker Swarm to configure the IP address of the manager node as 192.168.20.12. This is the address through which all other nodes in the swarm can access this manager node. The response to this command will be:

Swarm initialized: current node (*******) is now a manager.

Here, ******* is a token automatically generated by Docker Swarm.

To add a worker node to this swarm, use the following command:

docker swarm join --token <token> 192.168.20.12:2377

To add a manager node to this swarm, use the following command and follow the instructions:

docker swarm join-token manager

You can also promote a worker node to a manager node using the following command:

docker node promote worker1

Here, worker1 is the name of the second host added to a swarm.

To leave the swarm, use the following command:

docker swarm leave

To deploy a service to a node, use the following command:

docker service create --name helloworld --replicas 3 partech


Here, helloworld is the name of the service and partech is the name of the image. Three replicate tasks are created for the helloworld service.

Conclusion

In this post, we have seen how Docker Swarm can be used instead of Docker Compose to manage multiple containers across multiple host systems, which is not possible with Docker Compose. Docker Swarm also offers several other advantages over Docker Compose, such as load balancing and the ability to continue running services even after a node has failed. 

Cookies
This site uses anonymous cookies. Click on "Agree" if you agree to the use of cookies, or click on "Change" to determine your preferences.
This site uses anonymous cookies.