Day 83 : Project 4 - Web Application Deployment Using Docker Swarm

In the modern era of software development, deploying and managing applications efficiently is crucial for maintaining high availability and reliability. Docker Swarm, a native clustering and orchestration tool for Docker, offers a powerful solution for container orchestration. This blog post will guide you through deploying a web application on Docker Swarm, utilizing its features such as load balancing, rolling updates, and service discovery.

Project Description

In this project, we will explore the deployment of a web application using Docker Swarm, a powerful container orchestration tool. Docker Swarm enables the seamless management and scaling of containerized applications, making it an ideal choice for production environments. Leveraging Docker Swarm’s features, such as load balancing, rolling updates, and service discovery, we aim to achieve high availability and reliability for our web application.

The key steps involved in this project include pulling an existing Docker image from DockerHub and deploying it onto a Docker Swarm cluster. By setting up the Swarm cluster, we will benefit from automated failover, load balancing, and horizontal scaling capabilities, ensuring our application can efficiently handle varying workloads and demands.

Let’s dive into the project and learn how Docker Swarm can enhance the deployment and management of containerized applications.

Hands-on Project: Web Application Deployment Using Docker Swarm

In this hands-on project, we will guide you through the process of deploying a web application using Docker Swarm, a powerful container orchestration tool. Before we begin, make sure you have a Docker image of your web application stored on DockerHub.

Pre-requisites:

To start, access the AWS portal and set up three new instances with Docker pre-installed. Use the following shell script and provide it in the EC2 User Data field:

#!/bin/bash

echo "Docker installation"
echo "------------------------------------------------------------------"
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
sudo reboot

Servers renamed.

Step 1: Set Up Docker Swarm Cluster

The first step is to set up a Docker Swarm cluster. If you haven’t already, create a Docker Swarm cluster by initializing it on a manager node. Use the following command on the manager node:

docker swarm init

We can see Docker Swarm uses port 2377, so we’ll open this port in our Security Group.

Once the swarm is initialized on the “swarm-manager” node, a key will be generated to add additional nodes as workers to the swarm. Copy and execute this key on the remaining servers to join them to the swarm.

We will run this command on both worker1 and worker2 to add them to the swarm cluster.

docker-swarm-worker1

docker-swarm-worker2

Run docker node ls command to view information about nodes in the swarm cluster - 3 nodes in the swam cluster ( 1 manager node and 2 worker nodes)

docker node ls

Step 2: Deploy the Web Application

With the Docker Swarm cluster ready, it’s time to deploy your web application using the Docker image stored on DockerHub. Use the following command to deploy the application as a service on the Swarm:

docker service create --name django-notes-app --replicas 3 -p 8000:8000 prathmeshdockerr/django-notes-app:latest

Step 3: Verify the Deployment

Once the service is deployed, you can verify its status and check if the desired number of replicas are running on the Swarm nodes. Use the following commands to see the running services:

docker service ls

docker ps

Now, the service is running on all three nodes. To verify this, navigate to http://<ec2-instance-public-ip-address>:8000 using every instance IP Address.

  • swarm-manager

  • swarm-worker1

  • swarm-worker2

Step 4: Clean-up

When you are done with the project, you can remove the service and leave the Swarm. To remove the service, use the following command from any worker node:

docker swarm leave

After removing one of the workers, we can observe its status as “down” within the status information.

To leave the Swarm, run the following command on the manager node:

docker swarm leave --force

Conclusion

Project-4 demonstrates the power and flexibility of Docker Swarm for deploying and managing containerized web applications in a production environment. By leveraging Docker Swarm's features like load balancing, rolling updates, and service discovery, you can ensure high availability, reliability, and scalability for your applications. This hands-on project not only highlights the technical steps but also showcases the practical benefits of using Docker Swarm in real-world scenarios.

Embrace Docker Swarm to streamline your application deployment and management processes, ensuring a robust and scalable infrastructure!

I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .

thank you : )

0
Subscribe to my newsletter

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

Written by

Prathmesh Vibhute
Prathmesh Vibhute