.png?type=webp)
Docker Swarm: Guide to Getting Started
28 November, 2022
0
0
0
Contributors
Docker swarm is a native clustering tool for Docker that turns a pool of Docker hosts into a single, virtual host. With swarm, administrators and developers can easily manage a cluster of Docker hosts as a single entity, allowing them to centrally deploy and manage applications and containers. Additionally, swarm makes it easy to scale your applications and containers by adding or removing hosts from your cluster.
In this tutorial, you will learn the fundamentals of Docker Swarm and how you can start working with it. In order to follow this tutorial you must have docker installed in your machine and also you should be aware of basic docker concepts and commands.
Docker Swarm: Overview
Docker Swarm is a orchestration tool like Kubernetes, it is used to distribute the workload of the containers to multiple nodes. Docker Swarm follows Manager/Worker approach in order to achieve this.
The Manager Node in a Swarm is responsible to maintain Worker Nodes, it is also responsible for upscaling or downscaling the worker nodes. It is recommended to have multiple manager nodes in your Swarm, however, only a single manager is allow to work on worker nodes.
Since Docker Swarm works with multiple different node, you cannot work with it using Docker Desktop, you can either initialize it in GCP or AWS, or you could also use Docker Playground to access it. For this tutorial I will be using Docker Playground in order to create Swarm instances.
Initializing Docker Swarm
To start Docker Swarm, open your terminal and type the following command:
Running the above command will produce the following output:
In the above output, you can see that docker provided commands which should be run in order to add worker and manager nodes to your current swarm.
The --advertise-addr command is the IP that other node should use in order to connect with this manager and --listen-addr is the IP that the current manager should listen to.
To confirm that the node is created, you can run docker node ls command which produces the following output:
In the above output you can see that there is only one node created with Leader Status which means this node can make decisions about adding the new worker node or discarding any node from the Swarm.
Add Worker Nodes to the Swarm
In order to add worker node, run the following command:
The above command produces following output:
The output return the command which should be run in order to create a worker node in this Swarm. Now to create a new worker node, you first need to create a new Instance in the Docker Playground. You can do it by pressing Add New Instance button at the left pane as shown in the below image.
After creating the new instance run the above join command to make this instance as a worker node.
After executing the above command, Docker return a confirmation message saying This node joined as a worker .
Now run the docker node ls command to view all the created nodes. Running this command should produce the following output:
Now repeat the above process of Creating a New Instance and Adding it to the Swarm as a worker one more time. After doing this, the output of docker node ls command should return the following.
Adding Manager Nodes
The manager node in a Docker Swarm can be added in a similar way as Worker Nodes. To add Manager Node run the following command in node1 which is the current Leader in your Swarm.
The above command returns the command which should be executed in order to create manager nodes in your Swarm.
Now copy the above command and create two new managers. After doing this your docker node ls output should like this:
Conclusion
In this tutorial you learned about the Swarm Mode in Docker and how to get started with it. Swarm mode is meant to keep the application highly available by orchestrating all the Managers and Workers. There is so much more to Docker Swarm, in this tutorial you just went over the basics.
In the upcoming blogs I intend to share my learning on more Advanced stuff about DevOps, Python and Django. If it interests you then consider following me.
python
docker
devops
container