Skip to main content

Setting Up a Swarm Cluster

Imagine organizing a fleet of delivery trucks. One truck alone can’t serve an entire city, so you appoint a manager to coordinate routes and add more trucks as workers. Similarly, in Docker Swarm, you set up a manager node and add worker nodes to form a cluster. Once connected, they operate as one unified system.


Cluster Foundations

1. Swarm Cluster Components

  • Manager Node:
    • Initializes the cluster.
    • Maintains cluster state and schedules tasks.
  • Worker Node:
    • Joins the cluster.
    • Executes tasks assigned by the manager.
  • Services:
    • Define how containers run across the cluster.
  • Tasks:
    • Individual container instances of a service.

2. Requirements for a Swarm Cluster

  • Multiple Docker hosts (VMs or physical machines).
  • Docker installed on each host.
  • Network connectivity between hosts (TCP port 2377 for cluster management, 7946 for communication, 4789 for overlay networking).

3. Workflow for Setting Up a Swarm Cluster

  1. Initialize the Swarm on the manager node.
  2. Generate a join token.
  3. Add worker nodes using the token.
  4. Verify the cluster status.
  5. Deploy services across the cluster.

Things to Remember

  • A Swarm cluster requires at least one manager and one worker.
  • Join tokens are used to securely add nodes.
  • Managers handle scheduling; workers execute tasks.

Hands‑On Lab

Step 1: Initialize Swarm on Manager Node

docker swarm init --advertise-addr <MANAGER_IP>
  • --advertise-addr specifies the manager’s IP address.

Step 2: Get Worker Join Token

docker swarm join-token worker
  • Displays the command workers must run to join.

Step 3: Add Worker Nodes
On each worker node:

docker swarm join --token <WORKER_TOKEN> <MANAGER_IP>:2377

Step 4: Verify Cluster Status
On manager node:

docker node ls
  • Shows all nodes in the cluster.

Step 5: Deploy a Service

docker service create --name web --replicas 3 nginx
  • Runs 3 replicas of Nginx distributed across nodes.

Step 6: Inspect Service Distribution

docker service ps web
  • Displays which nodes are running each replica.

Practice Exercise

  1. Set up a Swarm cluster with one manager and two workers.
  2. Deploy a service frontend with 4 replicas.
  3. Verify that replicas are distributed across nodes.
  4. Remove one worker node and observe how Swarm reschedules tasks.
  5. Reflect on how Swarm ensures high availability.

Visual Learning Model

Swarm Cluster
   ├── Manager Node (schedules tasks)
   ├── Worker Node 1 (runs tasks)
   └── Worker Node 2 (runs tasks)
Service: frontend → 4 replicas distributed across nodes

The Hackers Notebook

Setting up a Swarm cluster involves initializing a manager node, adding worker nodes with join tokens, and deploying services across the cluster. Managers handle scheduling, while workers execute tasks. This setup enables scalability, high availability, and distributed workloads.


Updated on Dec 26, 2025