Day 83: Setting up Docker Swarm on AWS with a Django App

Day 83: Setting up Docker Swarm on AWS with a Django App

Introduction:

Docker Swarm is a powerful orchestration tool that allows you to manage and scale your Dockerized applications. In this step-by-step tutorial, we will guide you through the process of setting up a Docker Swarm on AWS using three EC2 instances, with one serving as the Swarm Manager and the other two as Swarm Nodes. Additionally, we will deploy a simple Django app within the Swarm.

Prerequisites:

Before getting started, ensure that you have:

  1. AWS account with access to EC2 instances.

  2. SSH access to your instances.

  3. Basic knowledge of Docker.

Step 1: Launch EC2 Instances

Create three EC2 instances using the AWS Management Console. One instance will be designated as the Swarm Manager, while the remaining two will be Swarm Nodes. Ensure that security groups are configured to allow traffic on ports 2377 and 8001.

Step 2: Install Docker Engine

Connect to each EC2 instance via SSH and install Docker Engine by running the following commands:

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

#Install the Docker packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 3: Initialize Docker Swarm

On the Swarm Manager, initialize Docker Swarm using the command:

sudo docker swarm init

Copy the generated command with the token and run it on both Swarm Nodes to join them to the Swarm as workers.

Step 4: Verify Swarm Nodes

List all nodes in the Swarm to ensure that both Nodes have successfully joined the Swarm:

sudo docker node ls

Step 5: Deploy a Docker Service

Create a Docker service on the Swarm Manager by running:

sudo docker service create --name django-app-service --replicas 3 --publish 8001:8001 trainwithshubham/react-django-app:latest

This command deploys a Django app with three replicas and exposes port 8001.

Step 6: Check Service Status

To verify the service deployment, run:

sudo docker service ls

Step 7: Verify Containers

Check that the containers are running on the Swarm Manager:

sudo docker ps

Step 8: Access the Django App

The Django app should be accessible on any of the three nodes. Grab the IP address of any node and access the app using:

http://<Any_ip_of_3_vms>:8001

Conclusion:

Congratulations! You have successfully set up a Docker Swarm on AWS with a Django app running in a distributed environment. This tutorial provides a foundation for deploying and managing scalable applications using Docker Swarm. Feel free to explore additional Docker Swarm features and customize the deployment according to your application's requirements.

Follow me on LinkedIn.

Checkout my GitHub profile.