๐ Deploying a Production-Ready Web App using Docker Swarm on AWS
Table of contents
๐ Introduction
Containerization has revolutionized the way applications are deployed and managed, and Docker Swarm is a powerful tool for orchestrating containers in a production environment. In this step-by-step guide, we will walk you through the process of deploying a web application using Docker Swarm on AWS. By the end of this tutorial, you will have a robust and scalable setup for your web app.
โ Solving the Problem ๐งฉ
By deploying your web app using Docker Swarm on AWS, you are addressing several key challenges:
Scalability: Docker Swarm enables you to scale your application horizontally by adding or removing nodes seamlessly. This ensures your app can handle increased traffic.
High Availability: With Swarm, your app is distributed across multiple nodes, making it highly available. If one node fails, the others can continue serving the application.
Simplified Management: Docker Swarm provides an easy-to-use interface for managing containerized applications, reducing the complexity of orchestration.
Resource Efficiency: Containers consume fewer resources compared to traditional virtual machines, optimizing resource utilization and reducing costs.
โ Step 1: Provision AWS Instances ๐
Start by logging into your AWS portal and create three new EC2 instances, each with unique names:
Swarm-manager
Swarm-worker1
Ensure that you configure the inbound rules for these instances to allow the following traffic:
Custom TCP port 2377 from anywhere (IPv4)
Custom TCP port 8001 from anywhere (IPv4)
โ Step 2: Install Docker ๐ณ
SSH into each of the two instances and install Docker with the Docker Engine. You can refer to Docker's official documentation or other resources for detailed instructions on Docker installation.
โ Step 3: Initialize Swarm on Swarm Manager ๐ค
Access the "Swarm Manager" node and initiate the Swarm by running the following command:
sudo docker swarm init
This command initializes an empty Swarm.
โ Step 4: Add Workers to the Swarm ๐๏ธ
After initializing the Swarm on the "swarm-manager" node, a key will be generated. You need to copy and run this key on the other two servers (Swarm-worker1). This step adds both machines as workers to the Swarm.
โ Step 5: Check Swarm Node Status ๐ฅ
To verify the status of all nodes in the Swarm, run the following command on the manager node:
docker node ls
This command will display information about all the nodes in the Swarm.
โ Step 6: Create a Docker Service ๐ ๏ธ
On the Swarm Manager node, create a Docker service using the following command:
sudo docker service create --name django-app-service --replicas 2 --publish 8001:8001 trainwithshubham/react-django-app:latest
This command creates a service named "django-app-service" with three replicas, publishing port 8001.
โ Step 7: List Docker Services ๐
To list the Docker services running in the Swarm, use the following command:
sudo docker service ls
This will display a list of services, including the one you just created.
โ Step 8: Verify Containers ๐
The service you created will deploy containers on the manager and worker nodes. To check if containers are running on the manager node, execute:
sudo docker ps
You should see containers related to your service.
โ Step 9: Access the Web App ๐
Now, your web app service is running on all three nodes. To access it, grab the IP address of any of the nodes followed by port 8001, like this:
http://<Any_IP_of_2_VMs>:8001
http:3.82.60.74:8001
You should be able to access your web application through this URL.
โ Step 10: Removing Nodes โป๏ธ
If you need to remove a node from the Swarm environment, run the following command on the specific worker node:
sudo docker swarm leave
As a result, the worker node will leave the Swarm, and you can confirm its status by checking the output of:
docker node ls
๐ Conclusion
Congratulations! ๐ You've successfully deployed a production-ready web app using Docker Swarm on AWS. Docker Swarm's orchestration capabilities make it easier to manage and scale your application, ensuring it runs smoothly in a production environment. Feel free to connect and follow for more informative content on containerization and cloud computing.
๐ Checkout GitHub Repository for projects:
๐ github.com/sumanprasad007