Scaling Applications with Docker Swarm: High Availability and Efficient Orchestration

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.

All my servers are like below.

Master:

Worker-1:

Worker-2:

Worker-3:

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

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

Now, check docker is running or not

systemctl status docker

Also will give access to current users on master and workers 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 token 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 worker-1, worker-2, worker-3 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 Worker-1 as worker one . How? simple just paste the token on your worker'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 are connected or not.

docker node ls

On the master node, we see that just one worker node are connected because we just use the token key on the worker-1 node. That's why one worker node are connected to the master node.

Same way we paste the token to other worker nodes and then check if all nodes are connected or not.

Now all three nodes are connected.

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

For Example, I am using an MYSQL

I will go to Master and run the 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 a service

--name mysql-service is name of the service

--replicas 3 is number of replicas of MySQL

--publish 3306:3306 is allows port for containers

--e MYSQL_ROOT_PASSWORD="Test@123"

--mysql:5.7 is the 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 replicas as 3, so here Master takes one replica and the rest of the two are distributed in workers.

Let's check on Master and Workers.

Here is Master.

Here is Worker-1:

Here is Worker-2:

Here is Worker-3

But we do not have any replicas on worker-3 because all replicas have already been distributed.

I want to give one replicate to worker-3 as well what should I do?

Easy!!!!

docker service scale mysql-service=4

Now we have one replica in worker-3

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

I have a Django todo App on docker hub 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 nahid0002/django-todo-app:latest

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

Verify Service Deployment

Run below command to verify that the service has been created and is running.

docker service ls

Check Container Status

To check the containers running on the manager node, use the below command :

docker ps

Master:

Worker-1:

Worker-2:

Worker-3:

Now, we can run the application on port 8000.

Master:

Worker-1:

Worker-2:

Worker-3:

Removing Nodes

If you need to remove any node from the swarm, run below command on the specific worker node.

docker swarm leave

This will remove the node from the swarm environment.

That's all about the most amazing topic docker swarm.

By following these steps, you can deploy your web application using Docker Swarm on AWS, ensuring a scalable and reliable production-ready environment. Docker Swarm simplifies the process of managing your application across multiple nodes, allowing for efficient scaling and high availability. Docker Swarm's capabilities empower you to confidently manage complex applications in a distributed environment, making it an indispensable tool for modern DevOps practices and seamless application deployment.

Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Medium

Github

Mail