Docker Swarm

Amitt AshokAmitt Ashok
3 min read

Docker is a free package management tool which helps us in package management, Docker is a platform designed to help us build, share, and run container applications.

Docker Swarm is a feature of Docker that allows you to set up a cluster consisting of one manager and multiple worker nodes. These nodes work together in a tightly coupled manner to meet the service requirements.

Docker swarm gives us different features as

--> Deployment

--> Scaling

--> Resource allocation

--> Load balancing

--> Health Monitoring

Now we will see how docker swarm works and how to use it.

  1. Create one node/system as master/manager

    docker swarm init

For understanding purposes, I am creating 4 instances on AWS as swarm workers.

Swarm manager:- 172-31-34-90 is the IP address of the manager node

ubuntu@ip-172-31-34-90:~$ docker swarm init
Swarm initialized: current node (va2uul58txk146aboh0wrdlz9) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2v6eh96i1pui7kiypxvjitvf0sr18331ln6srx95i72in8whgs-7e988wvdua2lwwqm9lmf3rn3k 172.31.34.90:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Swarm worker 1:- -172-31-33-90

ubuntu@ip-172-31-33-90:~$ docker swarm join --token SWMTKN-1-2v6eh96i1pui7kiypxvjitvf0sr18331ln6srx95i72in8whgs-7e988wvdua2lwwqm9lmf3rn3k 172.31.34.90:2377
This node joined a swarm as a worker.
ubuntu@ip-172-31-33-90:~$

Swarm worker2:- 172-31-45-118

ubuntu@ip-172-31-45-118:~$ docker swarm join --token SWMTKN-1-2v6eh96i1pui7kiypxvjitvf0sr18331ln6srx95i72in8whgs-7e988wvdua2lwwqm9lmf3rn3k 172.31.34.90:2377
This node joined a swarm as a worker.
ubuntu@ip-172-31-45-118:~$

Swarm woker3:- 172-31-34-64

ubuntu@ip-172-31-34-64:~$ docker swarm join --token SWMTKN-1-2v6eh96i1pui7kiypxvjitvf0sr18331ln6srx95i72in8whgs-7e988wvdua2lwwqm9lmf3rn3k 172.31.34.90:2377
This node joined a swarm as a worker.
ubuntu@ip-172-31-34-64:~$
  1. Docker Node details

    docker node ls

     ID                            HOSTNAME           STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
     jlr9d7g7b01a6b6nmzqj0horj     ip-172-31-33-90    Ready     Active                          24.0.7
     yknnlgi3eha40rl9re9ufwfy2     ip-172-31-34-64    Ready     Active                          24.0.7
     va2uul58txk146aboh0wrdlz9 *   ip-172-31-34-90    Ready     Active         Leader           24.0.7
     c9ogu8mj702nsf1g47ev98ut1     ip-172-31-45-118   Ready     Active                          24.0.7
    
  2. Create docker service

    docker service create --name mysql-ser --replicas 6 -e MYSQL_ROOT_PASSWORD=abc@123 --publish 3306:3306 mysql:5.7

     ubuntu@ip-172-31-34-90:~$ docker service create --name mysql-ser --replicas 6 -e MYSQL_ROOT_PASSWORD=abc@123 --publish 3306:3306 mysql:5.7
     rf9k878fziq8wbttxco591igg
     overall progress: 6 out of 6 tasks 
     1/6: running   [==================================================>] 
     2/6: running   [==================================================>] 
     3/6: running   [==================================================>] 
     4/6: running   [==================================================>] 
     5/6: running   [==================================================>] 
     6/6: running   [==================================================>] 
     verify: Service converged
    

    We created 6 replicas of the MySQL service, and they were distributed across 4 nodes.

     ubuntu@ip-172-31-34-90:~$ docker ps
     CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
     3075ba5f92c8   mysql:5.7   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   3306/tcp, 33060/tcp   mysql-ser.2.t6eimv95e31hxux065fmn2tae
     ubuntu@ip-172-31-34-90:~$ 
     SWARM MANAGER
    
     ubuntu@ip-172-31-33-90:~$ docker ps
     CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
     a5c37fc07a71   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   mysql-ser.3.wj6jdtcgnjps4aexu0uksjft3
     ubuntu@ip-172-31-33-90:~$ 
     WORKER 1
    
     ubuntu@ip-172-31-45-118:~$ docker ps
     CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
     318b6860df5c   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   mysql-ser.6.uw4cb6cavbpaj8nerf4gv8adc
     39a025f1f807   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   mysql-ser.4.sx1rkoxz86mdco8kudwkilz5b
     WORKER 2
    
     ubuntu@ip-172-31-34-64:~$ docker ps
     CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                 NAMES
     1b55fe40bcd2   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   mysql-ser.1.jybhw825yy20li11ruvdd77th
     ec9585249600   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   3306/tcp, 33060/tcp   mysql-ser.5.wwnwxwjixwys94m2kjl8vuxyf
     WORKER 3
    

    Currently, there are 6 replicas of the MySQL server working, we can check by

     ubuntu@ip-172-31-34-90:~$ docker service ls
     ID             NAME        MODE         REPLICAS   IMAGE       PORTS
     rf9k878fziq8   mysql-ser   replicated   6/6        mysql:5.7   *:3306->3306/tcp
    
  1. Scale down the service

    docker service scale mysql-ser=5

     docker service scale mysql-ser=5
     mysql-ser scaled to 5
     overall progress: 5 out of 5 tasks 
     1/5: running   [==================================================>] 
     2/5: running   [==================================================>] 
     3/5: running   [==================================================>] 
     4/5: running   [==================================================>] 
     5/5: running   [==================================================>] 
     verify: Service converged
    

Today, I learned about Docker Swarm and practised by setting up a Docker cluster with one node as the manager and the others as workers.

Thank You.....

0
Subscribe to my newsletter

Read articles from Amitt Ashok directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amitt Ashok
Amitt Ashok

Hey Hi there, Amit here... I love to explore new things and learn new technology.