Swarm-环境搭建

Swarm-环境搭建

系统规划

https://fantasy-ke.github.io/picx-images-hosting/docker/image-20220726154555016.39l7gwav0x.webp

环境准备

创建服务器

新建三台虚拟机

节点 IP 系统 配置 存储 服务
manager 192.168.2.xx CentOS7.6 8U16G 100G redis,frp,mssql
worker1 192.168.2.xx CentOS7.6 4U8G 80G app
worker2 192.168.2.xx CentOS7.6 4U8G 80G app

时间同步

时间同步任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
yum install -y ntp

cat <<EOF>>/var/spool/cron/root
# 12点同步
00 12 * * * /usr/sbin/ntpdate -u ntp1.aliyun.com && /usr/sbin/hwclock -w
# 23:59分执行删除
59 23 * * * docker image prune -af
EOF

##查看计划任务
crontab -l

##手动执行
/usr/sbin/ntpdate -u ntp1.aliyun.com && /usr/sbin/hwclock -w

Docker

启动docker

1
2
sudo systemctl start docker
sudo systemctl enable docker

防火墙

Error response from daemon: rpc error: code = Unavailable desc = connection error: desc = “transport: Error while dialing dial tcp 192.168.2.61:2377: connect: no route to host”

打开防火墙

1
2
3
4
5
6
7
8
9
10
11
12
# manager
firewall-cmd --zone=public --add-port=2377/tcp --permanent

# 所有node
firewall-cmd --zone=public --add-port=7946/tcp --permanent
firewall-cmd --zone=public --add-port=7946/udp --permanent
firewall-cmd --zone=public --add-port=4789/tcp --permanent
firewall-cmd --zone=public --add-port=4789/udp --permanent

# 所有node
firewall-cmd --reload
systemctl restart docker

要在 swarm 集群中使用使用路由网格,首先需要开启加入swarm集群的节点的以下端口:

  • 2377 :主节点监听端口
  • 7946 :容器网络发现
  • 4789 :容器网络入口

Swarm

创建Swarm

创建Swarm

1
docker swarm init --advertise-addr your_manager_ip

https://fantasy-ke.github.io/picx-images-hosting/docker/image-20220726145701895.6m3xb9smof.webp

1
2
3
4
5
6
7
8
[root@manager ~]# docker swarm init --advertise-addr 192.168.2.61
Swarm initialized: current node (rzds6oyb0bgvudzegpscmgiz3) is now a manager.

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

docker swarm join --token SWMTKN-1-51b7t8whxn8j6mdjt5perjmec9u8qguxq8tern9nill737pra2-ejc5nw5f90oz6xldcbmrl2ztu 192.168.2.61:2377

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

加入Swarm

1
docker swarm join --token SWMTKN-1-51b7t8whxn8j6mdjt5perjmec9u8qguxq8tern9nill737pra2-ejc5nw5f90oz6xldcbmrl2ztu 192.168.2.61:2377

https://fantasy-ke.github.io/picx-images-hosting/docker/image-20220726145636086.3uuv37756b.webp

查看节点

1
2
3
4
5
[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
rzds6oyb0bgvudzegpscmgiz3 * manager Ready Active Leader 20.10.17
rjj3fr5uazywwsfj6ok3f3fw3 worker1 Ready Active 20.10.17
6vkvdm3gcxip8htc6cfk8bm3n worker2 Ready Active 20.10.17

服务约束

添加label

1
2
sudo docker node update --label-add role=env manager
sudo docker node update --label-add gct_medpro=gct_medpro zhuji

https://fantasy-ke.github.io/picx-images-hosting/docker/image-20220726162645325.99tdlmmgzk.webp

给节点也加上

1
2
3
4
[root@manager ~]# sudo docker node update --label-add role=app worker1
worker1
[root@manager ~]# sudo docker node update --label-add role=app worker2
worker2