Deploying a Web Application Using Docker Swarm

Deploying a Web Application Using Docker Swarm

Project Description

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 aims to demonstrate the benefits of Docker Swarm for deploying and managing containerized applications in production environments.

Setting Up the Environment on AWS EC2

To deploy a web application using Docker Swarm on AWS EC2, you'll prepare your environment by setting up three instances with Docker pre-installed. You can use the EC2 User Data to execute scripts upon instance launch for streamlining Docker installation.

#!/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

  • Ensure that inbound rules for all the three instances allowed Custom TCP traffic on ports 2377 and 8001 from anywhere (IPv4) within the AWS console.

  • This ensures communication between your manager nodes and worker nodes in the cluster and grants external access to your applications as necessary.

Initialize Swarm Mode on Manager Node

  • SSH into the EC2 instances assigned as the manager node i.e "Swarm-manager"

  • Run the following command to initialize Swarm mode:

docker swarm init
  • The docker swarm init command is used to initialize a Docker Swarm on a node, effectively turning it into a Swarm manager.

  • After running docker swarm init, Docker generates a join token, which you can use to add worker nodes to the Swarm.

  • Copy and run the copied token on the remaining servers to add worker nodes to the Swarm.

  • To check all the connected nodes on the manager node in a Docker Swarm cluster, you can use the following command:
docker node ls

Creating a Docker Swarm Service on the Manager Node

  • To create a service on the manager node, run the following command:
docker service create --name django-app-service --replicas 3 --publish 8001:8001 trainwithshubham/react-django-app:latest

  • Now, to list the services running within the Docker Swarm cluster, run the following command:
docker service ls

  • The docker ps command is used to list all currently running containers.
Docker ps

  • To verify that your service is running across all nodes in your Docker Swarm cluster, you can access the application by using the IP address of any node in the cluster, followed by the specified port number, here, 8001. <Public IPv4 address:8001>

  • Swarm-manager

  • Swarm-worker1

  • Swarm-worker2

Removing Nodes

  • You can remove a node from the environment if needed by using the command docker swarm leave on the respective worker node.

  • To verify if a node has been successfully removed from the Docker Swarm cluster, you can check the list of nodes using:
docker node ps

Thank you for reading