Isaac.

Docker Swarm: Complete Guide

Table of Contents

  1. What is Docker Swarm?
  2. Key Concepts
  3. Setting Up a Swarm
  4. Services and Tasks
  5. Networking
  6. Storage
  7. Load Balancing
  8. Security
  9. Common Pitfalls and Solutions
  10. Real-World Example

What is Docker Swarm?

Docker Swarm is a container orchestration tool built into Docker that allows you to manage a cluster of Docker nodes as a single virtual system. It provides native clustering capabilities for Docker containers.

Key Features:

  • Declarative service model
  • Scaling and self-healing
  • Load balancing
  • Service discovery
  • Rolling updates
  • Secure by default

Key Concepts

1. Nodes

  • Manager Nodes: Handle cluster management tasks and orchestration
  • Worker Nodes: Execute containers and tasks

2. Services

A service is the definition of the tasks to execute on the worker nodes.

3. Tasks

A task is a running container that is part of a service.

4. Load Balancer

Built-in load balancing distributes requests between containers.

Setting Up a Swarm

Initializing a Swarm

# Initialize 600">swarm on manager 600">node
600">docker 600">swarm init --advertise-addr <MANAGER-IP>

# Example
600">docker 600">swarm init --advertise-addr 192.168.1.100

Output:

Swarm initialized: current 600">node (x1x1x1x1x1x1) is now a manager.

To add a worker to this 600">swarm, run the following command:

    600">docker 600">swarm join --token SWMTKN-1-0exampl3t0ken--192.168.1.100:2377

To add a manager to this 600">swarm, run '600">docker 600">swarm join-token manager' and follow the instructions.

Adding Worker Nodes

# On worker 600">nodes, run the join command from above
600">docker 600">swarm join --token SWMTKN-1-0exampl3t0ken--192.168.1.100:2377

# To get the join token again
600">docker 600">swarm join-token worker

Viewing Swarm Status

# List 600">nodes in the 600">swarm
600">docker 600">node ls

# Inspect a specific 600">node
600">docker 600">node inspect <600">node-id>

# Promote a worker to manager
600">docker 600">node promote <600">node-id>

# Demote a manager to worker
600">docker 600">node demote <600">node-id>

Services and Tasks

A service in Docker Swarm is a definition for running containers (tasks) across the cluster. You can scale services up or down, update them, and roll back if needed.

Creating a Service

600">docker 600">service create --name webserver -p 80:80 600">nginx

This creates a service named webserver using the nginx image, exposing port 80.

Scaling a Service

600">docker 600">service scale webserver=3

This scales the webserver service to 3 replicas.

Updating a Service

600">docker 600">service update --image 600">nginx:600">alpine webserver

This updates the webserver service to use the nginx:alpine image.

Networking

Docker Swarm provides built-in networking for service discovery and communication between containers.

Creating an Overlay Network

600">docker 600">network create -d overlay my_overlay

This creates an overlay network named my_overlay for multi-host communication.

Attaching a Service to a Network

600">docker 600">service create --name db --600">network my_overlay 600">postgres

This attaches the db service to the my_overlay network.

Storage

Swarm supports persistent storage using Docker volumes.

Creating a Volume

600">docker 600">volume create mydata

Using a Volume in a Service

600">docker 600">service create --name db --mount type=600">volume,source=mydata,target=/var/lib/600">postgresql/data 600">postgres

Load Balancing

Swarm automatically load balances requests to services across available nodes and containers.

Testing Load Balancing

600">curl http://<SWARM-IP>:80

Requests will be distributed among the running containers for the service.

Security

  • Mutual TLS between nodes
  • Automatic certificate rotation
  • Role-based access

Rotating Certificates

600">docker 600">swarm ca --rotate

Common Pitfalls and Solutions

  • Firewall Issues: Ensure required ports (2377, 7946, 4789) are open.
  • Node Clock Skew: Use NTP to synchronize clocks.
  • Network Overlap: Avoid overlapping subnets in overlay networks.

Real-World Example: Simple Web Stack

Here is a docker-compose.yml for deploying a simple web stack with Nginx and a static site:

"color:#F59E0B">version: '3.8'
"color:#F59E0B">services:
  "color:#F59E0B">web:
    "color:#F59E0B">image: nginx:alpine
    "color:#F59E0B">ports:
      - '80:80'
    "color:#F59E0B">volumes:
      - ./public:/usr/share/nginx/html:ro
    "color:#F59E0B">deploy:
      "color:#F59E0B">replicas: 2
      "color:#F59E0B">restart_policy:
        "color:#F59E0B">condition: on-failure
"color:#F59E0B">networks:
  "color:#F59E0B">default:
    "color:#F59E0B">external:
      "color:#F59E0B">name: my_overlay

Deploy the stack:

600">docker 600">stack deploy -c 600">docker-compose.yml my600">stack

Check Stack Status

600">docker 600">stack 600">services my600">stack
600">docker 600">stack ps my600">stack

Conclusion

Docker Swarm is a powerful, production-ready orchestrator built into Docker. While Kubernetes is more popular for large-scale deployments, Swarm is a great choice for simpler setups and teams already using Docker. With its easy setup, built-in security, and native Docker integration, Swarm remains a valuable tool for container orchestration.