6️⃣更改角色:将Worker晋升为Manager
以docker-n2为例,将docker-n2管理节点由
worker角色
变成manager角色
。
[root@docker-m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 * docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Ready Active 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14 [root@docker-m1 ~]# docker node update --role manager docker-n2 docker-n2 [root@docker-m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 * docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Ready Active 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active Reachable 20.10.14
7️⃣移除再添加管理节点
将集群中某台管理节点移除集群,重新获取
管理节点的令牌
,再添加至集群中。
# 查看帮助命令 [root@docker-m1 ~]# docker swarm leave --help Usage: docker swarm leave [OPTIONS] Leave the swarm Options: -f, --force Force this node to leave the swarm, ignoring warnings [root@docker-m1 ~]#
在docker-m3节点执行操作,将docker-m3管理节点移除集群
[root@docker-m3 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh * docker-m3 Ready Active Reachable 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14 [root@docker-m3 ~]# docker swarm leave -f Node left the swarm.
在docker-m1管理节点上查看。发现docker-m3管理节点已经关闭
[root@docker-m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 * docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Ready Active Reachable 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14 [root@docker-m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 * docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Down Active Unreachable 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14
重新获取添加管理节点的令牌命令。
执行
docker swarm join-token manager
命令,获取命令。
[root@docker-m1 ~]# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-528o8bfk061miheduvuvnnohhpystvxnwiqfqqf04gou6n1wmz-1z6k8msio37as0vaa467glefx 192.168.200.81:2377 [root@docker-m1 ~]#
重新将docker-m3管理节点添加到集群中。
[root@docker-m3 ~]# docker swarm join --token SWMTKN-1-528o8bfk061miheduvuvnnohhpystvxnwiqfqqf04gou6n1wmz-1z6k8msio37as0vaa467glefx 192.168.200.81:2377 This node joined a swarm as a manager. [root@docker-m3 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Down Active Reachable 20.10.14 jvtiwv8eu45ev4qbm0ausivv2 * docker-m3 Ready Active Reachable 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Ready Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14 [root@docker-m3 ~]#
8️⃣移除再添加工作节点
将集群中某台工作节点移除集群,重新获取
工作节点的令牌
,再添加至集群中。
在docker-n1节点执行操作,将docker-n1工作节点移除集群
[root@docker-n1 ~]# docker swarm leave Node left the swarm.
在docker-m1管理节点上查看。发现docker-n1工作节点已经关闭
[root@docker-m1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 34cug51p9dw83u2np594z6ej4 * docker-m1 Ready Active Leader 20.10.14 hwmwdk78u3rx0wwxged87xnun docker-m2 Ready Active Reachable 20.10.14 4q34guc6hp2a5ok0g1zkjojyh docker-m3 Down Active Reachable 20.10.14 jvtiwv8eu45ev4qbm0ausivv2 docker-m3 Ready Active Reachable 20.10.14 4om9sg56sg09t9whelbrkh8qn docker-n1 Down Active 20.10.14 xooolkg0g9epddfqqiicywshe docker-n2 Ready Active 20.10.14
重新获取添加工作节点的令牌命令。
执行
docker swarm join-token worker
命令,获取命令。
[root@docker-m1 ~]# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-528o8bfk061miheduvuvnnohhpystvxnwiqfqqf04gou6n1wmz-3ixu6we70ghk69wghfrmo0y6a 192.168.200.81:2377 [root@docker-m1 ~]#
重新将docker-n1工作节点添加到集群中。
[root@docker-n1 ~]# docker swarm join --token SWMTKN-1-528o8bfk061miheduvuvnnohhpystvxnwiqfqqf04gou6n1wmz-3ixu6we70ghk69wghfrmo0y6a 192.168.200.81:2377 This node joined a swarm as a worker.
删除多余的节点。
[root@docker-m1 ~]# docker node rm 34emdxnfc139d6kc4ht2xsp4b 4om9sg56sg09t9whelbrkh8qn 34emdxnfc139d6kc4ht2xsp4b 4om9sg56sg09t9whelbrkh8qn [root@docker-m1 ~]#
9️⃣在集群中部署NGINX应用测试
🏉 查看service帮助命令
# 查看service 帮助命令 [root@docker-m1 ~]# docker service 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. [root@docker-m1 ~]#
⚾️ 创建NGINX服务
# 1、搜索镜像 [root@docker-m1 ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 16720 [OK] bitnami/nginx Bitnami nginx Docker Image 124 [OK] ubuntu/nginx Nginx, a high-performance reverse proxy & we… 46 bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 17 [OK] rancher/nginx-ingress-controller 10 ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4 bitnami/nginx-ldap-auth-daemon 3 bitnami/nginx-exporter 2 rancher/nginx-ingress-controller-defaultbackend 2 circleci/nginx This image is for internal use 2 vmware/nginx 2 vmware/nginx-photon 1 bitnami/nginx-intel 1 rancher/nginx 1 wallarm/nginx-ingress-controller Kubernetes Ingress Controller with Wallarm e… 1 rancher/nginx-conf 0 rancher/nginx-ssl 0 ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0 rancher/nginx-ingress-controller-amd64 0 continuumio/nginx-ingress-ws 0 ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0 kasmweb/nginx An Nginx image based off nginx:alpine and in… 0 rancher/nginx-proxy 0 wallarm/nginx-ingress-controller-amd64 Kubernetes Ingress Controller with Wallarm e… 0 ibmcom/nginx-ingress-controller-amd64 0 [root@docker-m1 ~]# # 2、下载镜像 pull [root@docker-m1 ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@docker-m1 ~]# # 3、查看镜像 [root@docker-m1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 4 months ago 141MB # 4、使用service命令启动Nginx docker run 容器启动,不具有扩缩容器。 docker service 服务启动,具有扩缩容,滚动更新。 [root@docker-m1 ~]# docker service create -p 8888:80 --name xybdiy-nginx nginx ngoi21hcjan5qoro9amd7n1jh overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged [root@docker-m1 ~]#
🏀 查看NGINX服务
发现nginx服务被部署到了docker-n2 工作节点上。随机分布。
# 查看NGINX服务 [root@docker-m1 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ngoi21hcjan5 xybdiy-nginx replicated 1/1 nginx:latest *:8888->80/tcp [root@docker-m1 ~]# docker service ps xybdiy-nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w5azhbc3xrta xybdiy-nginx.1 nginx:latest docker-n2 Running Running 20 minutes ago [root@docker-m1 ~]# [root@docker-n2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d65e6e8bf5fd nginx:latest "/docker-entrypoint.…" 28 minutes ago Up 28 minutes 80/tcp xybdiy-nginx.1.w5azhbc3xrtafxvftkgh7x9vk [root@docker-n2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d65e6e8bf5fd nginx:latest "/docker-entrypoint.…" 28 minutes ago Up 28 minutes 80/tcp xybdiy-nginx.1.w5azhbc3xrtafxvftkgh7x9vk # 查看NGNIX服务详细信息 [root@docker-m1 ~]# docker service inspect xybdiy-nginx [ { "ID": "ngoi21hcjan5qoro9amd7n1jh", "Version": { "Index": 34 }, "CreatedAt": "2022-05-03T12:38:22.234486876Z", "UpdatedAt": "2022-05-03T12:38:22.238903441Z", "Spec": { "Name": "xybdiy-nginx", "Labels": {}, "TaskTemplate": { "ContainerSpec": { "Image": "nginx:latest@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31", "Init": false, "StopGracePeriod": 10000000000, "DNSConfig": {}, "Isolation": "default" }, "Resources": { "Limits": {}, "Reservations": {} }, "RestartPolicy": { "Condition": "any", "Delay": 5000000000, "MaxAttempts": 0 }, "Placement": { "Platforms": [ { "Architecture": "amd64", "OS": "linux" }, { "OS": "linux" }, { "OS": "linux" }, { "Architecture": "arm64", "OS": "linux" }, { "Architecture": "386", "OS": "linux" }, { "Architecture": "mips64le", "OS": "linux" }, { "Architecture": "ppc64le", "OS": "linux" }, { "Architecture": "s390x", "OS": "linux" } ] }, "ForceUpdate": 0, "Runtime": "container" }, "Mode": { "Replicated": { "Replicas": 1 } }, "UpdateConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "RollbackConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "EndpointSpec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8888, "PublishMode": "ingress" } ] } }, "Endpoint": { "Spec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8888, "PublishMode": "ingress" } ] }, "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8888, "PublishMode": "ingress" } ], "VirtualIPs": [ { "NetworkID": "uhjulzndxnofx63e2bb3r8iq9", "Addr": "10.0.0.7/24" } ] } } ] [root@docker-m1 ~]#
🏈 创建多个NGINX服务副本
动态扩容,缓解主机被访问的压力。
查看update帮助命令
[root@docker-m1 ~]# docker service update --help Usage: docker service update [OPTIONS] SERVICE Update a service Options: ...... -q, --quiet Suppress progress output --read-only Mount the container's root filesystem as read only --replicas uint Number of tasks --replicas-max-per-node uint Maximum number of tasks per node (default 0 = unlimited) ......
创建多个NGINX服务副本
[root@docker-m1 ~]# docker service update --replicas 2 xybdiy-nginx xybdiy-nginx overall progress: 2 out of 2 tasks 1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged [root@docker-m1 ~]#
查看创建的NGINX服务副本
[root@docker-m1 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ngoi21hcjan5 xybdiy-nginx replicated 2/2 nginx:latest *:8888->80/tcp [root@docker-m1 ~]# docker service ps xybdiy-nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w5azhbc3xrta xybdiy-nginx.1 nginx:latest docker-n2 Running Running 36 minutes ago rgtjq163z9ch xybdiy-nginx.2 nginx:latest docker-m1 Running Running 33 seconds ago
测试访问NGINX服务
http://192.168.200.81:8888/ http://192.168.200.91:8888/
🏐 模拟故障情况
当docker-m1管理主机发生宕机时,查看NGINX服务是否能够正常运行访问。
# 关闭docker-m1节点 [root@docker-m1 ~]# shutdown -h now Connection to 192.168.200.81 closed by remote host. Connection to 192.168.200.81 closed.
查看节点状态
[root@docker-m2 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 75dxq2qmzr2bv4tkg20gh0syr docker-m1 Down Active Unreachable 20.10.14 l2is4spmgd4b5xmmxwo3jvuf4 * docker-m2 Ready Active Reachable 20.10.14 u89a2ie2buxuc5bsew4a2wrpo docker-m3 Ready Active Leader 20.10.14 aon2nakgk87rds5pque74itw4 docker-n1 Ready Active 20.10.14 ljdb9d3xkzjruuxsxrpmuei7s docker-n2 Ready Active 20.10.14 [root@docker-m2 ~]#
查看服务状态
[root@docker-m2 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS ngoi21hcjan5 xybdiy-nginx replicated 3/2 nginx:latest *:8888->80/tcp [root@docker-m2 ~]# docker service ps xybdiy-nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w5azhbc3xrta xybdiy-nginx.1 nginx:latest docker-n2 Running Running 2 minutes ago tteb16dnir6u xybdiy-nginx.2 nginx:latest docker-n1 Running Running 2 minutes ago rgtjq163z9ch \_ xybdiy-nginx.2 nginx:latest docker-m1 Shutdown Running 17 minutes ago [root@docker-m2 ~]#