docker swarm快速入门篇

简介: 关于Docker Swarm集群部署和验证高可用性的快速入门教程。

一.docker swarm集群部署

主机名 操作系统 配置
centos10.yinzhengjie.com CentOS 7.9.2009 CPU: 2core,Memory: 4G,Disk: 50G+
ubuntu13.yinzhengjie.com Ubuntu 20.04.05 CPU: 2core,Memory: 4G,Disk: 50G+
ubuntu14.yinzhengjie.com Ubuntu 20.04.05 CPU: 2core,Memory: 4G,Disk: 50G+

1. 初始化manager节点

root@ubuntu13.yinzhengjie.com:~# docker swarm init --advertise-addr 10.0.0.13
Swarm initialized: current node (ophsn9oh8xzih7ofvbp0fkyet) is now a manager.

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

    docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377

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

root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker swarm join-token worker  # 查看worker节点加入到集群的token,其实上面的命令已经自带啦~
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377

root@ubuntu13.yinzhengjie.com:~#

2.worker节点加入集群

root@web14.yinzhengjie.com:~# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
root@web14.yinzhengjie.com:~# 

[root@centos10.yinzhengjie.com ~]# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
[root@centos10.yinzhengjie.com ~]#

3.查看集群节点列表

root@ubuntu13.yinzhengjie.com:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active                          23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active                          23.0.1
root@ubuntu13.yinzhengjie.com:~#

4.给节点添加label

root@ubuntu13.yinzhengjie.com:~# docker node update --label-add name=swarm01 centos10.yinzhengjie.com
centos10.yinzhengjie.com
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker node update --label-add name=swarm02 ubuntu13.yinzhengjie.com
ubuntu13.yinzhengjie.com
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker node update --label-add name=swarm03 web14.yinzhengjie.com
web14.yinzhengjie.com
root@ubuntu13.yinzhengjie.com:~#

5.将work节点提升为manager角色以实现高可用

root@ubuntu13.yinzhengjie.com:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active                          23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active                          23.0.1
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker node promote centos10.yinzhengjie.com
Node centos10.yinzhengjie.com promoted to a manager in the swarm.
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker node promote web14.yinzhengjie.com
Node web14.yinzhengjie.com promoted to a manager in the swarm.
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active         Reachable        23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active         Reachable        23.0.1
root@ubuntu13.yinzhengjie.com:~#

6.查看node信息

root@ubuntu13.yinzhengjie.com:~# docker node inspect centos10.yinzhengjie.com
[
    {
        ....
        "Status": {
            "State": "ready",
            "Addr": "10.0.0.10"
        },
        "ManagerStatus": {
            "Reachability": "reachable",
            "Addr": "10.0.0.10:2377"
        }
    }
]
root@ubuntu13.yinzhengjie.com:~#

二.验证docker swarm集群

1.创建容器测试

root@ubuntu13.yinzhengjie.com:~# docker service create --replicas 2 -p 9999:80 --network yinzhengjie-network --name myweb2023 nginx:1.22.1-alpine 
2b3d7adaws22am7u32y38zd0p
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 
root@ubuntu13.yinzhengjie.com:~#

2.查看service信息

root@ubuntu13.yinzhengjie.com:~# docker service ls   # 注意哈,它会监听所有worker节点的9999端口哟~
ID             NAME        MODE         REPLICAS   IMAGE                 PORTS
2b3d7adaws22   myweb2023   replicated   2/2        nginx:1.22.1-alpine   *:9999->80/tcp
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# ss -ntl
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port      Process      
...                     
LISTEN      0           4096                         *:9999                          *:*                                          
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker service ps myweb2023  # 查看容器在哪个worker节点上。 
ID             NAME          IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
z9qohpe9j0qs   myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Running         Running less than a second ago             
wh7jgc61amsz   myweb2023.2   nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 3 minutes ago                      
root@ubuntu13.yinzhengjie.com:~#

3.访问服务

root@ubuntu13.yinzhengjie.com:~# curl -I 10.0.0.13:9999
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Fri, 24 Feb 2023 07:28:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 19 Oct 2022 10:49:37 GMT
Connection: keep-alive
ETag: "634fd641-267"
Accept-Ranges: bytes

root@ubuntu13.yinzhengjie.com:~#

三.验证服务的高可用性

1.查看服务

root@ubuntu13.yinzhengjie.com:~# docker service ps 2b3d7adaws22
ID             NAME          IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
z9qohpe9j0qs   myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Running         Running less than a second ago             
wh7jgc61amsz   myweb2023.2   nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 8 minutes ago                      
root@ubuntu13.yinzhengjie.com:~#

2.将服务"centos10.yinzhengjie.com"重启

[root@centos10.yinzhengjie.com ~]# reboot

3.再次查看服务,容器会自动在其他节点被拉起

root@ubuntu13.yinzhengjie.com:~# docker service ps 2b3d7adaws22
ID             NAME              IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
qmxthyaq4pra   myweb2023.1       nginx:1.22.1-alpine   ubuntu13.yinzhengjie.com   Ready           Ready 3 seconds ago                        
z9qohpe9j0qs    \_ myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Shutdown        Running less than a second ago             
wh7jgc61amsz   myweb2023.2       nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 9 minutes ago                      
root@ubuntu13.yinzhengjie.com:~# 
root@ubuntu13.yinzhengjie.com:~# docker service ps 2b3d7adaws22  # 过了一端时间后,注意观察"myweb2023.1"状态从"Ready"变更为"Running"
ID             NAME              IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                     ERROR     PORTS
qmxthyaq4pra   myweb2023.1       nginx:1.22.1-alpine   ubuntu13.yinzhengjie.com   Running         Running 3 minutes ago                       
z9qohpe9j0qs    \_ myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Shutdown        Shutdown less than a second ago             
wh7jgc61amsz   myweb2023.2       nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 12 minutes ago                      
root@ubuntu13.yinzhengjie.com:~#
目录
相关文章
|
24天前
|
Prometheus 监控 Cloud Native
如何使用Prometheus监控Docker Swarm集群的资源使用情况?
还可以根据实际需求进行进一步的配置和优化,如设置告警规则,当资源使用超出阈值时及时发出警报。通过这些步骤,能够有效地使用 Prometheus 对 Docker Swarm 集群的资源进行监控和管理。
41 8
|
24天前
|
Prometheus 监控 Cloud Native
如何监控Docker Swarm集群的性能?
如何监控Docker Swarm集群的性能?
80 8
|
24天前
|
调度 开发者 Docker
Docker Swarm
Docker Swarm 为容器化应用的部署和管理提供了一种高效、可靠的方式,使开发者能够更轻松地构建和运行分布式应用。随着容器技术的不断发展,Docker Swarm 在企业级应用中的应用也将越来越广泛。
48 8
|
24天前
|
监控 Docker 容器
Docker Swarm集群的扩展与缩容策略,涵盖其意义、方法、步骤及注意事项
本文深入探讨了Docker Swarm集群的扩展与缩容策略,涵盖其意义、方法、步骤及注意事项,旨在帮助用户高效管理集群资源,适应业务变化,确保服务稳定性和资源优化。
46 6
|
1月前
|
关系型数据库 MySQL Java
【Docker最新版教程】一文带你快速入门Docker常见用法,实现容器编排和自动化部署上线项目
Docker快速入门到项目部署,MySQL部署+Nginx部署+docker自定义镜像+docker网络+DockerCompose项目实战一文搞定!
|
1月前
|
Kubernetes 负载均衡 调度
Docker Swarm 核心概念及详细使用
Docker Swarm 是 Docker 的原生集群管理工具,用于将多个 Docker 主机整合成一个虚拟主机,提供集群管理和调度功能。其核心特点包括集群管理、容错与高可用性、负载均衡、声明式服务模型、服务发现和安全性。本文档详细介绍了 Docker Swarm 的安装配置、服务部署、节点管理、网络配置及故障模拟等关键操作,适用于中小型项目或对 Kubernetes 复杂性有所顾虑的用户。
100 6
|
1月前
|
应用服务中间件 nginx Docker
Docker Swarm、Docker Stack和Portainer的使用
Docker Swarm、Docker Stack 和 Portainer 各有其独特的功能和优势。Docker Swarm 适用于分布式服务的管理和编排,Docker Stack 便于多容器应用的定义和部署,而 Portainer 提供了直观的 UI,简化了 Docker 环境的管理。结合使用这些工具,可以大大提高容器化应用的部署和管理效率。希望本文对您理解和应用这些工具有所帮助。
88 5
|
2月前
|
负载均衡 应用服务中间件 网络安全
docker swarm添加更多的服务
【10月更文挑战第16天】
30 6
|
1月前
|
API Docker 容器
【赵渝强老师】构建Docker Swarm集群
本文介绍了如何使用三台虚拟主机构建Docker Swarm集群。首先在master节点上初始化集群,然后通过特定命令将node1和node2作为worker节点加入集群。最后,在master节点上查看集群的节点信息,确认集群构建成功。文中还提供了相关图片和视频教程,帮助读者更好地理解和操作。
|
1月前
|
调度 Docker 容器
【赵渝强老师】Docker Swarm集群的体系架构
Docker Swarm自1.12.0版本起集成至Docker引擎,无需单独安装。它内置服务发现功能,支持跨多服务器或宿主机创建容器,形成集群提供服务。相比之下,Docker Compose仅限于单个宿主机。Docker Swarm采用主从架构,Swarm Manager负责管理和调度集群中的容器资源,用户通过其接口发送指令,Swarm Node根据指令创建容器运行应用。