Docker Swarm Installation

In this blog, we will Setup a 3 Node Docker Swarm Cluster

Prerequisites

3 Fresh Deployed Ubuntu servers with docker installed on them and are in the same network. Set up the hostname of each server as - master, worker1, and worker2

DNS Configuration

Update the /etc/hosts file in each machine with the IPs of all the 3 nodes so we can resolve names to IP's

Initialize the Swarm

Now we will initialize the swarm on the manager node -

root@master:~# docker swarm init
Swarm initialized: current node (buc72h0hyo66gcilq7melh9pq) is now a manager.
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5mjantlahzsaeij4l7n8716gs4ce03rk261aogw9hgn1tnlip1-2rrk2p8shojy755bgxzjz1e6t 192.168.0.123:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
root@master:~#

This command makes the host as the master node and it comes to the cluster mode. Also, in the output it has given us a command with the token to be able to add any worker node to this cluster.

Right now, only one node is there in the cluster which can be checked using below command-

root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
root@master:~#

To attach the remaining worker nodes to the master node, run below command on the worker nodes -

On worker1 -

root@worker01:~# docker swarm join --token SWMTKN-1-5mjantlahzsaeij4l7n8716gs4ce03rk261aogw9hgn1tnlip1-2rrk2p8shojy755bgxzjz1e6t 192.168.0.123:2377
This node joined a swarm as a worker.
root@worker01:~#

check docker node list again on docker swarm manager machine using below command

root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
xvdyauu8t83r3udrqh6c5w8a8     worker01   Ready     Active                          20.10.11
root@master:~#

On worker2 -

root@worker02:~# docker swarm join --token SWMTKN-1-5mjantlahzsaeij4l7n8716gs4ce03rk261aogw9hgn1tnlip1-2rrk2p8shojy755bgxzjz1e6t 192.168.0.123:2377
This node joined a swarm as a worker.
root@worker02:~#

Both the nodes has been attached in cluster-

root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
xvdyauu8t83r3udrqh6c5w8a8     worker01   Ready     Active                          20.10.11
xmq9x22q50h4qlp9311796hd9     worker02   Ready     Active                          20.10.11
root@master:~#