docker swarm集群
机器环境(虚拟机centos)
担任角色 | IP地址 | 主机名 |
---|---|---|
manager | 10.52.72.252 | centos7-01 |
node | 10.52.72.253 | centos7-02 |
node | 10.52.72.254 | centos7-03 |
准备工作
(1)修改主机名
hostnamectl set-hostname xxx
10.52.72.252 主机上执行 hostnamectl set-hostname centos7-01
10.52.72.253 主机上执行 hostnamectl set-hostname centos7-02
10.52.72.254 主机上执行 hostnamectl set-hostname centos7-03
(2)配置hosts文件(3台机器都配置)
vim /etc/hosts
10.52.72.252 centos7-01
10.52.72.253 centos7-02
10.52.72.254 centos7-03
(3)设置防火墙
放行2377/tcp(管理端口)、7946/udp(节点间通信端口)、4789/udp(overlay 网络端口)端口
(4)安装docker
[root@centos7-01 ~]# yum -y install docker
[root@centos7-02 ~]# yum -y install docker
[root@centos7-03 ~]# yum -y install docker
设置docker开机启动并启动3台机器docker
systemctl enable docker
systemctl start docker
创建Swarm并添加节点
创建Swarm集群
在manager节点执行
swarm init --advertise-addr 10.52.72.252
Swarm initialized: current node (xbmqicdvcx62jzoe4wwnzebia) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5adm1jxedw2w3eznc06yausonlgufaspelnsz25ni8ojpvwp8d-6jpf6l9q75r880g4z6mxan4yo 10.52.72.252:2377
To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
上面命令执行后,该机器自动加入到swarm集群。这个会创建一个集群token,获取全球唯一的 token,作为集群唯一标识。后续将其他节点加入集群都会用到这个token值。
其中,–advertise-addr参数表示其它swarm中的worker节点使用此ip地址与manager联系。命令的输出包含了其它节点如何加入集群的命令。
重新创建swarm集群
docker swarm leave -f
swarm init --advertise-addr 10.52.72.252
查看集群信息
dicker node ls
ID | HOSTNAME | STATUS | AVAILABILITY | MANAGER STATUS | ENGINE VERSION |
---|---|---|---|---|---|
3jcmnzjh0e99ipgshk1ykuovd * | centos7-01 | Ready | Active | Leader | 18.06.0-ce |
添加节点到Swarm集群
添加节点主机到Swarm集群
10.52.72.253 主机上执行
docker swarm join --token SWMTKN-1-5adm1jxedw2w3eznc06yausonlgufaspelnsz25ni8ojpvwp8d-6jpf6l9q75r880g4z6mxan4yo 10.52.72.252:2377
10.52.72.254 主机上执行
docker swarm join --token SWMTKN-1-5adm1jxedw2w3eznc06yausonlgufaspelnsz25ni8ojpvwp8d-6jpf6l9q75r880g4z6mxan4yo 10.52.72.252:2377
10.52.72.252 主机上
dicker node ls
ID | HOSTNAME | STATUS | AVAILABILITY | MANAGER STATUS | ENGINE VERSION |
---|---|---|---|---|---|
3jcmnzjh0e99ipgshk1ykuovd * | centos7-01 | Ready | Active | Leader | 18.06.0-ce |
3jcmnzjh0e99ipgshk1ykuove * | centos7-02 | Ready | Active | Leader | 18.06.0-ce |
3jcmnzjh0e99ipgshk1ykuovf* | centos7-03 | Ready | Active | Leader | 18.06.0-ce |
修改节点状态
docker node update --availability active/drain centos7-02