Docker swarm is a mode in which you can run containers as clusters over multiple nodes, thereby enabling features such as load balancing, scaling, in-built orchestration and so on.
Initiating a swarm
NOTE : we will assume that we have 3 nodes (with firewall off) on the same network with docker installed and running.
- A swarm needs at-least one of the nodes to be a manager, hence we will initiate a swarm with one of the nodes in the network as ‘manager’ and advertise its ip as manager-ip.
- The other nodes can be joined to swarm as workers.
docker swarm init --advertise-addr 192.168.56.101
Swarm initialized: current node (lqljj1kqrc847x5j1h2jk49kl) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-2yrz9jl6ykmq2eyxrs1gclsvy8s48n4m04s72ge8ykiljx8t72-90ooiw8vwbmojm3hg6eemjpwk 192.168.56.101:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
The manager has provided us with a join token with which we can join other nodes as workers. This join token can always be displayed by running the following.
Display an existing worker Join-token
Run the following on Manager node.
docker swarm join-token worker
To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-2yrz9jl6ykmq2eyxrs1gclsvy8s48n4m04s72ge8ykiljx8t72-90ooiw8vwbmojm3hg6eemjpwk 192.168.56.101:2377
Adding a worker to swarm using worker Join-token
Run the following on the node that you intend to add as worker.
docker swarm join --token SWMTKN-1-2yrz9jl6ykmq2eyxrs1gclsvy8s48n4m04s72ge8ykiljx8t72-90ooiw8vwbmojm3hg6eemjpwk 192.168.56.101:2377
This node joined a swarm as a worker.
Adding additional managers to swarm
Run the following on manager node to display a manger join-token.
docker swarm join-token manager
To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-2yrz9jl6ykmq2eyxrs1gclsvy8s48n4m04s72ge8ykiljx8t72-2o82iswv10em6xe4l87cefz2a 192.168.56.101:2377
Run the following on the node that you intend to add as an additional manager.
docker swarm join --token SWMTKN-1-2yrz9jl6ykmq2eyxrs1gclsvy8s48n4m04s72ge8ykiljx8t72-2o82iswv10em6xe4l87cefz2a 192.168.56.101:2377
This node joined a swarm as a manager.
Promote a worker to manager
Run the following on manager node.
docker node promote node2
Node node2 promoted to a manager in the swarm.
Demote a manager to worker
Run the following on the manager node.
docker node promote node2
Manager node2 demoted in the swarm.
Remove a worker node from swarm
Run the following on the worker node that you intend to remove from swarm.
docker swarm leave
Node left the swarm.
NOTE : To remove a manager node from swarm, demote the manager to worker and then remove the worker from swarm. A manager node can be directly removed by adding ‘–force’ flag, however this is not recommended since this disrupts the swarm quorum.
Change the availability state of nodes
A node’s availability state can be set to active or drain. When a node is active it can be assigned tasks and when it is drained, it can no longer accept tasks. Any tasks that was assigned to a drained node will be reassigned to other active nodes.
Run the following on manager node.
docker node update --availability drain node2
node2
docker node update --availability active node2
node2
List nodes and their status
Run the following on manager node.
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION vakk1aotvkmk1uw10siophmbq node2 Ready Drain 19.03.2 c19rzvk5c0l8vw5tlqj3vn2sr node3 Ready Active 19.03.2 lqljj1kqrc847x5j1h2jk49kl * node1 Ready Active Leader 19.03.2
NOTE : When a node is removed from a swarm, its details are still stored and its status is set to ‘down’ but not removed completely. When this node is added back as a worker/manager, a duplicate entry gets added for same node with ‘Ready’ status. Now you will have two nodes with same name, but with different status ‘Down’ and ‘Ready’, causing ambiguity. Therefore it is always recommended to remove it completely and not just from the swarm, by running the following command.
docker node rm vakk1aotvkmk1uw10siophmbq
vakk1aotvkmk1uw10siophmbq
The last argument being the node-id for the node that was removed from swarm. (as in the output of ‘docker node ls’ command)
In this post, we get to know how to initiate a docker swarm and managed nodes in swarm. Next, will put the swarm to work, by deploying a service in it.