Swarm Cluster and Services

Back

Loading concept...

🐝 Docker Swarm: Your Container Orchestra

Imagine you have a bee hive. One bee can carry some honey, but a whole swarm of bees working together? They can do AMAZING things! Docker Swarm works the same wayβ€”it’s how we get many computers to work together as one super-team.


🎯 What is Docker Swarm?

Think of Docker Swarm like a school play:

  • You have one director (the manager) who decides what happens
  • You have actors (workers) who do the actual performing
  • Together, they create something magical!

Without Swarm: You run containers on ONE computer. If it breaks, everything stops. 😒

With Swarm: You run containers across MANY computers. If one breaks, others keep going! πŸŽ‰

Single Computer          Docker Swarm
    πŸ“¦                   πŸ“¦ πŸ“¦ πŸ“¦
    ⬇️                   ⬇️ ⬇️ ⬇️
   πŸ’»                  πŸ’» πŸ’» πŸ’»
 (risky!)            (safe & strong!)

πŸš€ Swarm Mode Initialization

Before bees can work together, they need to form a swarm. Same with Docker!

Starting Your Swarm

On your first computer (this becomes the manager):

docker swarm init

That’s it! One command. Docker will reply with something like:

Swarm initialized: current node
is now a manager.

To add a worker, run:
docker swarm join --token SWMTKN-1-abc123...

Real Example:

docker swarm init --advertise-addr 192.168.1.10

This tells other computers: β€œHey! Find me at this address!”

What Happens Behind the Scenes?

graph TD A["Your Computer"] -->|docker swarm init| B["🎩 Manager Node"] B --> C["Ready to Accept Workers!"] C --> D["Swarm is Born 🐝"]

πŸ‘” Managers and Workers

Every swarm has two types of members:

🎩 Managers (The Bosses)

  • Decide what containers to run
  • Remember the state of everything
  • Direct workers to do tasks
  • Can also do work themselves!

πŸ‘· Workers (The Doers)

  • Follow manager instructions
  • Run containers
  • Report back their status
  • Cannot make decisions alone

Think of it like a restaurant:

  • 🎩 Manager = Head Chef (plans the menu, assigns tasks)
  • πŸ‘· Worker = Line Cooks (prepare the dishes)

How Many Managers?

Swarm Size Recommended Managers
Small (dev) 1 manager
Medium 3 managers
Large 5 managers

Why odd numbers? If managers disagree, they vote! Odd numbers prevent ties.

graph TD M1["🎩 Manager 1"] --- M2["🎩 Manager 2"] M2 --- M3["🎩 Manager 3"] M1 --- M3 M1 --> W1["πŸ‘· Worker 1"] M2 --> W2["πŸ‘· Worker 2"] M3 --> W3["πŸ‘· Worker 3"]

πŸšͺ Joining and Leaving a Swarm

Joining as a Worker

Remember that token from docker swarm init? Workers use it:

docker swarm join \
  --token SWMTKN-1-abc123... \
  192.168.1.10:2377

Breaking it down:

  • --token = Your secret password to join
  • 192.168.1.10:2377 = Manager’s address

Joining as a Manager

Want another manager? Get a special manager token:

# On existing manager:
docker swarm join-token manager

# Then use that token on new machine

Leaving the Swarm

Worker leaving:

docker swarm leave

Manager leaving:

docker swarm leave --force

⚠️ Warning: Managers need --force because leaving affects the whole swarm!

Removing a Node (from Manager’s side)

docker node rm worker-node-name
graph TD A["New Computer"] -->|join token| B["Swarm"] B -->|Welcome!| A C["Leaving Computer"] -->|swarm leave| D["Goodbye! πŸ‘‹"]

πŸ“¦ Swarm Services

Here’s where the magic happens! Services are how you run apps in a swarm.

Container vs Service

Container Service
Runs on ONE machine Runs on MANY machines
You manage it Swarm manages it
If it dies, it’s dead If it dies, swarm restarts it!

Think of it like:

  • 🏠 Container = One house
  • 🏘️ Service = A whole neighborhood of identical houses

Your First Service

docker service create \
  --name my-web \
  --replicas 3 \
  nginx

This says: β€œRun 3 copies of nginx, keep them running!”

graph TD S["🎯 Service: my-web"] --> R1["πŸ“¦ Replica 1"] S --> R2["πŸ“¦ Replica 2"] S --> R3["πŸ“¦ Replica 3"] R1 --> N1["πŸ’» Node 1"] R2 --> N2["πŸ’» Node 2"] R3 --> N3["πŸ’» Node 3"]

Checking Your Services

# List all services
docker service ls

# See details of one service
docker service ps my-web

πŸ“ˆ Service Creation and Scaling

Creating Services with Options

docker service create \
  --name api \
  --replicas 5 \
  --publish 8080:80 \
  --env APP_ENV=production \
  my-api-image

What this does:

  • --name api β†’ Call it β€œapi”
  • --replicas 5 β†’ Run 5 copies
  • --publish 8080:80 β†’ Port 8080 outside β†’ 80 inside
  • --env β†’ Set environment variable

Scaling Up and Down

Need more power? Scale up!

docker service scale my-web=10

Now you have 10 copies instead of 3! πŸš€

Too many? Scale down:

docker service scale my-web=2

Scaling Multiple Services

docker service scale \
  web=5 \
  api=3 \
  cache=2

One command, multiple services scaled!

graph TD subgraph Before B1["πŸ“¦ πŸ“¦ πŸ“¦"] end subgraph After Scale Up A1["πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦ πŸ“¦"] end Before -->|scale=10| After

Updating Services

docker service update \
  --image nginx:latest \
  my-web

This updates all replicas to the new imageβ€”one at a time, no downtime!


🌍 Global vs Replicated Services

Two ways to spread your containers:

πŸ”„ Replicated Services (Default)

β€œRun exactly N copies, put them anywhere”

docker service create \
  --name web \
  --replicas 3 \
  nginx
  • You choose how many: 3 replicas
  • Swarm decides where to put them
  • Some nodes might have 0, some might have 2

Use when: You need a specific number of copies

🌍 Global Services

β€œRun exactly ONE copy on EVERY node”

docker service create \
  --name monitor \
  --mode global \
  datadog/agent
  • Every node gets exactly one
  • New node joins? It automatically gets one too!
  • Node leaves? That copy goes away

Use when:

  • Monitoring agents (need one per machine)
  • Log collectors
  • Security scanners

Side by Side Comparison

REPLICATED (3 copies)     GLOBAL (1 per node)

Node 1: πŸ“¦ πŸ“¦             Node 1: πŸ“¦
Node 2: πŸ“¦                Node 2: πŸ“¦
Node 3: (empty)           Node 3: πŸ“¦
Node 4: (empty)           Node 4: πŸ“¦

Total: 3 containers       Total: 4 containers
                          (one per node)
graph TD subgraph Replicated R["Service"] --> R1["πŸ“¦"] R --> R2["πŸ“¦"] R --> R3["πŸ“¦"] end subgraph Global G["Service"] --> G1["πŸ“¦ Node1"] G --> G2["πŸ“¦ Node2"] G --> G3["πŸ“¦ Node3"] G --> G4["πŸ“¦ Node4"] end

πŸŽ‰ Putting It All Together

Let’s create a real swarm setup!

Step 1: Initialize on first machine

docker swarm init --advertise-addr 192.168.1.10

Step 2: Join workers

# On other machines:
docker swarm join --token SWMTKN-1-xxx 192.168.1.10:2377

Step 3: Create services

# Web app - 5 replicas
docker service create \
  --name web \
  --replicas 5 \
  --publish 80:80 \
  nginx

# Monitoring - one per node
docker service create \
  --name monitor \
  --mode global \
  prom/node-exporter

Step 4: Check everything

docker node ls          # See all nodes
docker service ls       # See all services
docker service ps web   # See web replicas

🧠 Key Takeaways

Concept One-Line Summary
Swarm Many computers acting as one
Manager The brain that makes decisions
Worker The hands that do the work
Service Your app, managed by the swarm
Replicated Run N copies anywhere
Global Run 1 copy everywhere

πŸš€ You Did It!

You now understand how Docker Swarm turns many computers into one powerful team. Like bees in a hive, each node has its role, and together they’re unstoppable!

Remember:

  • One swarm init creates your hive
  • Tokens let others join
  • Services keep your apps running
  • Scaling is just one command away!

Go forth and orchestrate! 🐝✨

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.