Day 75: Project-4 (Docker Swarm)

Day 75: Project-4 (Docker Swarm)

Docker Swarm

Introduction:

The project aims to deploy a web application using Docker Swarm, a container orchestration tool that allows for easy management and scaling of containerized applications. The project will utilize Docker Swarm's production-ready features such as load balancing, rolling updates, and service discovery to ensure high availability and reliability of the web application. The project will involve creating a Dockerfile to package the application into a container and then deploying it onto a Swarm cluster. The Swarm cluster will be configured to provide automated failover, load balancing, and horizontal scaling to the application. The goal of the project is to demonstrate the benefits of Docker Swarm for deploying and managing containerized applications in production environments.


Task-01

Deploying web applications using Docker Swarm

  1. To begin, access the AWS portal and proceed to create three new EC2 instances with the following names:

    • Swarm-manager

    • Swarm-worker1

    • Swarm-worker2

Check the below screenshot:

  1. On all three instances, within the inbound rules configuration, ensure that the following rules are added:

    • Permit Custom TCP Traffic on Port 2377 from Anywhere (IPv4).

    • Permit Custom TCP Traffic on Port 8001 from Anywhere (IPv4).

Select the instance --> Security --> Security Groups --> Edit Inbound Rules --> Add Rule

Save Rules:

Do the same for the other 2 instances.

  1. On each of the three nodes, it is essential to set up Docker, including the Docker Engine. To install Docker, including the Docker Engine, on Ubuntu, you can refer to my previous blog Day 32: Docker

  2. Go to the EC2 instance- “Swarm Manager” node and run the command,

sudo docker swarm init

After initializing the swarm on the “swarm-manager” node, there will be one key generated to add other nodes into the swarm as a worker as mentioned below:

  1. Copy and run that key on the other 2 EC2 instances (i.e. Swarm-worker1 & Swarm-worker2)

Go to the worker Instances and use the following command

sudo docker swarm join --token ***token***
# Example 
# sudo docker swarm join --token SWMTKN-1-0m8fcgvbnwrvubbg6g8e668i8m1hno612s14s6395gmq9xggwe-5gpe9xjg28v47kfgp0sqlp2ig 172.31.20.147:2377

Swarm-worker1

Swarm-worker2

Both workers are now connected to manager.

  1. Go to Swarm-Manager and check all the nodes using the command
sudo docker node ls

  1. On the Manager node, create a service django-app
sudo docker service create --name django-app --replicas 3 --publish  8001:8001 trainwithshubham/react-django-app:latest

  1. Check the service using the below command
sudo docker service ls

  1. Now, this service will create a container in the Manager node. To check, run the command:
sudo docker ps

  1. The service will be running on all three nodes.

To check this, copy the <public_IP_address> of any of the nodes followed by port 8001.

Refer to the below examples:

  1. To remove any node from the environment, use the below command:
sudo docker swarm leave

From specific worker nodes

.As we removed, one of the workers and inside the status, we can see it’s down.

We have successfully created and deployed the application.


Conclusion:

In conclusion, this blog guided you through the deployment of web applications using Docker Swarm on AWS. You learned the necessary steps to set up EC2 instances, configure inbound rules, install Docker, initialize a Swarm, and create a service for your application. With Docker Swarm, you can efficiently manage and scale your applications across multiple nodes, ensuring a reliable and scalable environment for your web applications.

Hope you like my post. Don't forget to like, comment, and share.