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:~#
目录
相关文章
|
5天前
|
负载均衡 应用服务中间件 网络安全
docker swarm添加更多的服务
【10月更文挑战第16天】
13 6
|
5天前
|
Docker 容器
docker swarm启动服务并连接到网络
【10月更文挑战第16天】
10 5
|
5天前
|
调度 Docker 容器
docker swarm创建覆盖网络
【10月更文挑战第16天】
9 5
|
4天前
|
存储 Kubernetes C++
Kubernetes VS Docker Swarm:哪个容器编排工具更适合你?
随着容器技术的快速发展,容器编排工具成为了现代软件开发和运维的重要环节。在众多容器编排工具中,Kubernetes和Docker Swarm无疑是最受欢迎的两个。本文将从技术特性、易用性和社区支持三个方面,对Kubernetes和Docker Swarm进行比较,以帮助您选择更适合您需求的容器编排工具。
17 3
|
6天前
|
负载均衡 网络协议 关系型数据库
docker swarm 使用网络启动服务
【10月更文挑战第15天】
13 4
|
6天前
|
Docker 容器
|
6天前
|
应用服务中间件 nginx Docker
docker swarm创建覆盖网络
【10月更文挑战第14天】
11 3
|
6天前
|
数据安全/隐私保护 Docker 容器
docker swarm创建网络
【10月更文挑战第15天】
7 1
|
6天前
|
Docker 容器
docker swarm 在服务中使用网络
【10月更文挑战第14天】
9 2
|
3天前
|
负载均衡 安全 调度
深入调查研究Docker Swarm
【10月更文挑战第19天】
8 0