Docker Swarm Overview
Docker Swarm contains two components:
- Secure Cluster of Docker Hosts
- Engine for Orchestrating Micro services apps.
Docker Swarm contains more than two Docker hosts which can be on dedicated physical servers, or on VM, or on cloud instances and are able to communicate to each other over reliable network.
Now these Docker nodes can be configured as Manager or workers. Docker manager manages the control plane of cluster and also dispatches tasks to workers. All the configuration and state of swarm is located distributed etcd file located on all Managers.
Workers are accepts tasks from Managers and execute them.
TLS protocol is used between Managers and Workers to integrate and communicates each other. It is TLS who encrypt communication, authenticates nodes, and authorize roles.
Docker Swarm overview is shown below:
While creating the Swarm clusters, you need the following ports to be open on routers and Firewalls:
- 2377/tcp: For Secure Client to Swarm communication
- 7946/tcp and 7946/udp: For control plane communication
- 4789/udp: for VXLAN-based overlay networks
The basic steps to create Docker Swarm is given below:
- Initialize first Manager Node
- Join Additional Manager Node
- Join Workers Node
Initialize Docker Swarm:
Step1: Check all working and active container and Stop them
Step2: Create two Clone your Docker Host and rename it, as per given figure below:
Step3: Now Make Docker Node 1 as Manager by docker swarm init command. Once you do, it will create docker swarm join token to add workers, just copy it in another notepad. Now copy and Paste this token command to rest of two docker host. And verify Docker host on Node1.
Step4: Once Command is properly executes, you will see three Docker Swarm Node, one is manager and rest two has been successfully registered as Workers.
Installing App or containers to execute an application is called as Docker Services. Let see how a service runs on Docker Swarm.
Run below command on Swarm Node 1 and you will see the application is executed on container. Once done test it on browser by using Docker Host IP along with its Container Mapped Port.
Swarm Manager High Availability:
Swarm Manager supports High Availability, which means when one Manager Node fails, and the rest Swarm Manager Node keeps Swarm Node function available.
Swarm implements Active-passive Multi Manager HA, in which one is active and all other are passive in nature, it is the Active Manager or leader who gives all commands and issue task to workers. If in any case Passive receives any command and instructions for swarm, it proxies them across to leader.
Below Figure describes all steps about Swarm HA:
In continuation to this HA, Following best practice.
- Deploy an odd number of Managers.
- Don’t deploy too many Managers (3 or 5 recommended)
Let’s suppose you have 4 Swarm Manager and in case of network partitioned situation, you will left with 2 Swarm Node Manager on each side. When this situation reaches it is said as Split Brain. Due to this neither side will know who are alive on another side, and which side has majority (Quorum). Due to this split brain situation, you will not be able to make any configuration and create and add Swarm Node.
But this above Problem can be solved when we use 3 or 5 Managers Node, as even after Network Partition, one side will have Majority and achieve quorum. Below Figure describe the facts discussed above.
Once Docker Swarm is created, you can either make replicas of your applications or run each replicas on each cluster Worker node.
The above method is called as scaling a services and can be configured by Scale command. In blow figure we are scaling two more replicas of webserver, here we see that two web server replicas are created and is spanned between Node, Node1 and Node 3.
Let’s see by creating three replicas, with same method, and verify the result.
Verify it by using Docker Host IP address of each Host along with its container port.
Now shut down one Node, you will see that one of the replica will move to Node 1, and this can be verified by docker service ps service name.