Setting Up Docker Swarm on AWS: A Step-by-Step Guide
Day 83 & 84: Docker Swarm
Introduction
Docker Swarm is a robust orchestration tool that simplifies the management and scaling of Dockerized applications. In this tutorial, we'll walk you through setting up a Docker Swarm on AWS using three EC2 instances—one serving as the Swarm Manager and the other two as Swarm Nodes. We'll also deploy a simple Django app within the Swarm, ensuring you have a scalable and efficient setup.
Prerequisites
Before we begin, make sure you have:
An AWS account with access to EC2 instances.
SSH access to your instances.
Basic knowledge of Docker.
Step 1: Launch EC2 Instances
Create Three EC2 Instances:
- Use the AWS Management Console to create three EC2 instances.
- Designate one instance as the Swarm Manager and the other two as Swarm Nodes.
- Ensure the security groups are configured to allow traffic on ports 2377 (for Swarm management) and 8001 (for the Django app).
Step 2: Install Docker Engine
Connect to Each EC2 Instance via SSH
Install Docker Engine by Running the Following Commands:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install -y 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 -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 3: Initialize Docker Swarm
- On the Swarm Manager:
sudo docker swarm init
On Both Swarm Nodes:
Copy the generated command with the token from the Swarm Manager 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 Successful Joining:
sudo docker node ls
Step 5: Deploy a Docker Service
Create a Docker Service on the Swarm Manager:
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
Verify the Service Deployment:
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
Access the Django App Using the IP Address of Any Node:
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 solid 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.
Happy Learning!
Follow me on LinkedIn.