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:~#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/17150043.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。