21 Days of Docker-Day 14 -Introduction to Docker Swarm- Part 1

What is Docker Swarm?

Docker Swarm is a clustering and scheduling tool for Docker containers. With Swarm, you can establish and manage a cluster of Docker nodes as a single virtual system.

Swarm Manager: Swarm manager purpose is to receive commands on behalf of the cluster and assign containers to Swarm nodes

Worker Node: is responsible for running container workload.

Service: To deploy your application to a swarm, we must need to submit a service definition to a manager node. Swarm manager node dispatches units of work called tasks to worker nodes.

Installing and Configuring Docker Swarm

Setup

One Swarm Manager
Two Worker Node
  • On all three servers, install Docker Community Edition.
# Update all the package listing
sudo apt-get update

# All Dependent Package
sudo apt-get -y install apt-transport-https ca-certificates \  curl gnupg-agent software-properties-common

# Download and Install GPG Key for Docker Repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Add the Docker Repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) \  stable"

# Update all the package listing based on new repository
sudo apt-get update

# Install Docker Package
sudo apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic containerd.io

NOTE: Docker Swarm comes bundled with Docker, so we just need to install Docker Package.

  • Add non-root users to the Docker group so that you can run docker commands as non-root/normal users.
sudo usermod -a -G docker <username>

NOTE: Log out each server, then log back in.

  • Verify the version of docker
$ docker version
 Client:
 Version:           18.09.5
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        e8ff056
 Built:             Thu Apr 11 04:43:57 2019
 OS/Arch:           linux/amd64
 Experimental:      false
 Server: Docker Engine - Community
 Engine:
 Version:          18.09.5
 API version:      1.39 (minimum version 1.12)
 Go version:       go1.10.8
 Git commit:       e8ff056
 Built:            Thu Apr 11 04:10:53 2019
 OS/Arch:          linux/amd64
 Experimental:     false 

Configuring the Swarm Manager

  • On the swarm manager server, initialize the swarm
docker swarm init --advertise-addr <swarm manager private IP>;
$ docker swarm init --advertise-addr 10.0.1.101
 Swarm initialized: current node (orkjv9q2gitaypqlnp93lq5dd) is now a manager.
 To add a worker to this swarm, run the following command:
 docker swarm join --token SWMTKN-1-2azpcn2q6gblghcrp1jmiqp5f4oi49jfr7g2yveezvginsodul-cykhommuu9w8vnsi6tcxe063b 10.0.1.101:2377
 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. 
  • In case if you missed this command, there are ways to retrieve it
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2azpcn2q6gblghcrp1jmiqp5f4oi49jfr7g2yveezvginsodul-cykhommuu9w8vnsi6tcxe063b 10.0.1.101:2377

Adding the Worker Node to the Cluster

  • Go to both the worker node and execute this command
$  docker swarm join --token SWMTKN-1-2azpcn2q6gblghcrp1jmiqp5f4oi49jfr7g2yveezvginsodul-cykhommuu9w8vnsi6tcxe063b 10.0.1.101:2377
This node joined a swarm as a worker.
  • Go back to the swarm manager and list all the nodes
$ docker node ls
 ID                            HOSTNAME                      STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
 w45sgx9gjpijtrq445avrd9wt *   plakhera12c.mylabserver.com   Ready               Active              Leader              19.03.4
 b5lyurguajincw1l9see0etr7     plakhera13c.mylabserver.com   Ready               Active                                  19.03.4
 x1v3x9ejsnpzgateb9xhbi431     plakhera14c.mylabserver.com   Ready               Active                                  19.03.4 
  • * in front of plakhera12c.mylabserver.com shows its a manager/Leader node