Docker Swarm 搭建:
主机名 | 系统版本 | IP地址 | 功能 |
swarm-1 | centos7.9 | 192.168.17.10 | 管理节点 |
swarm-2 | centos7.9 | 192.168.17.20 | 工作节点 |
swarm-3 | centos7.9 | 192.168.17.30 | 工作节点 |
CPU、Memory、Disk不做要求。
准备IP地址和主机名:
hostnamectl set-hostname swarm-x
ntp:
crontab -e
0 */1 * * * ntpdate time1.aliyun.com
关闭防火墙:
systemctl stop firewalld && systemctl disable firewalld
关闭SELinux:
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
如果第一次配置,需要重启系统。
虚拟机模板已经进行了处理。
安装Docker:
参考我的博客文章,直接安装。
如果遇到了需要用到本地仓库,需要修改docker daemon:
Docker Swarm集群部署:
角色划分:
swarm-1 | swarm-2 | swarm-3 |
master(manager node) | worker | worker |
集群初始化:
[root@swarm-1 ~]# docker swarm --help
Usage: docker swarm COMMAND
Manage Swarm
Commands:
init Initialize a swarm
join Join a swarm as a node and/or manager
Run 'docker swarm COMMAND --help' for more information on a command.
[root@swarm-1 ~]# docker swarm init --help
Usage: docker swarm init [OPTIONS]
Initialize a swarm
Options:
--advertise-addr string Advertised address (format: "<ip|interface>[:port]")
--autolock Enable manager autolocking (requiring an unlock key to start a
stopped manager)
--availability string Availability of the node ("active", "pause", "drain") (default
"active")
--cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h)
(default 2160h0m0s)
--data-path-addr string Address or interface to use for data path traffic (format:
"<ip|interface>")
--data-path-port uint32 Port number to use for data path traffic (1024 - 49151). If no
value is set or is set to 0, the default port (4789) is used.
--default-addr-pool ipNetSlice default address pool in CIDR format (default [])
--default-addr-pool-mask-length uint32 default address pool subnet mask length (default 24)
--dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
--external-ca external-ca Specifications of one or more certificate signing endpoints
--force-new-cluster Force create a new cluster from current state
--listen-addr node-addr Listen address (format: "<ip|interface>[:port]") (default
0.0.0.0:2377)
--max-snapshots uint Number of additional Raft snapshots to retain
--snapshot-interval uint Number of log entries between Raft snapshots (default 10000)
--task-history-limit int Task history retention limit (default 5)
[root@swarm-1 ~]# docker swarm init --listen-addr 192.168.17.10
Swarm initialized: current node (nr169n90ifce5y1fj52f41t59) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0f7ozh45pdn9xk895f4tqv2butj8x0cwaml24z964ftfnl30fo-afb9p7hnn795k3iugx8xfua0y 192.168.17.10:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
说明:
1)swarm初始化,当前节点现在是一个管理者。
2)增加一个worker到这个swarm中,需要运行如下命令:
docker swarm join --token SWMTKN-1-0f7ozh45pdn9xk895f4tqv2butj8x0cwaml24z964ftfnl30fo-afb9p7hnn795k3iugx8xfua0y 192.168.17.10:2377
添加工作节点到swarm集群中:
然后我们就再另外两台机器上部署:
[root@swarm-2 ~]# docker swarm join --token SWMTKN-1-0f7ozh45pdn9xk895f4tqv2butj8x0cwaml24z964ftfnl30fo-afb9p7hnn795k3iugx8xfua0y 192.168.17.10:2377
This node joined a swarm as a worker.
检查节点是否添加成功:
[root@swarm-1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
nr169n90ifce5y1fj52f41t59 * swarm-1 Ready Active Leader 24.0.2
tc0ulnxfvdnpvcwioo2mzcxh7 swarm-2 Ready Active 24.0.2
dkdnhw8zcylue2k10mdh5g0s3 swarm-3 Ready Active 24.0.2
我们能看到三个节点处于活跃的状态。还能看到Docker的版本。
master节点的高可用:
可以支持多master节点。
docker swarm join-token manager