> ## Documentation Index
> Fetch the complete documentation index at: https://testdocs.riad.com.bd/llms.txt
> Use this file to discover all available pages before exploring further.

# Docker Swarm

> An in-depth guide to setting up and managing a Docker Swarm cluster for container orchestration.

## 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.

<Info>
  **Prerequisites**

  Before 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.
</Info>

## Why use orchestration?

When managing multiple Docker containers across different hosts, several challenges can arise:

<AccordionGroup>
  <Accordion title="Manual Deployment">
    Deploying containers on each host manually is time-consuming and error-prone.
  </Accordion>

  <Accordion title="Scaling Issues">
    Scaling services up or down requires manual intervention on each host.
  </Accordion>

  <Accordion title="Load Balancing">
    Distributing traffic evenly across containers is complex without built-in load balancing.
  </Accordion>

  <Accordion title="Service Discovery">
    Containers need to know how to find each other, which can be difficult without orchestration.
  </Accordion>

  <Accordion title="Fault Tolerance">
    If a container or host fails, there is no automatic recovery mechanism.
  </Accordion>
</AccordionGroup>

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

<CardGroup cols={2}>
  <Card title="Simplified Deployment" icon="rocket">
    Deploy services across multiple nodes with a single command.
  </Card>

  <Card title="High Availability" icon="shield-check">
    Automatic failover and recovery of services in case of node failures.
  </Card>

  <Card title="Scalability" icon="chart-line">
    Easily scale services up or down with simple commands.
  </Card>

  <Card title="Rolling Updates" icon="rotate">
    Update services with zero downtime using rolling updates.
  </Card>
</CardGroup>

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

<Steps>
  <Step title="Check the docker swarm status">
    In command line use below command:

    ```bash theme={null}
    docker info | grep Swarm
    ```

    If the swarm is not enabled, we will see below output

    ```plaintext theme={null}
    Swarm: inactive
    ```
  </Step>

  <Step title="Initialize the Swarm">
    On your manager node, run the following command to initialize the swarm:

    ```bash theme={null}
    docker swarm init --advertise-addr <MANAGER-IP>
    ```
  </Step>

  <Step title="Join Worker Nodes">
    Copy the join token command output from the previous step and run it on your worker nodes:

    ```bash theme={null}
    docker swarm join --token <TOKEN> <MANAGER-IP>:2377
    ```
  </Step>

  <Step title="Verify the Cluster">
    On the manager node, verify that all nodes have joined:

    ```bash theme={null}
    docker node ls
    ```
  </Step>

  <Step title="Promote all node to manager">
    On the manager node run below command

    ```bash theme={null}
    docker node promote $(docker node ls -q)
    ```
  </Step>
</Steps>

## Deploying a Service (Imperative)

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

<Steps>
  <Step title="Create the service">
    Create an Nginx service with 3 replicas mapping port 88 on the host to port 80 in the container.

    ```bash theme={null}
    docker service create --name nginx --replicas 3 -p 88:80 nginx
    ```
  </Step>

  <Step title="List services">
    Check which services are running.

    ```bash theme={null}
    docker service ls
    ```
  </Step>

  <Step title="Check service tasks">
    See where the service tasks (containers) are running.

    ```bash theme={null}
    docker service ps nginx
    ```
  </Step>

  <Step title="Scale the service">
    Scale the service up or down.

    ```bash theme={null}
    docker service scale nginx=5
    ```
  </Step>

  <Step title="Inspect the service">
    View detailed information about the service.

    ```bash theme={null}
    docker service inspect --pretty nginx
    ```
  </Step>

  <Step title="Delete the service">
    Remove the service from the swarm.

    ```bash theme={null}
    docker service rm nginx
    ```
  </Step>
</Steps>

## Rolling Updates

Docker Swarm allows you to update services without downtime.

<Steps>
  <Step title="Update image">
    Update the service to use a specific image version.

    ```bash theme={null}
    docker service update --image nginx:1.17.10 nginx
    ```
  </Step>

  <Step title="Rollback">
    If an update fails, you can rollback to the previous state.

    ```bash theme={null}
    docker service update --rollback nginx
    ```
  </Step>

  <Step title="Configure failure action">
    Define behavior when an update fails.

    ```bash theme={null}
    docker service update --update-failure-action=rollback --update-failure-limit=3 nginx
    ```
  </Step>
</Steps>

## 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.

```bash theme={null}
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:

```yaml docker-compose.yml theme={null}
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:

```bash theme={null}
docker stack deploy --compose-file docker-compose.yml stack-name
```

To view the deployed stacks, use:

```bash theme={null}
docker stack ls
```

To remove a stack, use:

```bash theme={null}
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](https://docs.docker.com/engine/swarm/).
-----------------------------------------------------------------------------------------------------------------------------------------------------
