Skip to main content

Command Palette

Search for a command to run...

Docker Swarm Project from Scratch.

Published
4 min read
Docker Swarm Project from Scratch.

Docker Swarm is a container orchestration tool that allows you to manage a cluster of Docker nodes as a single virtual system. With Docker Swarm, you can deploy and manage containers across a group of Docker hosts, and scale applications up or down as needed.

Docker Swarm provides a set of tools for managing the entire lifecycle of a containerized application, including deployment, scaling, rolling updates, and failure recovery. It enables you to define and manage services, which are collections of containers that work together to provide a specific function or application.

Docker Swarm also provides built-in load balancing and service discovery, making it easy to route traffic to the appropriate containers. It also supports high availability, so that if a node in the cluster fails, the workload can be automatically redistributed to other nodes.

Overall, Docker Swarm simplifies the process of managing containerized applications at scale, and is a popular choice for deploying and scaling Docker-based applications.

For example: If you have an important container and it crashed due to any reason, you cannot revive that container again and we cannot replicate same container in other systems, to resolve this problem Docker Swarm comes into the picture.

Let's do some practical practices on Docker Swarm.

Let's create AWS instances to understand Docker Swarm.

Note I have used 4 AWS servers just and you can use as per your requirements.

Now I will make one server Master and rest of the three as slave1, slave2, slave3.

Now, I will all my servers like below.

Master:

Slave1:

Slave2:

Slave3:

Now I will update all four servers and install Docker with the below commands.

sudo apt-get update
sudo apt-get install docker.io

Also will give access to current users on master and slaves also reboot the servers.

sudo usermod -aG docker $USER
sudo reboot

Now I will initialize Docker Swarm on my Master.

docker swarm init

Now my main server is Master.

Now If you can see the above screenshot there is a toket and asking to allow a port so now copy this ticket and allow the port.

I will go to my Master security group and will allow 2377 port here.

Now I will go to Slave1, Slave2 and Slave3 and allow the same port here too.

Note: If you have used the same security group for all four servers then if you will allow any port/IP on one server it will be by default allowed for all the servers.

Now Let's join out Slave1 as worker one . How? simple just paste the token on your slave's terminal and hit ENTER.

You will get a message as "This node joined a swarm as a worker."

Now, Let's check on the Master if all the node connected or not.

docker node ls

Here we have all three nodes.

Now I want to create a service on this cluster for a perticular app.

For Example, I am using a MYSQL

I will go to Master and run below commands.

 docker service create --name mysql-service --replicas 3 --publish 3306:3306 -e MYSQL_ROOT_PASSWORD="Test@123" mysql:5.7

Here docker service create is used to create service

--name mysql-service is name of the service

--replicas 3 is numbers of replicas of mysql

--publish 3306:3306 is allowing port for containers

-e MYSQL_ROOT_PASSWORD="Test@123" mysql:5.7 is environment and mysql version

Now if you see all the replicas are ready.

Now here Master distributed the replica in all servers.

For example, we used replica as 3 , so here Master takes one replica and the rest of the two are distributed in slaves.

Let's check on Master and Slaves.

Here is Master.

Here is Slave1

Here is Slave 2

But we do not have any replicas on Slave3 because all replicas have already been distributed.

I want to give one replicate to slave3 as well what should I do?

Easy!!!!

docker server scale mysql-service=4

Now we have one replica in Slave3

Now I want to run an application on all Master and all slaves.

I have a Django TODO App on docker hum now, I am going to run this application on my cluster.

here is the container and commands.

docker service create --name django-todo-service --replicas 4 --publish 8000:8000 trainwithshubham/django-todo-app:latest

If you see my images are ready and I have to open port 8000 to run this application.

Now, Let,s check the application on all 4 servers.

Master:

Slave1:

Slave2:

Slave3:

Now, We can run the application on port 8000.

Master IP:

Slave1 IP:

Slave2:

Slave3 :

More from this blog

Untitled Publication

22 posts