Docker Swarm

Docker Swarm is Docker's native orchestration and clustering tool, designed to help manage multiple containers across multiple hosts. It simplifies deploying, managing, and scaling containers, making it a valuable tool for building robust, distributed applications.

1. What is Docker Swarm?

Docker Swarm allows you to group multiple Docker hosts into a single "Swarm" and manage them as one entity. In this Swarm, Docker containers are deployed across various hosts, and Docker Swarm ensures that the containers are managed and scaled correctly.

2. Key Concepts of Docker Swarm

Before diving into Swarm, it's important to understand its key components:

2.1. Nodes

  • Manager Node: The node that controls the Swarm. It manages the cluster, handles the orchestration, and distributes tasks across the worker nodes. It can also run containers.

  • Worker Node: These nodes receive and execute tasks from the manager node but do not manage the Swarm.

2.2. Services

A service in Docker Swarm is the definition of a task (e.g., running a specific container). It is the abstraction that allows you to define the desired state of containers in the cluster.

2.3. Tasks

Tasks represent individual containers that are deployed by the Swarm across various worker nodes. A service can have many tasks.

2.4. Overlay Network

Docker Swarm uses the overlay network to allow containers to communicate securely across different Docker hosts. It abstracts away networking complexities and allows services to connect seamlessly.

2.5. Replicated vs Global Services

  • Replicated Services: Multiple replicas of the service run across different nodes, and you can specify how many replicas you want.

  • Global Services: A single instance of the service is deployed on every node in the Swarm.

3. Setting up Docker Swarm

3.1. Initialize Docker Swarm

You start by initializing Swarm on a manager node. Run this command on a Docker host:

bashCopy codedocker swarm init

This will turn the current Docker node into a manager node and create a Swarm.

3.2. Adding Nodes to the Swarm

Once a Swarm is initialized, you can add other Docker nodes (workers or managers) to the Swarm.

  1. Get the Join Token (For Workers or Managers):

     bashCopy codedocker swarm join-token worker
    

    This command will give you a token and instructions for adding worker nodes to the Swarm. You can similarly get a token for adding manager nodes.

  2. Join the Swarm (on the worker node):

     bashCopy codedocker swarm join --token <token> <manager-ip>:2377
    

3.3. Verifying Nodes

To verify the nodes in the Swarm, run this command on the manager node:

bashCopy codedocker node ls

4. Deploying Services in Docker Swarm

4.1. Creating a Service

A service is a long-running container that is managed by Swarm. Here's how to create a service:

bashCopy codedocker service create --name <service_name> --replicas <num> <image_name>

Example:

bashCopy codedocker service create --name web --replicas 3 -p 80:80 nginx

This command creates a service named web with 3 replicas of the nginx container, exposed on port 80.

4.2. Listing Services

You can list all running services using:

bashCopy codedocker service ls

4.3. Inspecting a Service

To see detailed information about a service (such as its tasks, status, and configuration), use:

bashCopy codedocker service inspect <service_name>

4.4. Scaling a Service

You can scale a service to increase or decrease the number of replicas (containers) running across the cluster:

bashCopy codedocker service scale <service_name>=<num>

Example:

bashCopy codedocker service scale web=5

This scales the web service to 5 replicas.

4.5. Updating a Service

You can update a running service (for instance, to change the image version):

bashCopy codedocker service update --image <new_image> <service_name>

Example:

bashCopy codedocker service update --image nginx:latest web

5. Docker Swarm Networking

5.1. Creating an Overlay Network

To allow services to communicate across nodes, Docker Swarm uses an overlay network.

bashCopy codedocker network create --driver overlay <network_name>

Example:

bashCopy codedocker network create --driver overlay my-overlay-network

5.2. Attaching Services to an Overlay Network

When creating services, you can attach them to an overlay network to enable communication between them.

bashCopy codedocker service create --name db --network my-overlay-network postgres
docker service create --name web --network my-overlay-network nginx

This ensures that the web service can communicate with the db service over the my-overlay-network network.

6. Managing the Swarm

6.1. Drain a Node

If you need to take a node out of service (for maintenance, for example), you can drain it. Draining means that no new tasks will be scheduled on that node, and existing tasks will be rescheduled on other nodes.

bashCopy codedocker node update --availability drain <node_id>

6.2. Promote/Demote Nodes

To promote a worker node to a manager, use the following command:

bashCopy codedocker node promote <node_id>

To demote a manager node to a worker:

bashCopy codedocker node demote <node_id>

6.3. Leave the Swarm

To remove a node from the Swarm, use the following command on the node:

bashCopy codedocker swarm leave

7. Docker Swarm Stack

A stack in Docker Swarm is a collection of services that are deployed using a docker-compose.yml file. It allows you to manage multiple interrelated services more efficiently.

7.1. Creating a Stack

First, create a docker-compose.yml file:

yamlCopy codeversion: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: postgres

Then, deploy the stack:

bashCopy codedocker stack deploy -c docker-compose.yml <stack_name>

Example:

bashCopy codedocker stack deploy -c docker-compose.yml my_stack

7.2. Listing Stacks

You can list all stacks running in your Swarm using:

bashCopy codedocker stack ls

7.3. Remove a Stack

To remove a stack, use the following command:

bashCopy codedocker stack rm <stack_name>

8. High Availability & Failover

Docker Swarm is designed for high availability. If a node in the Swarm goes down, the manager node will automatically reschedule the failed containers to other nodes.

9. Swarm Security

Docker Swarm uses mutual TLS (Transport Layer Security) for secure communication between nodes in the Swarm. This ensures that nodes and services are authenticated and encrypted.

  • Manager-Worker Communication: All communication between managers and workers is encrypted.

  • Automatic Certificate Rotation: Docker Swarm automatically rotates certificates periodically to improve security.

10. Monitoring Docker Swarm

To monitor Swarm performance and operations, you can integrate with monitoring tools like:

  • Prometheus: For metrics collection.

  • Grafana: For visualizing Swarm metrics.

  • Docker’s Built-in Logs and Metrics: Use docker service logs <service_name> to view logs, and docker stats to see container resource usage.


Summary

Docker Swarm provides a robust way to orchestrate and manage containers across a cluster of machines. It’s simpler than Kubernetes but offers essential features like service discovery, scaling, load balancing, and high availability. Here's a recap of key Docker Swarm operations:

  • Initialize Swarm: docker swarm init

  • Join Swarm: docker swarm join

  • Create Service: docker service create

  • Scale Service: docker service scale

  • Create Stack: docker stack deploy

  • Manage Nodes: docker node ls, docker node promote, docker node demote

For large-scale applications, Docker Swarm simplifies managing, scaling, and orchestrating containers across multiple hosts.

Feel free to dive deeper into any specific aspect if you want more details!

0
Subscribe to my newsletter

Read articles from Yuvraj Singh Nain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yuvraj Singh Nain
Yuvraj Singh Nain