What is Docker Swarm?
Docker swarm is a mode of handling a cluster of Docker Engines, hence the name Swarm. The cluster of Docker hosts run in swarm mode consisting of managers and workers. The docker-engine instances which participate in the swarm are called nodes. A production level swarm deployment consists of docker nodes spread across multiple servers.
Why use it? – Container Orchestration
When you are working in a production environment, 100s of docker containers will be running multiple applications in it. Managing all these containers can be a big pain for all the DevOps engineers; this is where Docker Swarm helps you out. It manages and orchestrates the cluster running multiple docker containers with ease. Below are some of its features:
High-Availability – aims to offer no downtime or outage. Load Balancing – allocate the resources and requests on other nodes in the cluster automatically if any node fails. De-centralized – multiple manager nodes run in a production environment; hence the cluster is never dependent on a single manager node. Scalability – using a single docker swarm command, you can easily scale-up or scale-down containers in the cluster.
Orchestrate Docker Containers
Now that you know the basics of Docker Swarm let us look at an example of its implementation. In this example, I have three machines running in a cluster with the below details: To initialize the swarm mode in docker, run the below command on the manager node. The flag –advertise-addr is used for advertising itself to the nodes which can join the cluster. The above command will generate a token which will be used by other nodes to join this cluster. Copy the command with the token generated and run it on the worker nodes. Running the token on worker1 node. Running the token on worker2 node. Now, on the manager node, you can check which nodes are running in the cluster. Let’s build the geekflare_mongodb docker image which we used in Dockerfile Tutorial. Run a container of the MongoDB docker image by creating a swarm service. 27017 is the port number on which MongoDB is exposed. Check if the docker swarm service has started. MODE replicated means the container has been replicated on all the nodes in the cluster and REPLICAS 1/1 means only one swarm service is currently running. Let us check on which node in the cluster this single service is running. It is running on manager1 node. Run docker ps command to get more details about the container which is running this swarm service. You can run the swarm service in “global” mode also instead of default “replicated” mode. Global mode runs one task of the swarm service on all the nodes in the cluster. Before I run the service in global mode, let me remove the existing running container. Start the swarm service inside a docker container in global mode using –mode flag. Check if the swarm service started in global mode. Since, three nodes (1 manager, 2 workers) are running in the cluster, that’s why the number of replicas is 3. 3 services are running now across 3 nodes, check it by running the below command. Next, let me show how you can define the number of replicas. Before that, I will remove the current container, which is running. Use –replicas flag in the command and mention the number of replicas you want of the swarm service. For example, I want to have two replicas of the swarm service: Check the swarm services currently running. You can see one replica is running on manager1 node and the other one on worker1 node. Go to worker1 node and check if the docker container is running the swarm service. To stop this container, run the command below. Now from manager1 node if you check which all nodes are running the service, you will see its running on manager1 node and worker2 node. The CURRENT STATE of worker1 node is Shutdown (as we stopped the container running the service). But since two replicas must run of this service, another service was started on worker 2. This is how you achieve high availability using docker swarm. It’s very easy to scale up or down docker containers. The command below will scale up the mongo container to 5. Check how many replicas of mongo container is running now, it must be 5. Check where these 5 replicas are running in the cluster. 1 replica is running on manager1 node and 2 replicas on both the worker nodes each. In your cluster, if you don’t want your services to run on manager node(s), and want to keep it only for managing the nodes, you can drain the manager node out. Check the availability of the manager node. You will see the services are not running on the manager node anymore; they are spread across the worker nodes in the cluster. That was all about Docker Swarm and how to orchestrate containers in docker swarm mode. Try out these on your non-production environment to get an idea of how it works.