Docker Swarm

Updated: Jun 17, 2020

Docker Swarm is a container orchestration tool from Docker and it is Docker’s answer to K8s. It is a set of machines, can be either physical or virtual, configured and joined together to form a cluster thereby enabling the user to manage single or multiple containers deployed across a number machines in the cluster. Docker swarm enables faster deployments and can be scaled up or down on demand.

When deploying multiple containers in a single go, YAML files are used, for example when deploying a LAMP stack where we need to run Apache, MySQL, PHP all in Linux environment this can be achieved by using YAML file then Docker compose deploys the stack.

Working with Docker Swarm

Docker Swarm is achieved by configured and joining a group of machines either physical or virtual. A machine can join in the swarm as either a Manager Node or as a Worker Node. There can be multiple Manager nodes and Worker Nodes in a swarm. The swarm is effective until at least there is one Manager node left running in the swarm. A worker node can be promoted to a Manager node likewise a Manager node can be demoted to Worker node in a swarm. By default, a Manager node functions as both a Manager node as well as a Worker node.

There can be a maximum of seven Manager Nodes in a swarm. One of the Manager node is assigned as a Leader node which manages swarm and orchestrates all the Tasks in the swarm. In case a Leader node is unreachable or is down, one among the other Manager nodes is elected as leader node by Swarm algorithm.

A worker node receives instruction and tasks from the Manager node and performs the tasks with the available resources.

Scaling is achieved in swarm by declaring the number of tasks that you want to run. The Swarm manager is responsible for increasing or decreasing the number of tasks on each host machine based on Automatic Load Balancing algorithm.

Scaling is achieved in swarm by declaring the number of tasks that you want to run. The Swarm manager is responsible for increasing or decreasing the number of tasks on each host machine based on Automatic Load Balancing algorithm.

Automatic Load Balancing

Host machines in the Swarm are allotted applications based on the optimal utilization of the available resources. This process is called as the Automatic Load Balancing.

High Availability

When a container fails, the swarm manager detects that event and automatically replaces the container. In case a node fails or it is unreachable, the swarm manager automatically replaces all the containers to another running node thus maintaining the number of instances to run.

Rolling updates and Rollback

Rolling updates in Docker swarm is achieved by applying incremental service updates to all nodes. The time spacing or the delay in applying the update can be controlled by the user. In case something goes south, you can rollback to previous state.

Difference between Docker Swarm and K8s

In Docker swarm, applications are deployed as services and micro services whereas in K8s the same is achieved by using the combination of Pods, Nodes, Deployments and Services. Docker Swarm is preferred over K8s when the deployment needs to be simple.

Let us setup a high performance cluster (HPC) using Docker Swarm

The below are the steps that we will follow to create our first HPC

1. Create three virtual machines running docker viz., Manager1, Node1, Node2

2. Initiate the swarm from Manager node and join other machines as Worker nodes

3. Create a service; Scale up and down

4. Update, rollout and rollback the deployed service

5. Stop and remove service

Creating Virtual Machines

We will be using Docker-machine for win 10 64 bit for creating Virtual machines. Installing docker-machine and docker engine is out of the scope of this article and we will assume it is already installed and ready. So, we will step right in. Virtual machines can be created as below

Docker-machine create -d virtualbox Manager1

Docker-machine create -d virtualbox Node1

Docker-machine create -d virtualbox Node2

The above commands create three virtual machines named Manager1, Node1, Node2 each with specification 1 core processor and 1GB RAM, enough to run Docker engine and client and Docker Swarm running tinycorelinux a small sized variant of linux. Creating a virtual machine takes time based on system configuration, wait till the machine is created

To view the IP number of each machine individually

Docker-machine ip manager1

Docker-machine ip Node1

Docker-machine ip Node2

To see all the virtual machines at the same time

Docker-machine ls

SSH or Login separately into the newly created machine

Docker-machine ssh manager1

Docker-machine ssh node1

Docker-machine ssh node2

Login into the VM can be confirmed by the change in the command prompt.

Run the following commands both before Swarm initialization and after and note down the changes

Docker node ls

This command lists the nodes available in the swarm

Docker service ls

This command lists the services available in the swarm

Docker info

This command displays information about docker engine and docker client running in the host machine. The section that we are interested in is swarm which says it is inactive.

Swarm initialization

We will initiate the swarm, from the Manager node and running the following commands

Docker swarm init --advertise-addr <Manager node ip>

Docker swarm init --advertise-addr

--advertise-addr flag instructs the swarm that what comes next is the ip address of the manager node

This command initiates the swarm gives the token to add another machine as worker. The join token is copied and saved and you can use the command by running it in worker machine to join the swarm.

Docker info

This command can be run in all machines. After joining the swarm, in the swarm section, we have one manager and one worker.

The following commands may be used to get Join-token for manager and worker nodes

Docker swarm join-token manager

Docker swarm join-token worker

Joining the swarm

Copy and paste join-token to join as a worker or as a manager. We can check, whether the join was successful or not by running

Docker info

To check how many machines are connected in the swarm. This command can only be run in a manager machine.

Docker node ls

To check how many services are running in the swarm. This command can only be run in a manager machine.

Docker service ls

Since we have not created any service so far, the result is none

Creating a service

Docker service create --name=<my-service-name> <image>

Docker service create -p 80:80 --name=myservicenginx nginx

This will create a service named myservicenginx with port 80 exposed and running nginx web server

Scaling up and down

Docker service scale <my-service-name>=nos

Scaling up

Docker service scale myservicenginx=3

This will create three containers running one container in each of the hosts machines, maintaining equal load on all host machines

Docker service ls

Docker service scale myservicenginx=6

This will create six containers running two container in each of the hosts machines, maintaining equal load on all host machines

Scaling down

Docker service scale myservicenginx=2

This will create three containers running one container in two machines

Docker service scale myservicenginx=0

This will stop all container in all machines but the service still running

Service update and rollback

Docker service update --image=<new-image-name> <my-service-name>

Docker service update --image=httpd myservicenginx

This will replace incrementally all running nginx container in the service <myservicenginx> to httpd one container at a time.

Docker service ls

Docker service rollback <my-service-name>

Docker service rollback myservicenginx

This will rollback all containers incrementally to the previous running container

Docker service ls

Stopping the service

Docker service scale myservicenginx=0

This command only stops and removes running container leaving the service still running. To completely stop and remove a service

Docker service rm <my-service-name1> <my-service-name2> etc

Docker service rm myservicenginx

kindly provide your valuable feedback on this topic.

145 views1 comment

Recent Posts

See All


  • Pinterest
  • Facebook
  • LinkedIn

© 2020 by techyfella.

Subscribe for techyfella updates!