Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.riad.com.bd/llms.txt

Use this file to discover all available pages before exploring further.

Introduction

Docker Swarm is a native clustering and orchestration tool for Docker containers. It allows you to create and manage a cluster of Docker nodes as a single virtual system. In this guide, you will walk through the steps to set up and manage a Docker Swarm cluster.
PrerequisitesBefore you begin, ensure you have the following prerequisites in place:
  • Multiple machines (physical or virtual) with Docker installed.
  • Basic knowledge of Docker and command line operations.

Why use orchestration?

When managing multiple Docker containers across different hosts, several challenges can arise:
Deploying containers on each host manually is time-consuming and error-prone.
Scaling services up or down requires manual intervention on each host.
Distributing traffic evenly across containers is complex without built-in load balancing.
Containers need to know how to find each other, which can be difficult without orchestration.
If a container or host fails, there is no automatic recovery mechanism.
Docker Swarm addresses these challenges by providing a unified platform for deploying, scaling, and managing containerized applications across a cluster of Docker hosts.

Benefits of Docker Swarm

Simplified Deployment

Deploy services across multiple nodes with a single command.

High Availability

Automatic failover and recovery of services in case of node failures.

Scalability

Easily scale services up or down with simple commands.

Rolling Updates

Update services with zero downtime using rolling updates.
Other key features include:
  • Health Checks: Automatically monitor and restart unhealthy containers.
  • Built-in Load Balancing: Automatically distributes traffic across containers.
  • Service Discovery: Built-in DNS-based service discovery for easy communication between containers.
  • Secure by Default: Encrypted communication between nodes and secure service-to-service communication.

What is a Service?

In Docker Swarm, a service is a higher-level abstraction that defines how containers should be deployed and managed within the swarm. A service consists of one or more replicas of a containerized application, and it specifies the desired state for those containers. When you create a service, Docker Swarm ensures that the specified number of replicas are running and manages their lifecycle. Key features of Docker Swarm services include:
  1. Replicas: You can define the number of replicas (instances) of a service that should be running at any given time. Docker Swarm will automatically create or remove containers to match the desired state.
  2. Load Balancing: Docker Swarm automatically distributes incoming requests to the available replicas of a service, providing built-in load balancing.
  3. Service Discovery: Services can be discovered by other services within the swarm using DNS-based service discovery.
  4. Rolling Updates: You can update a service with new container images or configurations without downtime, using rolling updates.
  5. Constraints and Preferences: You can specify constraints and preferences for where services should run within the swarm, such as on specific nodes or based on resource availability.
  6. Networking: Services can be connected to overlay networks, allowing containers on different nodes to communicate with each other securely.

Docker Components in Swarm Mode

Docker Swarm mode introduces several key components that work together to provide container orchestration and management across a cluster of Docker nodes. Here are the main components of Docker Swarm mode:
  1. Manager Nodes: Manager nodes are responsible for managing the swarm, including maintaining the desired state of services, scheduling tasks, and handling swarm management operations. They use the Raft consensus algorithm to ensure consistency across the swarm.
  2. Worker Nodes: Worker nodes are responsible for running the containers that make up the services in the swarm. They receive tasks from manager nodes and execute them.
  3. Services: A service is a definition of how containers should be deployed and managed within the swarm. It specifies the desired state for a group of containers, including the number of replicas, networking, and load balancing.
  4. Tasks: A task is a single instance of a service running on a worker node. Each task corresponds to a container that is created and managed by Docker Swarm.
  5. Overlay Networks: Overlay networks allow containers running on different nodes to communicate with each other securely. They provide a virtual network that spans across the swarm, enabling service discovery and load balancing.
  6. Load Balancer: Docker Swarm includes a built-in load balancer that distributes incoming requests to the available replicas of a service, ensuring even distribution of traffic.
  7. Raft Consensus Algorithm: Docker Swarm uses the Raft consensus algorithm to maintain consistency among manager nodes. This ensures that all manager nodes have the same view of the swarm’s state.
  8. Service Discovery: Docker Swarm provides DNS-based service discovery, allowing services to find and communicate with each other using service names.

Setting up a Swarm

1

Check the docker swarm status

In command line use below command:
docker info | grep Swarm
If the swarm is not enabled, we will see below output
Swarm: inactive
2

Initialize the Swarm

On your manager node, run the following command to initialize the swarm:
docker swarm init --advertise-addr <MANAGER-IP>
3

Join Worker Nodes

Copy the join token command output from the previous step and run it on your worker nodes:
docker swarm join --token <TOKEN> <MANAGER-IP>:2377
4

Verify the Cluster

On the manager node, verify that all nodes have joined:
docker node ls
5

Promote all node to manager

On the manager node run below command
docker node promote $(docker node ls -q)

Deploying a Service (Imperative)

In this example, we will deploy an Nginx service using imperative commands.
1

Create the service

Create an Nginx service with 3 replicas mapping port 88 on the host to port 80 in the container.
docker service create --name nginx --replicas 3 -p 88:80 nginx
2

List services

Check which services are running.
docker service ls
3

Check service tasks

See where the service tasks (containers) are running.
docker service ps nginx
4

Scale the service

Scale the service up or down.
docker service scale nginx=5
5

Inspect the service

View detailed information about the service.
docker service inspect --pretty nginx
6

Delete the service

Remove the service from the swarm.
docker service rm nginx

Rolling Updates

Docker Swarm allows you to update services without downtime.
1

Update image

Update the service to use a specific image version.
docker service update --image nginx:1.17.10 nginx
2

Rollback

If an update fails, you can rollback to the previous state.
docker service update --rollback nginx
3

Configure failure action

Define behavior when an update fails.
docker service update --update-failure-action=rollback --update-failure-limit=3 nginx

Global Mode

When running in Global Mode, a container is deployed to every available worker node in the cluster. You do not specify the number of replicas. This is ideal for monitoring agents or logging services that need to run on every node.
docker service create -p 8081:80 --name web --mode global nginx:latest

Docker Stack

A Docker Stack is a group of interrelated services that work together to form a complete, multi-container application, deployed and managed as a single entity. It is specifically used within Docker Swarm mode to orchestrate distributed applications across multiple nodes. Example docker-compose.yml for a stack:
docker-compose.yml
version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "8081:80"
    volumes:
      - /tmp:/usr/share/nginx/html
    deploy:
      mode: replicated
      replicas: 3
To deploy the stack, use the following command:
docker stack deploy --compose-file docker-compose.yml stack-name
To view the deployed stacks, use:
docker stack ls
To remove a stack, use:
docker stack rm stack-name

Conclusion

Docker Swarm provides a powerful and easy-to-use platform for container orchestration and management. By following the steps outlined in this guide, you can set up and manage a Docker Swarm cluster to deploy, scale, and maintain your containerized applications efficiently. For more detailed information and advanced configurations, refer to the official Docker Swarm documentation.