swarm集群操作详解


承接上篇的集群搭建操作的结束之后,此篇讲解集群的相关操作命令

swarm集群常用操作

  • 写在前面:如果集群损坏切在不影响集群业务的下需要重置的
使用docker swarm init --force-new-cluste

节点信息查看

命令位置:集群节点
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active              Reachable           18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4

筛选查看命令

①只展示id
[root@swarm01 /]# docker node ls -q
2g6592p43eonilcjvivw3ww1f
x64fuzm4y70ov30i5txx327a9
hka4wtaqt05hsme3c5ycp2nmp
[root@swarm01 /]# 
②筛选满足条件(支持参数 name id)
[root@swarm01 /]# docker node ls -f name=swarm01
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4

修改节点

命令位置:集群节点

节点降低:manager-> worker
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active              Reachable           18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4
[root@swarm01 /]# docker node demote swarm02
Manager swarm02 demoted in the swarm.
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4

节点升级: worker -> manager
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4
[root@swarm01 /]# docker node promote swarm02
Node swarm02 promoted to a manager in the swarm.
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active              Reachable           18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4

指定参数修改
--advertise-addr: ip
--listen-addr: ip与端口
--availability: 节点的有效性("active"|"pause"|"drain")
--role:worker manager

[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active              Reachable           18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4
[root@swarm01 /]# docker node update --role worker swarm02
swarm02
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4

移除节点

移除节点完成操作
①命令位置: swarm03(准备移除的节点)
[root@swarm03 ~]# docker swarm leave
Node left the swarm.
[root@swarm03 ~]# 
②命令位置:swarm01(控制节点)
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Ready               Active                                  18.09.4
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
hka4wtaqt05hsme3c5ycp2nmp     swarm03             Down                Active                                  18.09.4
[root@swarm01 /]# docker node rm swarm03
swarm03
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
工作节点执行docker node leave之后在集群节点状态变为Down,这样在集群节点执行docker node rm便可以

如果需要强制移除节点,直接执行会报错
[root@swarm01 /]# docker node rm swarm03
Error response from daemon: rpc error: code = FailedPrecondition desc = node 2wo6hxhxa3loms06o3midxhdj is not down and can't be removed
使用强制参数
[root@swarm01 /]# docker node rm -f swarm03
swarm03
[root@swarm01 /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
2g6592p43eonilcjvivw3ww1f *   swarm01             Ready               Active              Leader              18.09.4
x64fuzm4y70ov30i5txx327a9     swarm02             Ready               Active                                  18.09.4
[root@swarm01 /]# 

部署应用

创建服务

docker service create
–detach , -d: 指定容器运行于前台还是后台,默认为false
–name: 服务名称
–network: 网络连接
–publish , -p: 端口映射
–env , -e: 设置环境变量
–tty , -t: 分配tty设备,该可以支持终端登录
–mount: 文件挂载
–replicas: 指定任务数量

拉取hello-world最新版本镜像作为演示demo
[root@swarm01 usr]# docker service create --replicas 3 -p 443:8080 --name hello \
> --mount type=bind,source=/storage/apiapp_log,destination=/go/src/logs \
> --mount type=bind,source=/root/apiapp/app.conf,destination=/root/app.conf \
> --mount type=bind,source=/root/apiapp/run_application.sh,destination=/root/run_application.sh \
> -e RUN_MODE=Production \
> -td hello-wrold:latest
image hello-wrold:latest could not be accessed on a registry to record
its digest. Each node will access hello-wrold:latest independently,
possibly leading to different nodes running different
versions of the image.

xfmlwf4eo9b27jlhrh889wooy
[root@swarm01 usr]# 

查看服务创建情况
[root@swarm01 usr]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                PORTS
xfmlwf4eo9b2        hello               replicated          0/3                 hello-wrold:latest   *:443->8080/tcp
[root@swarm01 usr]# 

服务参数修改(这里的镜像因为网速原因用的是最小的hello-world,错误地方暂且忽略)

docker service update [OPTIONS] SERVICE

[root@swarm01 /]# docker service update --replicas 2 hello
hello
overall progress: 0 out of 2 tasks 
1/2: preparing [=================================>                 ] 
2/2: preparing [=================================>                 ] 
service update paused: update paused due to failure or early termination of task 4yk09qa7z8rw11517ucmxgxku

针对任务数量修改,支持批量修改的命令为:docker service scale service=replicas。。。 支持批量操作

查看服务

[root@swarm01 ~]# docker service ls (options)
ID                  NAME                MODE                REPLICAS            IMAGE                PORTS
xfmlwf4eo9b2        hello               replicated          0/3                 hello-wrold:latest   *:443->8080/tcp

--filter 同样可以支持参数过滤操作

查看任务列表

docker service ps [OPTIONS] SERVICE [SERVICE...]  (支持filter过滤操作)【支持批量操作】
[再次声明,所有展示为了操作命令,运行状态是否正确这里不考虑]
[root@swarm01 /]# docker service ps hello
ID                  NAME                IMAGE                NODE                DESIRED STATE       CURRENT STATE                 ERROR                              PORTS
jfpqsqh46n4h        hello.1             hello-wrold:latest   swarm02             Running             Preparing 11 seconds ago                                         
vovkm6h5g4zk         \_ hello.1         hello-wrold:latest   swarm01             Shutdown            Rejected 12 seconds ago       "invalid mount config for type??   
unuabe9yy49l         \_ hello.1         hello-wrold:latest   swarm03             Shutdown            Rejected 38 seconds ago       "invalid mount config for type??   
l8apmw48cjiq         \_ hello.1         hello-wrold:latest   swarm02             Shutdown            Rejected about a minute ago   "invalid mount config for type??   
ue2057am4lps         \_ hello.1         hello-wrold:latest   swarm01             Shutdown            Rejected about a minute ago   "invalid mount config for type??   
k1i9e990753w        hello.2             hello-wrold:latest   swarm02             Running             Preparing 40 seconds ago                                         
2ezv34jno81a         \_ hello.2         hello-wrold:latest   swarm03             Shutdown            Rejected 41 seconds ago       "invalid mount config for type??   
k3qwlh6ocukc         \_ hello.2         hello-wrold:latest   swarm02             Shutdown            Rejected about a minute ago   "invalid mount config for type??   
h9jqy8coh3qd         \_ hello.2         hello-wrold:latest   swarm03             Shutdown            Rejected about a minute ago   "invalid mount config for type??   
nujear2hj645         \_ hello.2         hello-wrold:latest   swarm01             Shutdown            Rejected 2 minutes ago        "invalid mount config for type??   
hyhstj0fngak        hello.3             hello-wrold:latest   swarm02             Running             Preparing 15 seconds ago                                         
xeqe7xgebbd4         \_ hello.3         hello-wrold:latest   swarm01             Shutdown            Rejected 15 seconds ago       "invalid mount config for type??   
elmh2v12o16s         \_ hello.3         hello-wrold:latest   swarm03             Shutdown            Rejected 51 seconds ago       "invalid mount config for type??   
d7u4yxhu70db         \_ hello.3         hello-wrold:latest   swarm01             Shutdown            Rejected about a minute ago   "invalid mount config for type??   
vcwia4wqthfq         \_ hello.3         hello-wrold:latest   swarm02             Shutdown            Rejected about a minute ago   "invalid mount config for type??

查看服务详情

docker service inspect [OPTIONS] SERVICE [SERVICE...] 支持多个操作

[root@swarm01 /]# docker service inspect hello

删除服务

docker service rm SERVICE [SERVICE...]

[root@swarm01 /]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                PORTS
xfmlwf4eo9b2        hello               replicated          0/3                 hello-wrold:latest   *:443->8080/tcp
[root@swarm01 /]# docker service rm hello
hello
[root@swarm01 /]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
[root@swarm01 /]# 

查看服务日志

docker service logs [OPTIONS] SERVICE|TASK
--follow , -f: 跟随输出
--tail: 显示最新的N条日志
--since: 显示从某个时间开始的所有日志
--timestamps , -t: 显示时间戳

此篇文章建立在dokcer swarm集群搭建完成的基础纸上,可以参见地址:https://zhuanlan.zhihu.com/p/61535537

发布了88 篇原创文章 · 获赞 17 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_32112175/article/details/89091868