docker学习(九)docker swarm集群

钮实
2023-12-01


前言

Docker Swarm是Docker原生态的集群技术,swarm 集群由管理节点(manager)和工作节点(work node)组成。

swarm mananger:主节点,负责整个集群的管理工作包括集群配置、服务管理等所有跟集群有关的工作;

work node:工作节点,主要负责运行相应的服务来执行任务(task)。


一、环境准备

1、安装docker

2、准备两台机器

manager 172.16.10.151

node 172.16.10.153

二、安装主节点

安装主节点,生成token,其他工作节点使用该token加入集群

[root@swarm-master ~]# docker swarm init --advertise-addr 172.16.10.151
Swarm initialized: current node (yyjg5a442rncjuh58xdsel5a3) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4mi806upco3hjeiuu4c8boeehml9fuo5u7mnnode3o23h7ljuj-44nnu8s9b93vxob29tix73vhc 172.16.10.151:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

查看集群

[root@swarm-master ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
yyjg5a442rncjuh58xdsel5a3 *   swarm-master          Ready               Active              Leader              18.06.3-ce

三、安装从节点

使用上面的token

[root@swarm-node02 ~]# docker swarm join --token SWMTKN-1-4mi806upco3hjeiuu4c8boeehml9fuo5u7mnnode3o23h7ljuj-44nnu8s9b93vxob29tix73vhc 172.16.10.151:2377
This node joined a swarm as a worker.

在主节点上查看集群

[root@swarm-master ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
yyjg5a442rncjuh58xdsel5a3 *   swarm-master          Ready               Active              Leader              18.06.3-ce
y35ggyr28c1q2ltolvhva8d82     swarm-node02          Ready               Active                                  20.10.11

四、常用命令

1、docker node

[root@swarm-master ~]# docker node --help

Usage:	docker node COMMAND

Manage Swarm nodes

Commands:
  demote      Demote one or more nodes from manager in the swarm 将管理节点降级为普通节点
  inspect     Display detailed information on one or more nodes  查看节点的详细信息
  ls          List nodes in the swarm  查看节点列表
  promote     Promote one or more nodes to manager in the swarm 将普通节点升级为管理节点
  ps          List tasks running on one or more nodes, defaults to current node  查看运行的任务
  rm          Remove one or more nodes from the swarm  从集群中删除节点
  update      Update a node  更新节点状态
 
Run 'docker node COMMAND --help' for more information on a command.

2、docker service

[root@swarm-master ~]# docker service --help

Usage:	docker service COMMAND

Manage services

Commands:
  create      Create a new service  创建新的服务 
  inspect     Display detailed information on one or more services 查看服务详细信息
  logs        Fetch the logs of a service or task  查看服务日志
  ls          List services  查看服务列表
  ps          List the tasks of one or more services 查看服务任务信息 
  rm          Remove one or more services 删除服务
  rollback    Revert changes to a service's configuration 回滚服务
  scale       Scale one or multiple replicated services   扩容缩容
  update      Update a service  更新服务

Run 'docker service COMMAND --help' for more information on a command.

五、测试

1、下载nginx镜像

[root@swarm-master ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e5ae68f74026: Pull complete 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest

2、创建服务

[root@swarm-master ~]# docker service create --replicas 1 --name nginx-test nginx:latest
k8r71cut7ofe26r26rha4b3ny
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged

3、查看服务

[root@swarm-master ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
k8r71cut7ofe        nginx-test          replicated          1/1                 nginx:latest        

4、查看任务

[root@swarm-master ~]# docker service ps nginx-test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
4lsajhzyk84o        nginx-test.1        nginx:latest        swarm-master          Running             Running 5 minutes ago          

5、扩容

[root@swarm-master ~]# docker service scale nginx-test=2
nginx-test scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 

再次查看任务,可以看出两个任务分别运行在两个节点上

[root@swarm-master ~]# docker service ps nginx-test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
4lsajhzyk84o        nginx-test.1        nginx:latest        swarm-master          Running             Running 10 minutes ago                       
d3vrxeuc61o9        nginx-test.2        nginx:latest        swarm-node02          Running             Running 58 seconds ago          

6、更新服务

[root@swarm-master ~]# docker service update --image=nginx:1.21 nginx-test
nginx-test
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 

查看服务,镜像已经更新成 nginx:1.21

[root@swarm-master ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
k8r71cut7ofe        nginx-test          replicated          2/2                 nginx:1.21          

[root@swarm-master ~]# docker service ps nginx-test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
t3uqo4hdnapt        nginx-test.1        nginx:1.21          swarm-master          Running             Running 28 seconds ago                        
4lsajhzyk84o         \_ nginx-test.1    nginx:latest        swarm-master          Shutdown            Shutdown 28 seconds ago                       
6aiwhotqeuoj        nginx-test.2        nginx:1.21          swarm-node02          Running             Running 25 seconds ago                        
d3vrxeuc61o9         \_ nginx-test.2    nginx:latest        swarm-node02          Shutdown            Shutdown 26 seconds ago       

7、服务回滚

[root@swarm-master ~]# docker service update --rollback nginx-test
nginx-test
rollback: manually requested rollback 
overall progress: rolling back update: 2 out of 2 tasks 
1/2: running   [>                                                  ] 
2/2: running   [>                                                  ] 
verify: Service converged 

查看服务,镜像已经更新成 nginx:latest

[root@swarm-master ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
k8r71cut7ofe        nginx-test          replicated          2/2                 nginx:latest        

[root@swarm-master ~]# docker service ps -f "desired-state=running" nginx-test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
ayvu09m4yjuh        nginx-test.1        nginx:latest        swarm-master          Running             Running about a minute ago                       
kukxp1q8pmo7        nginx-test.2        nginx:latest        swarm-node02          Running             Running about a minute ago  

8、排除节点

排除主节点

docker node update --availability drain swarm-master

查看任务,都运行在了node节点上

[root@swarm-master ~]# docker service ps -f "desired-state=running" nginx-test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
kxvko9n3ct72        nginx-test.1        nginx:latest        swarm-node02          Running             Running 4 minutes ago                        
kukxp1q8pmo7        nginx-test.2        nginx:latest        swarm-node02          Running             Running 11 minutes ago

9、激活节点

docker node update --availability active swarm-master
 类似资料: