CodeGym /Courses /Docker SELF /Introduction to Docker Swarm

Introduction to Docker Swarm

Docker SELF
Level 18 , Lesson 0
Available

6.1 Main Concepts of Docker Swarm

Docker Swarm is Docker's built-in container orchestration system. It lets you create, manage, and scale a cluster of Docker hosts, turning them into a single logical host where you can run containers. Swarm automatically distributes tasks among nodes, offering convenience and fault tolerance. In this lecture, we'll go over the key components and basic commands for managing Docker Swarm.

Main concepts of Docker Swarm:

  1. Cluster (Swarm): a group of Docker hosts combined into a single logical cluster.
  2. Managers: nodes that manage the cluster and distribute tasks among worker nodes.
  3. Worker Nodes (Workers): nodes that execute tasks and run containers.
  4. Services: a logical representation of applications, describing what tasks need to be performed and how many container instances to run.
  5. Tasks: individual containers, which are instances of services.

Docker Swarm Architecture:

Docker Swarm uses a manager-worker architecture. Managers maintain the cluster's state, while worker nodes perform tasks.

Managers:

  • Maintain the state and configuration of the cluster.
  • Distribute tasks to worker nodes.
  • Ensure fault tolerance and high availability (if there are multiple managers).

Worker Nodes:

  • Execute tasks assigned by managers.
  • Run containers based on the tasks.

Essential Docker Swarm Commands:

  1. docker swarm init: initialize a new Swarm cluster.
  2. docker swarm join: join a node to an existing cluster.
  3. docker node ls: view the list of nodes in the cluster.
  4. docker service create: create a new service.
  5. docker service ls: view running services.
  6. docker service scale: scale a service to a specific number of instances.
  7. docker service rm: remove a service from the cluster.

Using Docker Swarm makes managing apps in distributed systems easier by providing fault tolerance, scalability, and deployment simplicity.

6.2 Creating a Docker Swarm Cluster

Creating and managing a Docker Swarm cluster

Step 1: Initialize the Cluster

Run the following command on the first host (manager):

Terminal

docker swarm init --advertise-addr <MANAGER_IPl>

This command initializes a new Swarm cluster, makes the current node a manager, and outputs the command for joining worker nodes.

Step 2: Join Worker Nodes

On other hosts, run the command output by docker swarm init to join them to the cluster:

Terminal

docker swarm join --token <SWARM_TOKEN> <MANAGER_IP>:2377

Where:

  • <SWARM_TOKEN>: the token used to connect to the cluster (provided by the docker swarm init command).
  • <MANAGER_IP>: IP address of the manager node.

Step 3: View Nodes

To view all nodes in the cluster, use the command:

Terminal

docker node ls

This command will show a list of all nodes, including their roles, states, and availability.

Sample output of docker node ls

Terminal

ID                                    HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
qwertyuiop12345    manager-node        Ready               Active             Leader
asdfghjkl67890      worker-node1       Ready               Active
zxcvbnm09876       worker-node2       Ready               Active

Explanation of columns:

  • ID: The unique identifier of the node.
  • HOSTNAME: The hostname of the machine running the node.
  • STATUS: The current status of the node (e.g., Ready).
  • AVAILABILITY: The availability of the node (e.g., Active).
  • MANAGER STATUS: Indicates if the node is a manager (e.g., Leader, Reachable) or a worker (empty).

Now the Docker Swarm cluster is set up, and you can deploy and scale applications.

6.3 Managing Docker Swarm

Managing services in Docker Swarm

1. Creating a Service

Create a service that will run with a defined number of replicas:

Terminal

docker service create --name my_web --replicas 3 -p 8080:80 nginx

In this example, a service my_web is created, which runs 3 instances of Nginx. Containers expose port 80 to port 8080 on the host.

2. Viewing Services

To see all running services in the cluster, use the command:

Terminal

docker service ls

Example output of the docker service ls command

Terminal

ID                     NAME         MODE           REPLICAS     IMAGE
ab12345               my_web       replicated     3/3          nginx:latest

3. Scaling a Service

To change the number of service instances (replicas) use the docker service scale command:

Terminal

docker service scale my_web=5

Now the my_web service will have 5 instances.

4. Updating a Service

To update the service image to a new version, use the docker service update command:

Terminal

docker service update --image nginx:latest my_web

Ensuring High Availability

Docker Swarm ensures high availability through:

  1. Task distribution across nodes in the cluster for automatic container recovery in case of failures.
  2. Support for multiple managers: It's recommended to have an odd number of managers (e.g., 3 or 5) to avoid quorum issues.
  3. Using replicas: Configure multiple replicas for each service to improve fault tolerance.

Quorum Concept

Quorum represents the majority of nodes that need to agree on a specific action (e.g., data write or leader election) for it to be considered valid. For example, in a 5-node cluster, the quorum would be 3 nodes.

Helpful tip! Make sure managers are placed on different physical or cloud servers to minimize the risk of simultaneous failures.

6.4 Example of Docker Compose configuration for Docker Swarm

You can use Docker Compose with Docker Swarm to make managing multi-container apps easier.

Example of a docker-compose.yml file

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "8080-8090:80"
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
    networks:
      - my_overlay_network

  db:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: example
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    networks:
      - my_overlay_network

networks:
  my_overlay_network:
    driver: overlay
    

Running a Docker Compose stack in Swarm

To deploy the stack in Docker Swarm, use the command:

Terminal

docker stack deploy -c docker-compose.yml mystack

Viewing the stack

To check deployed stacks:

Terminal

docker stack ls

Viewing services in the stack

Terminal

docker stack services mystack
3
Task
Docker SELF, level 18, lesson 0
Locked
Initializing a Swarm Cluster
Initializing a Swarm Cluster
3
Task
Docker SELF, level 18, lesson 0
Locked
Joining worker nodes to the Swarm
Joining worker nodes to the Swarm
3
Task
Docker SELF, level 18, lesson 0
Locked
Creating and scaling a service
Creating and scaling a service
3
Task
Docker SELF, level 18, lesson 0
Locked
Updating a Service in Swarm
Updating a Service in Swarm
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION