目录
一.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节点
[email protected]:~# 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.
[email protected]:~#
[email protected]:~# 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
[email protected]:~#
2.worker节点加入集群
[email protected]:~# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
[email protected]:~#
[[email protected] ~]# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
[[email protected] ~]#
3.查看集群节点列表
[email protected]:~# 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
[email protected]:~#
4.给节点添加label
[email protected]:~# docker node update --label-add name=swarm01 centos10.yinzhengjie.com
centos10.yinzhengjie.com
[email protected]:~#
[email protected]:~# docker node update --label-add name=swarm02 ubuntu13.yinzhengjie.com
ubuntu13.yinzhengjie.com
[email protected]:~#
[email protected]:~# docker node update --label-add name=swarm03 web14.yinzhengjie.com
web14.yinzhengjie.com
[email protected]:~#
5.将work节点提升为manager角色以实现高可用
[email protected]:~# 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
[email protected]:~#
[email protected]:~#
[email protected]:~# docker node promote centos10.yinzhengjie.com
Node centos10.yinzhengjie.com promoted to a manager in the swarm.
[email protected]:~#
[email protected]:~#
[email protected]:~# docker node promote web14.yinzhengjie.com
Node web14.yinzhengjie.com promoted to a manager in the swarm.
[email protected]:~#
[email protected]:~# 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
[email protected]:~#
6.查看node信息
[email protected]:~# docker node inspect centos10.yinzhengjie.com
[
{
....
"Status": {
"State": "ready",
"Addr": "10.0.0.10"
},
"ManagerStatus": {
"Reachability": "reachable",
"Addr": "10.0.0.10:2377"
}
}
]
[email protected]:~#
7.创建overlay网络
(1)部署overlay网络环境
[email protected]:~# docker run -dp 8500:8500 --restart always --name=dev-consul -e CONSUL_BIND_INTERFACE=ens33 consul
# 以下配置"cluster-store"和"cluster-advertise"参数并不支持docker23.0.1哟,建议更换20.10系列的docker版本
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"],
"cluster-store": "consul://10.0.0.14:8500",
"cluster-advertise": "10.0.0.14:6666"
}
EOF
systemctl restart docker
(2)使用ovarlay创建
[email protected]:~# docker network create -d overlay --subnet=172.31.0.0/21 --gateway=172.31.7.254 --attachable yinzhengjie-network
nkndkvgns1uzoqbk60flc8308
[email protected]:~#
[email protected]:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
...
28kxwkcn4ho3 ingress overlay swarm
nkndkvgns1uz yinzhengjie-network overlay swarm
[email protected]:~#
温馨提示:
(1)docker 23.0.1版本貌似不支持 "cluster-store"和 "cluster-advertise"的配置,如果非要使用overlay网络,可以考虑使用20.10系列的docker哟。
(2)当然,我们也可以使用macvlan,Flannel,Calico等技术哟。
二.验证docker swarm集群
1.创建容器测试
[email protected]:~# 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
[email protected]:~#
2.查看service信息
[email protected]:~# docker service ls # 注意哈,它会监听所有worker节点的9999端口哟~
ID NAME MODE REPLICAS IMAGE PORTS
2b3d7adaws22 myweb2023 replicated 2/2 nginx:1.22.1-alpine *:9999->80/tcp
[email protected]:~#
[email protected]:~# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
...
LISTEN 0 4096 *:9999 *:*
[email protected]:~#
[email protected]:~# 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
[email protected]:~#
3.访问服务
[email protected]:~# 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
[email protected]:~#
温馨提示:
如果非要
三.验证服务的高可用性
1.查看服务
[email protected]:~# 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
[email protected]:~#
2.将服务"centos10.yinzhengjie.com"重启
[[email protected] ~]# reboot
3.再次查看服务,容器会自动在其他节点被拉起
[email protected]:~# 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
[email protected]:~#
[email protected]:~# 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
[email protected]:~#
标签:入门篇,root,swarm,yinzhengjie,docker,com,ubuntu13
From: https://www.cnblogs.com/yinzhengjie/p/17150043.html