Introduction
As cloud-native applications continue to dominate the tech landscape, container orchestration tools have become essential in ensuring that applications run efficiently and at scale. Docker Swarm, a lightweight yet powerful container orchestration tool, allows developers to manage clusters of Docker containers effortlessly.
In this blog, I’ll take you through how to create a Docker Swarm cluster using AWS EC2 instances. By the end, you’ll have deployed a scalable, distributed service on a cluster of EC2 instances, all coordinated using Docker Swarm.
Why Use Docker Swarm?
With many orchestration tools like Kubernetes dominating the industry, Docker Swarm remains an excellent choice for teams seeking simplicity without compromising functionality. It’s native to Docker, which makes it straightforward to set up and manage while still offering the features you need for running containers across a cluster. Plus, pairing Docker Swarm with AWS EC2 gives you robust infrastructure to scale up or down based on your application’s demands.
Project Overview
In this project, you'll:
- Configure EC2 instances as Docker Swarm nodes (manager and workers).
- Deploy a containerized NGINX service on the Swarm cluster.
- Scale the service across multiple nodes.
- Test high availability by deploying a load-balanced service.
Benefits of Using Docker Swarm with AWS EC2
- Simple Setup
- Scaling Capabilities
- Cost Efficiency
- Built-in Load Balancing
Architecture Diagram
Architecture Diagram of a Cluster of Virtual Machines Built Using Docker Swarm
Component Breakdown
- EC2 Manager Node
- EC2 Worker Nodes
- NGINX Service
- User Data Scripts
Step-by-Step Implementation
1. Create an EC2 Instance for the Manager Node
#!/bin/bash
sudo yum update
sudo yum -y install docker
service docker start
usermod -a -G docker ec2-user
chkconfig docker on
pip3 install docker-compose
# Initialize the Swarm
docker swarm init --advertise-addr <manager-instance-ip>
2. Create Worker Nodes and Join Them to the Swarm
docker swarm join --token <swarm-join-token> <manager-instance-ip>:2377
3. Verify the Swarm Cluster
docker node ls
4. Deploy the NGINX Service
docker service create --name web_app --replicas 1 --publish 80:80 nginxdemos/hello
5. Verify the Service
Access the public IP address of any EC2 instance in the cluster.
6. Delete the Resources
docker service rm web_app
docker swarm leave --force
Challenges You Might Encounter
Network Connectivity:
Ensure that all EC2 instances are in the same VPC and that security groups allow traffic on port 2377 (for Swarm communication) and port 80 (for HTTP).Token Expiry:
The Swarm join token expires after a while. If you need to re-add a node, generate a new token with docker swarm join-token worker
Output
Successfully deployed an NGINX service across a Docker Swarm cluster and scaled the NGINX service to multiple nodes for high availability.
Conclusion
This project demonstrates the power of Docker Swarm for orchestrating containers across multiple EC2 instances on AWS. By setting up a manager node and multiple worker nodes, you’ve created a scalable and reliable environment for deploying services like NGINX. This setup can be expanded to host various microservices and production workloads, making it an excellent solution for modern cloud-based architectures.
Further Reading & Resources
Shubham Murti — Aspiring Cloud Security Engineer | Weekly Cloud Learning !!
Top comments (0)