Prerequisites 

The base OS assumed for this installation is Ubuntu 20.04  and Docker-CE or Docker-EE installed on the system. If you don't have Docker installed please refer B1000N - Install Docker-CE .


The swarm cluster configuration of the system will look like this:


Role
IP
OS
Swarm-Master
10.10.4.44
Ubuntu 20.04
Swarm-Worker
10.10.4.48
Ubuntu 20.04


 

Setting up the cluster 

Start the cli and and ensure each node has a unique hostname. Hostnames can be set using the following command on each of the nodes as a root/ superuser e.g. on the master run:

# hostnamectl set-hostname Swarm-Master

And on the worker node:

# hostnamectl set-hostname Swarm-Worker


Start the Docker Swarm on the manager node and add the worker nodes to the cluster. Start docker swarm on manager node with:

# docker swarm init --advertise-addr 10.10.4.44

The docker swarm will initialize and a join-token created for worker nodes:
*Output

Swarm initialized: current node (fsuaqqpihi2eabmmq8gldzhpv) is now a manager.
To add a worker to this swarm, run the following command:
sudo docker swarm join --token SWMTKN-1-018kvdektwa74z8fajb5c1u6jyz6qfk4ood8u4qotw7go9jj0p-cfpnh7omy86xcgoh45vau2kaj 10.10.4.44:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
 

From the above result, copy the join-token command to add the worker nodes to the manager node:

# docker swarm join --token SWMTKN-1-13xo81gxpb3ttjh5e335pfrmz9fbnliikgfys7u8l4r8k4m575-2gsjwjs3y1i4kgeua2yu840hw 10.10.4.43:2377

If the node is successful in joining, the following output should be seen:
*Output 

This node joined a swarm as a worker.
 

In order to verify that worker nodes are added to the manager node. Run the following command from the master node:

$ docker node ls 
* Output

ID                            HOSTNAME       STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION      │
9onumsknu180gldjjjtvlxfo3 *   Swarm-Master   Ready     Active         Leader           20.10.6             │
sccm7iq745c11j883y8vsu2rg     Swarm-Worker   Ready     Active                          20.10.6             │


In order to verify the that the swarm cluster is fully functional, we will deploy our first application, in this example, an Nginx server that runs on port 80 and exposes it on port 8080 of the host by using :
$ docker service create --name web-server --publish 8080:80 nginx:1.13-alpine 

You would get an output similar to the following:
* Output
pq5txw0p9c1qcjrrl2lw3mh5p
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
 

Verify the status of created service using:

$ docker service create --name web-server --publish 8080:80 nginx:1.13-alpine 

Output:
* Output
ID NAME MODE REPLICAS IMAGE PORTS
pq5txw0p9c1q web-server replicated 1/1 nginx:1.13-alpine *:8080->80/tcp

It should now be possible to access the service from the web-browser at 10.10.4.44:8080