Get Ac­quainted with Docker Swarm

The use of Docker con­tainer­i­sa­tion has be­come quite com­mon, es­pe­cially in pro­duc­tion. Docker Swarm is a clus­ter­ing and sched­ul­ing tool for Docker con­tain­ers. With the help of Swarm, sysad­mins can de­ploy and man­age a clus­ter of Docker nodes as a sin­gle vir

OpenSource For You - - Contents - By: Neetesh Mehro­tra The au­thor works at NIIT Tech­nolo­gies as a se­nior test en­gi­neer. His ar­eas of in­ter­est are Java de­vel­op­ment and au­to­ma­tion test­ing. He can be con­tacted at mehro­[email protected]

Docker is a tool that’s in­tended to make the process of cre­at­ing, de­ploy­ing and run­ning ap­pli­ca­tions eas­ier by us­ing con­tainer based vir­tu­al­i­sa­tion tech­nol­ogy. Docker is open source con­tainer tech­nol­ogy that pro­vi­sions far more apps run­ning on the same old servers com­pared to tra­di­tional VMs.

The Docker en­gine

The Docker en­gine is the core com­po­nent re­spon­si­ble for cre­at­ing Docker im­ages and run­ning them as ser­vices.

The core com­po­nents of the Docker en­gine are:

The Docker dae­mon

This is a con­tin­u­ous run­ning pro­gram (dae­mon) that man­ages the ser­vice and other Docker ob­jects tied to it.


This spec­i­fies in­ter­faces that pro­grams can use to speak to the dae­mon and di­rects what it should do.

The Docker client

This is used to in­ter­act with the dae­mon (Docker com­mand).

Docker net­work­ing

Docker net­work­ing comes into the pic­ture in large scale, real-time sce­nar­ios. Docker net­work­ing helps us to share data across var­i­ous con­tain­ers.

The host and con­tain­ers in Docker are linked on the ba­sis of a 1:N re­la­tion­ship, which means one host can com­mand mul­ti­ple con­tain­ers.

Clus­ter man­age­ment

Be­fore we get into the de­tails of Docker Swarm, let us un­der­stand some of the ba­sic con­cepts of a clus­ter.

Clus­ter man­age­ment is a tech­nique to man­age one or more clus­ter nodes. This is done us­ing a clus­ter man­ager and agent. A clus­ter man­ager is noth­ing but a GUI or CLI soft­ware.

The clus­ter­ing tool

A clus­ter­ing tool is soft­ware that man­ages a set of re­sources through a sin­gle point of com­mand. In our case, these sets of re­sources are noth­ing but con­tain­ers. For ex­am­ple, work­load dis­tri­bu­tion man­age­ment across a dis­trib­uted sys­tem/clus­ter is very te­dious for large en­ter­prise sys­tems. The clus­ter­ing tool eases mat­ters by au­tomat­ing this task. The in­struc­tor will just spec­ify the de­tails such as the clus­ter’s size, set­tings and some ad­vanced fea­tures. Every­thing else is taken care of by the clus­ter­ing tool. Docker Swarm is one such ex­am­ple of a clus­ter­ing tool for con­tain­ers.

The need for a con­tainer orches­tra­tion sys­tem

Con­tainer man­age­ment be­comes very tough in case of a large scale dis­trib­uted sys­tem which in­volves hun­dreds of con­tain­ers. A few of the key ac­tiv­i­ties are:

Scaling the num­ber of con­tain­ers based on the peak load Per­form­ing rolling up­dates for con­tain­ers

Per­form­ing health checks on con­tain­ers

Docker Swarm is a clus­ter man­age­ment and orches­tra­tion tool which is in­built in the Docker en­gine. A Swarm is a clus­ter of Docker en­gines, or nodes, with which you de­ploy ser­vices.

You can build this us­ing the Swar­mKit (a tool­kit to or­ches­trate the sys­tem). Swarm mode can be en­abled by ei­ther ini­tial­is­ing a Swarm or join­ing an ex­ist­ing Swarm.

How Docker Swarm evolved

In early 2014, Docker de­vel­oped a clus­ter man­age­ment sys­tem with a com­mu­ni­ca­tion pro­to­col known as Beam.

Later, with the Docker API, a dae­mon was in­tro­duced to com­mu­ni­cate with a dis­trib­uted sys­tem. This was named Lib­swarm. The dae­mon is called Swarmd.

In Novem­ber 2014, the Docker team re­tained the con­cept of clus­ter com­mu­ni­ca­tion with ad­di­tional re­mote APIs and named this Swarm. This first gen­er­a­tion was called Swarm v1.

In Fe­bru­ary 2016, Swarm v1 was com­pletely re­designed to over­come cer­tain lim­i­ta­tions and was named Swarm v2. In June 2016, Swar­mKit, which is an orches­tra­tion tool­kit within the Docker en­gine for dis­trib­uted ser­vice, came into be­ing. This ver­sion is also known as ‘Swarm mode’.

Fea­tures of Docker Swarm

The fea­tures of Docker Swarm are:

De­cen­tralised de­sign: You can man­age the nodes in the Swarm clus­ter through Swarm com­mands. This gives a sin­gle point of ac­cess to build the en­tire Swarm.

Scaling: You can spec­ify the num­ber of tasks for ev­ery ser­vice. This is au­to­mat­i­cally done through com­mands to scale the ser­vice.

De­sired state rec­on­cil­i­a­tion: The Swarm man­ager keeps track of the clus­ter state so that the ac­tual and the de­sired state is al­ways the same.

Multi-host net­work­ing: When you spec­ify an over­lay net­work to con­nect your ser­vices, the Swarm man­ager as­signs ad­dresses to the con­tain­ers on the over­lay net­work once you cre­ate/up­date the con­tain­ers.

Ser­vice dis­cov­ery: Swarm man­ager nodes al­lo­cate a unique DNS name for the ser­vices. You will be able to find con­tain­ers in the swarm through the DNS server em­bed­ded in the swarm.

Load bal­anc­ing: Swar­mKit has an in­ter­nal load bal­ancer which dis­trib­utes the ser­vice con­tain­ers within nodes. You can in­clude an ex­ter­nal load bal­ancer as well.

Be­fore we dive into the ar­chi­tec­ture of Swarm, let’s look at some of the key con­cepts in Docker Swarm. Docker node

Docker ser­vice

Docker tasks

Docker Node

Docker Node is a Docker en­gine in­stance that is in­cluded in Docker Swarm. In real-time, these nodes are dis­trib­uted across mul­ti­ple clouds as well as phys­i­cal ma­chines.

There are two kinds of Docker nodes:

Man­ager node

Worker node

Man­ager node: The man­ager node is re­spon­si­ble for all the orches­tra­tion and con­tainer man­age­ment ac­tiv­i­ties re­quired to keep up the de­sired sys­tem state.

Worker node: The worker node ex­e­cutes the tasks as­signed by the man­ager node.

Docker ser­vice

Ser­vice is noth­ing but a task def­i­ni­tion that has to be ex­e­cuted. You will have to cre­ate a ser­vice spec­i­fy­ing the im­age name and other ad­di­tional pa­ram­e­ters. In most cases, ser­vice is an im­age for a mi­croser­vice of some large ap­pli­ca­tion. An ex­am­ple would be an HTTP server or a data­base, any kind of ex­e­cutable pro­gram that you would run in a dis­trib­uted sys­tem, etc.


The node is the key mem­ber of a Docker Swarm. A Swarm can have more than one man­ager node. In this case, the nodes elect their leader us­ing the Raft al­go­rithm to con­duct the orches­tra­tion tasks.

Man­ager nodes are also worker nodes. But you can con­fig­ure them as man­ager-only nodes, thereby re­strict­ing them from work­ing on any other tasks.

The Docker Swarm API is an­other key com­po­nent of the Docker Swarm ar­chi­tec­ture.

Swarm has a list of re­mote APIs sim­i­lar to Docker. This en­ables all kinds of Docker clients to con­nect to Swarm. In ad­di­tion, the Swarm API pro­vides the clus­ter in­for­ma­tion as well.

Docker API in­for­ma­tion is re­stricted to one sin­gle

Docker en­gine, whereas the Docker Swarm API pro­vides the in­for­ma­tion on the clus­ter of en­gines, the num­ber of nodes avail­able in a clus­ter, and the node de­tails.

Raft Con­sen­sus

Man­ager nodes use the Raft Con­sen­sus Al­go­rithm to in­ter­nally man­age the clus­ter state.

This is to en­sure that all man­ager nodes that are sched­ul­ing and con­trol­ling tasks in the clus­ter main­tain/store a con­sis­tent state.

Docker Swarm: Com­mands

Let us now get ac­quainted with Docker Swarm com­mands. To ini­tialise Swarm, use the fol­low­ing com­mand:

docker swarm init

Note: You can con­fig­ure the man­ager node to pub­lish its ad­dress as a man­ager with the men­tioned IP ad­dress, as shown be­low:

docker swarm init --ad­ver­tise-addr <ip-ad­dress> For Docker Swarm Help, use the fol­low­ing com­mand: docker swarm --help

The above com­mand lists the ba­sic Swarm com­mands with their us­age de­tails.

To list the num­ber of nodes cur­rently avail­able in the Swarm, use the fol­low­ing com­mand:

docker node ls

De­ploy­ing a ser­vice with con­straints

You can add your own con­straints while de­ploy­ing a ser­vice. This will en­sure that the ser­vice will get sched­uled as a task only when the con­straint is met. This con­straint is known as place­ment.

In the fol­low­ing ex­am­ple, you add a con­straint to de­ploy a ser­vice only on nodes where there is SSD stor­age.

docker ser­vice cre­ate --name testSer­vice --repli­cas 2 --con­straint­bels.disk==ssd tom­cat

Fig­ure 2: Docker Swarm ar­chi­tec­ture

Fig­ure 1: The Docker net­work

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.