Analysis of the use and principle of Docker Swarm cluster management

Analysis of the use and principle of Docker Swarm cluster management

Swarm Cluster Management

Introduction

Docker Swarm is a cluster management tool for Docker. It turns a pool of Docker hosts into a single virtual Docker host. Docker Swarm provides the standard Docker API, so any tool that already communicates with the Docker daemon can use Swarm to easily scale to multiple hosts.

Supported tools include, but are not limited to, the following:

  • Dokku
  • Docker Compose
  • Docker Machine

principle

As shown in the figure below, the swarm cluster consists of a management node (manager) and a worker node (work node).

  • Swarm manager: Responsible for the management of the entire cluster, including cluster configuration, service management, and all other cluster-related tasks.
  • Work node: the available node in the figure, which is mainly responsible for running the corresponding services to execute tasks.

insert image description here

use

The following examples are all based on Docker Machine and VirtualBox. Make sure that VirtualBox has been installed on your host.

1. Create a swarm cluster management node (manager)

Create a docker machine:

$ docker-machine create -d virtualbox swarm-manager 

insert image description here

Initialize the swarm cluster. The machine that performs the initialization is the management node of the cluster.

$ docker-machine ssh swarm-manager
$ docker swarm init --advertise-addr 192.168.99.107 #The IP here is the IP assigned when the machine is created. 

insert image description here

The above output proves that initialization has been successful. You need to copy the following line, which will be used when adding working nodes:

docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh3j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377

2. Create a swarm cluster worker node (worker)

Here we create two machines, swarm-worker1 and swarm-worker2.

insert image description here

Enter the two machines separately and specify to add to the cluster created in the previous step. The content copied in the previous step will be used here.

insert image description here

The above data output indicates that it has been added successfully.

In the figure above, the content copied in the previous step is relatively long and will be automatically truncated. The commands actually run in the figure are as follows:

docker@swarm-worker1:~$ docker swarm join --token SWMTKN-1-4oogo9qziq768dma0uh3j0z0m5twlm10iynvz7ixza96k6jh9p-ajkb6w7qd06y1e33yrgko64sk 192.168.99.107:2377

3. View cluster information

Enter the management node and execute: docker info to view the information of the current cluster.

$ docker info 

insert image description here

4. Deploy services to the cluster

Note: Any operations related to cluster management are performed on the management node.

The following example creates a service named helloworld on a worker node, which is randomly assigned to a worker node:

docker@swarm-manager:~$ docker service create --replicas 1 --name helloworld alpine ping docker.com 

insert image description here

5. Check service deployment

Check which node the helloworld service is running on and you can see that it is currently running on the swarm-worker1 node:

docker@swarm-manager:~$ docker service ps helloworld 

insert image description here

View the detailed information of helloworld deployment:

docker@swarm-manager:~$ docker service inspect --pretty helloworld 

insert image description here

6. Expanding cluster services

We will expand the above helloworld service to two nodes.

docker@swarm-manager:~$ docker service scale helloworld=2 

insert image description here

You can see that it has expanded from one node to two nodes.

insert image description here

7. Deleting Services

docker@swarm-manager:~$ docker service rm helloworld 

insert image description here

Check if it has been deleted:

insert image description here

8. Rolling upgrade service

In the following example, we will introduce how to perform a rolling upgrade of the redis version to a higher version.

Create a 3.0.6 version of redis.

docker@swarm-manager:~$ docker service create --replicas 1 --name redis --update-delay 10s redis:3.0.6 

insert image description here

Rolling upgrade of redis.

docker@swarm-manager:~$ docker service update --image redis:3.0.7 redis 

insert image description here

From the picture, we can see that the redis version has been upgraded from 3.0.6 to 3.0.7, indicating that the service has been upgraded successfully.

9. Stop a node from receiving new tasks

View all nodes:

docker@swarm-manager:~$ docker node ls 

insert image description here

It can be seen that all nodes are currently Active and can receive new task assignments.

Stop the node swarm-worker1:

insert image description here

Note: The state of swarm-worker1 changes to Drain. The cluster service will not be affected, but the swarm-worker1 node will no longer receive new tasks, and the cluster load capacity will decrease.

The node can be reactivated with the following command:

docker@swarm-manager:~$ docker node update --availability active swarm-worker1 

insert image description here

This is the end of this article about the use and principle analysis of Docker Swarm cluster management. For more relevant Docker Swarm cluster management content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use Docker Swarm to build WordPress
  • Example of using Docker Swarm to build a distributed crawler cluster
  • How to use Docker Swarm to build a cluster
  • Detailed explanation of using Docker 1.12 to build a multi-host Docker swarm cluster
  • How to install Docker and use it in Docker Swarm mode
  • Docker swarm simple tutorial

<<:  A brief discussion on four solutions for Vue single page SEO

>>:  MySQL 8.0.22 compressed package complete installation and configuration tutorial diagram (tested and effective)

Recommend

HTML Code Writing Guide

Common Convention Tags Self-closing tags, no need...

IE8 uses multi-compatibility mode to display web pages normally

IE8 will have multiple compatibility modes . IE pl...

Detailed explanation of json file writing format

Table of contents What is JSON Why this technolog...

How to use binlog for data recovery in MySQL

Preface Recently, a data was operated incorrectly...

Let's talk about Vue's mixin and inheritance in detail

Table of contents Preface Mixin Mixin Note (dupli...

Implementation of nginx flow control and access control

nginx traffic control Rate-limiting is a very use...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

HTML table markup tutorial (10): cell padding attribute CELLPADDING

Cell padding is the distance between the cell con...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

Implementation of master-slave replication in docker compose deployment

Table of contents Configuration parsing Service C...

How to use vue filter

Table of contents Overview Defining filters Use o...

JavaScript implements the generation of 4-digit random verification code

This article example shares the specific code for...