开始尝试使用swarm之第二篇-节点类型

尝试一下docker-swarm之节点类型的特点区分

再加两个个虚拟机vm以准备

接上文,原本有一个管理节点。现新增两个vm,分别加入原管理节点。一共三个node

步骤

将vm的hostname设为 swarmworker1swarmworker2,hostname 很重要,以后会用

直接作为worker加入前面建立的节点,输入前面初始化节点时出现的命令

1
2
3
4
5
6
7
8
docker swarm join --token SWMTKN-1-0mzmp1o6vufmqmpo8rvyrg2kzvlakj94gweg6drqunzhmvvmxr-6i80acmunj32z9xqw1jbbmeln 192.0.0.30:2377
# This node joined a swarm as a worker.

docker node ls
# ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
# xgjnkzvysal5fv7ugm1t5d7zr * swarmmanger Ready Active Leader 20.10.21
# e3ye8fep0dr3xkxrzdxz8wzjw swarmworker1 Ready Active 20.10.21
# i5vz5saiixnx82j9hpy4d5wmc swarmworker2 Ready Active 20.10.21

Task

什么是task

大意就是服务的执行者,一个服务通过多个task来执行服务

swarm的task之于service相当于:

面向对像的object之于class;

docker的container之于images;

一个服务可有多个task,并且通过内部算法,部署在一个或多个node上

处理各类任务的,也是这些task

设置task个数的参数为replicas,即复制品或副本…

那么我们来试一下

管理节点与工作节点的职能

现在,作为工作节点,我们来测试一下能不能管理创建服务

1
2
3
docker service create --replicas 1 --name hello_world_in_worker1 alpine ping docker.com
# response
#Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.

非管理节点不能干这些事,想要操作

要么在管理节点运行此命令

要么在管理节点升级此工作节点为管理节点

尝试升级,在当前的管理节点运行

1
2
3
4
5
docker node promote  swarmworker1
# Node swarmworker1 promoted to a manager in the swarm.

docker node promote swarmworker2
# Node swarmworker2 promoted to a manager in the swarm.

再去 swarmworker1查看节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
docker node  ls
# ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
# xgjnkzvysal5fv7ugm1t5d7zr * swarmmanger Ready Active Leader 20.10.21
# e3ye8fep0dr3xkxrzdxz8wzjw swarmworker1 Ready Active Reachable 20.10.21
# i5vz5saiixnx82j9hpy4d5wmc swarmworker2 Ready Active Reachable 20.10.21


## 查看当前节点的服务的task
docker node ps
#ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS

## 查看swarmmanger节点的服务的task
docker node ps swarmmanger
# ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
# mrf42qoc5b7j helloworld1.1 alpine:latest swarmmanger Running Running 42 minutes ago
# wgphxyqcc8xr \_ helloworld1.1 alpine:latest swarmmanger Shutdown Failed 42 minutes ago "No such container: helloworld…"
# tt34jmkj65l6 \_ helloworld1.1 alpine:latest swarmmanger Shutdown Failed 2 hours ago "No such container: helloworld…"
# lqp1x83vycb6 helloworld.1 alpine:latest swarmmanger Running Running 42 minutes ago
# 27sb11llxgte \_ helloworld.1 alpine:latest swarmmanger Shutdown Failed 42 minutes ago "No such container: helloworld…"
# y441dw76u89o \_ helloworld.1 alpine:latest swarmmanger Shutdown Failed 2 hours ago "No such container: helloworld…"

## 直接查看指定服务的task
docker service ps helloworld
# ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
# lqp1x83vycb6 helloworld.1 alpine:latest swarmmanger Running Running 44 minutes ago
# 27sb11llxgte \_ helloworld.1 alpine:latest swarmmanger Shutdown Failed 44 minutes ago "No such container: helloworld…"
# y441dw76u89o \_ helloworld.1 alpine:latest swarmmanger Shutdown Failed 2 hours ago "No such container: helloworld…"

另外,这个task貌似不能直接删。可以用update命令来限制单个服务的历史task的个数。超了应该会删

1
docker swarm update --task-history-limit 5

至此,本节点已升级成管理节点,试一下能不能创建服务

1
2
3
4
5
6
7
8
9
10
11
## 在本节点(swarmworker1)运行以下命令
docker service create --replicas 1 --name helloworld alpine ping docker.com
# 56n5w9g6r6lu7xr8irzi93i9x
# overall progress: 1 out of 1 tasks
# 1/1: running [==================================================>]
# verify: Service converged

## ps 一下,发现创建成功,但是却运行在swarmmanger节点上
docker service ps helloworld
# ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
# t31vj9qeausx helloworld.1 alpine:latest swarmmanger Running Running 18 seconds ago

小结

管理节点和工作节点都能运行task

工作节点不能操作,甚至不能查,只能用docker ps等这种本地命令来查看容器的运行情况

0%