DEV Community

Cover image for Creating a Docker Swarm Cluster on AWS EC2 : AWS Project
Shubham Murti
Shubham Murti

Posted on

Creating a Docker Swarm Cluster on AWS EC2 : AWS Project

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:

  1. Configure EC2 instances as Docker Swarm nodes (manager and workers).
  2. Deploy a containerized NGINX service on the Swarm cluster.
  3. Scale the service across multiple nodes.
  4. 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

Image description

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>
Enter fullscreen mode Exit fullscreen mode

2. Create Worker Nodes and Join Them to the Swarm

docker swarm join --token <swarm-join-token> <manager-instance-ip>:2377
Enter fullscreen mode Exit fullscreen mode

3. Verify the Swarm Cluster

docker node ls
Enter fullscreen mode Exit fullscreen mode

4. Deploy the NGINX Service

docker service create --name web_app --replicas 1 --publish 80:80 nginxdemos/hello
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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

Let’s connect: Linkdin, Twitter, Github

Top comments (0)