🐳 Docker Swarm Explained: From Basics to Security & High Availability

Shreyash MyakalShreyash Myakal
4 min read

In the world of container orchestration, Docker Swarm stands as a simple yet powerful alternative to Kubernetes. If you are beginning your journey with DevOps, understanding Docker Swarm is essential to grasp how containerized applications can be deployed and managed efficiently at scale.

In this blog, we’ll explore:

  • What is Docker Swarm?

  • Why do we use Docker Swarm?

  • What is the Raft Consensus Algorithm?

  • Leader Agreement and High Availability

  • Docker Swarm Security with TLS

  • Step-by-Step Docker Swarm Setup (Master & Worker Nodes)

  • Deploying a Service in Swarm


✅ What is Docker Swarm?

Docker Swarm is Docker’s native clustering and orchestration tool. It allows you to manage a cluster of Docker engines as a single virtual system. This makes it easier to scale and manage containerized applications across multiple machines.

In simple words: Docker Swarm turns a group of Docker machines into a single, highly available system that can deploy and manage containers easily.


🚀 Why Use Docker Swarm?

Here are the key reasons:

  • High Availability: If a node fails, services are automatically shifted to healthy nodes.

  • Scaling: You can increase or decrease the number of container instances easily.

  • Load Balancing: Swarm manages network traffic and distributes it across containers.

  • Declarative Service Model: You define the desired state, and Swarm maintains it.

  • Simple CLI Integration: It integrates seamlessly with Docker CLI.


🧠 Raft Consensus Algorithm in Docker Swarm

Docker Swarm uses the Raft consensus algorithm to maintain consistency across the manager nodes in a cluster.

🔄 What is Raft?

Raft ensures that all manager nodes agree on the current state of the swarm cluster, including services, networks, and nodes. It’s a consensus algorithm used in distributed systems to avoid conflicts or data inconsistency.

🧩 Key Components of Raft:

  1. Leader – Accepts client requests and manages cluster updates.

  2. Followers – Sync their state with the leader.

  3. Candidates – Try to become a leader during elections.

⚙️ Types of Node Roles:

  • Manager Node: Handles orchestration, Raft consensus, and API requests.

  • Worker Node: Executes containers/tasks as instructed by the manager.

Docker Swarm uses an odd number of manager nodes (e.g., 3, 5) to ensure majority quorum in Raft.


👑 Leader Agreement & High Availability

  • Only one leader exists at any given time.

  • If the leader fails, a new one is elected from the manager nodes via Raft.

  • To achieve high availability, you should always run multiple manager nodes (minimum 3 for production).


🔐 Docker Swarm Security (TLS Encryption)

Docker Swarm secures communication between nodes using TLS (Transport Layer Security).

🔒 Key Security Features:

  • Mutual TLS (mTLS): All nodes authenticate each other using certificates.

  • Automatic Certificate Rotation: Swarm rotates node certificates every 90 days.

  • Encrypted Communication: Manager-to-worker and inter-manager communications are encrypted.

🔑 How It Works:

  • When a node joins the swarm, it receives a TLS certificate signed by the Swarm Root CA.

  • This ensures only trusted nodes can participate in the cluster.


⚙️ Docker Swarm Installation & Setup

Let's go step-by-step to create a Docker Swarm with 1 Manager and 2 Worker Nodes.

🧱 1. Install Docker (on all nodes)

bashCopyEditsudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

🧭 2. Initialize Swarm (on Master node)

bashCopyEditdocker swarm init --advertise-addr <MASTER-IP>

It will give you a command like this:

bashCopyEditdocker swarm join --token <WORKER-TOKEN> <MASTER-IP>:2377

🧩 3. Join Worker Nodes (run this on each worker)

bashCopyEditdocker swarm join --token SWMTKN-1-xyz123 <MASTER-IP>:2377

📋 4. Verify Nodes (on Manager)

bashCopyEditdocker node ls

📦 Docker Swarm Service Deployment

Now let’s deploy a simple nginx web server using Swarm services.

bashCopyEditdocker service create --name webserver --replicas 3 -p 80:80 nginx

✨ Explanation:

  • --name webserver: Name of the service

  • --replicas 3: Run 3 instances

  • -p 80:80: Map container port to host port

  • nginx: Docker image to run


📜 Summary

ConceptExplanation
Docker SwarmNative Docker orchestration tool
Why Use ItHA, scalability, load balancing
Raft AlgorithmKeeps managers in sync
Leader AgreementEnsures one active manager
High AvailabilityMultiple managers avoid downtime
SecurityTLS encryption for secure communication
InstallationSet up Swarm with init and join
Service DeployEasily launch scalable containers

🧠 Final Thoughts

Docker Swarm offers a clean and simple way to orchestrate containers for small to medium-scale production setups. While Kubernetes is more powerful and flexible, Swarm’s learning curve is gentler, making it perfect for beginners and quick deployments.

If you’re starting out in DevOps, try setting up a Swarm cluster and deploying your own multi-container app—you’ll learn orchestration, networking, and service management all in one go!

0
Subscribe to my newsletter

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

Written by

Shreyash Myakal
Shreyash Myakal

I’m currently learning Linux, AWS, DevOps, MySQL, and related technologies, aiming to become a Cloud Engineer. Passionate about cloud infrastructure and automation, I’m excited to apply these skills in real-world projects.