Detailed explanation of Docker Swarm concepts and usage

Detailed explanation of Docker Swarm concepts and usage

Docker Swarm is a container cluster management service developed by Docker. Starting from version 1.12.0, it has become a part of Docker after installation (bundled software), also known as Swarm Mode, and no additional installation is required.

Compared to Kubernetes, Docker Swarm is a simple software that seems to be underwhelming. But its compatibility with docker-compose makes up for everything. For beginners who have no experience in using clusters, starting with Docker Swarm is a good choice.

concept

Docker Swarm mainly includes the following concepts:

  • Swarm
  • Node
  • Stack
  • Service
  • Task
  • Load balancing

Swarm itself means "group", a crowd or a swarm. This refers to the state of the computer cluster after it is connected using Docker. The docker swarm command can create, join, and leave a cluster.

Node is a computer node, which can also be considered a Docker node. Node is divided into two categories: Manager and Worker. A Swarm must have at least one Manager, and some management commands can only be used on the Manager. Both types of nodes can run services, but only the Manager can execute run commands. For example, you can use the docker node command to view, configure, and delete Node only in the Manager.

A Stack is a group of Services, similar to docker-compose. By default, a Stack shares a Network and is mutually accessible but isolated from other Stack networks. This concept is just for the convenience of arrangement. The docker stack command can easily operate a Stack instead of operating Services one by one.

Service is a type of container. For users, Service is the core content of interaction with Swarm. Service has two running modes: one is replicated, which specifies the number of containers a Service runs; the other is global, which runs a container of this type on all nodes that meet the running conditions. The docker service command can operate the Service in Swarm.

Task refers to the task of running a container, which is the smallest unit of Swarm execution command. To successfully run a Service, you need to execute one or more Tasks (depending on the number of containers of a Service) to ensure that each container is started successfully. Usually users operate Services rather than Tasks.

Load balancing also includes reverse proxy. Swarm uses Ingress load balancing, which means that any access to a published port on each node can be automatically proxied to the real service. The general principle is shown in the figure below.

Replicated Mode

services: 
 some-serivce: 
  ... 
  deploy: 
   mode: replicated 
   replicas: 3

By default, mode is replicated, so this line can be omitted. The default number of replicas is 1, which means that this Service only starts one container. In this mode, you can start multiple services on demand, and Swarm will automatically adjust. Sometimes a Node will start multiple containers.

Global Mode

services: 
 some-serivce: 
  ... 
  deploy: 
   mode: global 
   placement: 
    ...

Deploy one for all deployable Nodes. Through placement, you can limit the nodes that meet the conditions and avoid deployment on inappropriate nodes.

operate

Some commonly used specific operations are listed here.

Creating the First Node

docker swarm init --advertise-addr $IP

$IP is the externally accessible IP address of the current Node, which is convenient for other Nodes to address.

In this way, a Swarm is initialized, which has only one Manager node.

Adding a new Node to the Swarm

On the Manager node, execute the following command to see how to join a Node:

$ docker swarm join-token manager 
To add a manager to this swarm, run the following command: 
  docker swarm join --token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-ezs4fylj526e801b3cl0pojr5 10.174.28.52:2377 
$ docker swarm join-token worker 
To add a worker to this swarm, run the following command: 
  docker swarm join --token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-164iqklrfv8o3t55g088hylyk 10.174.28.52:2377

On a machine that has not joined any Swarm, execute the command docker swarm join --token ... shown above to become a Manager or Worker node of this Swarm.

Set the node label

On the Manager node, you can set labels for any node:

docker node update $node_name --label-add main=true

$node_name is to set the node ID or HOSTNAME. Label is in the form of a key-value pair. In main=true, main is the key and true is the value.

After setting the Label, you can use the constraints in the placement in the Compose file to limit the available nodes.

services: 
 some-serivce: 
  ... 
  deploy: 
   placement: 
    constraints: 
     - node.labels.main == true 
   ...

The above configuration allows some-service to be used only on nodes where Label is set to main=true.

Start and stop services

docker stack deploy $stack_name -c docker-compose.yaml -c other.yaml ...

$stack_name is the Stack name. You can use -c to specify multiple docker-compose files, or you can deploy multiple files in batches under the same stack. The writing of these YAML files is essentially the same as that of the original docker-compose command, except that the following unique configurations are added and some configurations that are not supported in the Swarm scenario are ignored.

It is recommended to use the docker-compose file to orchestrate the Stack instead of manually creating it using docker service create. For detailed configuration items, see Compose file version 3 reference | Docker Documentation.

To stop all services in a Stack, run the following command.

docker stack rm $stack_name

Update the image of a running service

docker service update --image $image:$tag $service_name

The above is a detailed explanation of the concept and usage of Docker Swarm. For more information about Docker Swarm, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to use Docker Swarm to build WordPress
  • Sample code for implementing rolling updates of services using Docker Swarm
  • Docker Swarm from deployment to basic operations
  • How does docker swarm run a specified container on a specified node?
  • Detailed explanation of Docker Swarm service discovery and load balancing principles
  • Detailed explanation of docker swarm cluster failures and exceptions
  • How to use Docker Swarm to build a cluster
  • Docker Swarm Getting Started Example
  • Detailed explanation of using Docker 1.12 to build a multi-host Docker swarm cluster

<<:  Solution to the garbled code problem in MySQL 5.x

>>:  A brief understanding of MySQL storage field type query efficiency

Recommend

How to implement Echats chart large screen adaptation

Table of contents describe accomplish The project...

Docker packages the local image and restores it to other machines

1. Use docker images to view all the image files ...

Use Docker Compose to quickly deploy ELK (tested and effective)

Table of contents 1. Overview 1.1 Definition 1.2 ...

Display mode of elements in CSS

In CSS, element tags are divided into two categor...

MySQL performance optimization index pushdown

Index condition pushdown (ICP) is introduced in M...

Install mysql 5.6 from yum source in centos7.4 system

System environment: centos7.4 1. Check whether th...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which me...

Detailed explanation of MySQL sql_mode query and setting

1. Execute SQL to view select @@session.sql_mode;...

JavaScript implements the nine-grid click color change effect

This article shares the specific code of JavaScri...

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...

Building a KVM virtualization platform on CentOS7 (three ways)

KVM stands for Kernel-based Virtual Machine, whic...

Command to remove (delete) symbolic link in Linux

You may sometimes need to create or delete symbol...

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

Implementation steps of vue-element-admin to build a backend management system

Recently, when I was working on a conference heal...