【Docker Swarm】搭建Docker Swarm高可用集群(详细版)(下)

简介: 【Docker Swarm】搭建Docker Swarm高可用集群(详细版)

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 ~]#



🔟参 考 链 接


🔴群模式概述 |Docker 文档


🟠群模式入门|Docker 文档


🟡群模式关键概念|Docker 文档


🟢节点如何|Docker 文档


🔵创建群|Docker 文档


🟣docker node ls |Docker 文档

相关文章
|
19天前
|
Prometheus 监控 Cloud Native
如何使用Prometheus监控Docker Swarm集群的资源使用情况?
还可以根据实际需求进行进一步的配置和优化,如设置告警规则,当资源使用超出阈值时及时发出警报。通过这些步骤,能够有效地使用 Prometheus 对 Docker Swarm 集群的资源进行监控和管理。
37 8
|
19天前
|
Prometheus 监控 Cloud Native
如何监控Docker Swarm集群的性能?
如何监控Docker Swarm集群的性能?
67 8
|
19天前
|
调度 开发者 Docker
Docker Swarm
Docker Swarm 为容器化应用的部署和管理提供了一种高效、可靠的方式,使开发者能够更轻松地构建和运行分布式应用。随着容器技术的不断发展,Docker Swarm 在企业级应用中的应用也将越来越广泛。
42 8
|
20天前
|
监控 Docker 容器
Docker Swarm集群的扩展与缩容策略,涵盖其意义、方法、步骤及注意事项
本文深入探讨了Docker Swarm集群的扩展与缩容策略,涵盖其意义、方法、步骤及注意事项,旨在帮助用户高效管理集群资源,适应业务变化,确保服务稳定性和资源优化。
42 6
|
1月前
|
Kubernetes 负载均衡 调度
Docker Swarm 核心概念及详细使用
Docker Swarm 是 Docker 的原生集群管理工具,用于将多个 Docker 主机整合成一个虚拟主机,提供集群管理和调度功能。其核心特点包括集群管理、容错与高可用性、负载均衡、声明式服务模型、服务发现和安全性。本文档详细介绍了 Docker Swarm 的安装配置、服务部署、节点管理、网络配置及故障模拟等关键操作,适用于中小型项目或对 Kubernetes 复杂性有所顾虑的用户。
79 6
|
11天前
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
142 77
|
19天前
|
监控 Docker 容器
在Docker容器中运行打包好的应用程序
在Docker容器中运行打包好的应用程序
|
13天前
|
数据建模 应用服务中间件 nginx
docker替换宿主与容器的映射端口和文件路径
通过正确配置 Docker 的端口和文件路径映射,可以有效地管理容器化应用程序,确保其高效运行和数据持久性。在生产环境中,动态替换映射配置有助于灵活应对各种需求变化。以上方法和步骤提供了一种可靠且易于操作的方案,帮助您轻松管理 Docker 容器的端口和路径映射。
54 3
|
20天前
|
存储 缓存 监控
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
54 7
|
20天前
|
存储 Prometheus 监控
Docker容器内进行应用调试与故障排除的方法与技巧,包括使用日志、进入容器检查、利用监控工具及检查配置等,旨在帮助用户有效应对应用部署中的挑战,确保应用稳定运行
本文深入探讨了在Docker容器内进行应用调试与故障排除的方法与技巧,包括使用日志、进入容器检查、利用监控工具及检查配置等,旨在帮助用户有效应对应用部署中的挑战,确保应用稳定运行。
29 5
下一篇
DataWorks