Docker Series โ€” Part 16: Docker Swarm Setup on AWS EC2 (Master-Slave Architecture)

Nitin DhimanNitin Dhiman
3 min read

When you're running applications inside Docker containers, it's easy to forget that everything still depends on the host machine's health. What if your OS crashes? What if you're limited by the CPU/RAM on that single system? That's where Docker Swarm and horizontal scaling come into play.

In this part of the series, we dive into launching a Docker Swarm cluster using Amazon EC2 instances, with a focus on master-slave setup and service orchestration.

Why This Matters

Containers are great, but by default, they run on a single host. This creates a Single Point of Failure (SPOF).

To ensure resilience and scalability:

  • Vertical Scaling adds more resources (RAM/CPU) to one system (but has limitations and possible downtime).

  • Horizontal Scaling adds more systems and distributes the load (ideal for high availability).

Docker Swarm allows us to scale horizontally by forming a cluster of Docker hosts and managing them as a single virtual system.


Step-by-Step Setup on AWS EC2

Step 1: Launch EC2 Instances

Spin up 4 Amazon Linux instances from the AWS console.

Step 2: Install Docker on All Nodes

yum install docker -y

Step 3: Start Docker Services

systemctl start docker
docker info

Setting Up the Swarm Cluster

Step 4: Configure Master Node (Edit Inbound Rules)

Ensure that TCP port 2377 is open (Swarm management port), along with other necessary Docker ports (4789, 7946).

Step 5: Ping Test from Slave Nodes

Check connectivity between slave and master:

ping <master-private-ip>

Step 6: Initialize Swarm on Master

docker swarm init --advertise-addr <master-private-ip>

Output includes a token to join worker nodes.


Adding Worker Nodes to the Swarm

Step 7: Join Workers

Run the token command provided by the master on each slave:

docker swarm join --token <token> <master-private-ip>:2377

Step 8: Verify Node List from Master

docker node ls

Output will list all nodes with roles and statuses.


Deploying Services on the Swarm

Now that your cluster is ready, you can deploy services.

Step 9: Launch a Web Server on the Swarm

docker service create --name webserver httpd

Step 10: Check Service Status

docker service ls

You should see your service running in replicated mode.


What's Happening Behind the Scenes?

  • Swarm Manager: Handles orchestration, task distribution, and health checks.

  • Worker Nodes: Execute tasks assigned by the manager.

  • Services: High-level abstractions for running containers in a distributed manner.


Recap

ConceptPurpose
Vertical ScalingAdd more resources to one system
Horizontal ScalingAdd more machines for load distribution
Docker SwarmNative clustering and orchestration
docker serviceDeploy apps across multiple nodes
docker swarm initStart cluster from master node

Why Use Docker Swarm?

  • High Availability

  • Load Balancing

  • Easy to Setup on Cloud (like AWS)

  • Simple CLI-based Management

  • Production-Ready Clustering


Have questions or suggestions? Feel free to drop a comment or message me on LinkedIn - always happy to discuss cloud-native architecture

0
Subscribe to my newsletter

Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitin Dhiman
Nitin Dhiman

Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing ๐Ÿš€