目标:组建3台节点的etcd集群
节点信息:
node0 | 10.211.55.9 |
node1 | 10.211.55.10 |
node2 | 10.211.55.11 |
一. 准备工作:
1.关闭并停用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
2.永久关闭SELinux
vim /etc/selinux/config
SELINUX=disabled
3.同步集群系统时间
yum -y install ntp
ntpdate 0.asia.pool.ntp.org
4.重启机器
reboot
二.安装与配置参数:
安装ETCD:
yum -y install etcd (由于是rpm包所以可以用remove卸载)
配置工作目录:
mkdir /ETCD
cd /ETCD
mkdir data (用于放置数据,清除该文件夹内容,相当于重置集群)
mkdir log (用于输出日志)
编写启动与重启脚本:
1启动脚本(build.sh):
#!/bin/bash
etcd --name node1 \
--data-dir /ETCD/data/ \
--listen-peer-urls http://10.211.55.10:2380 \
--listen-client-urls http://10.211.55.10:2379,http://127.0.0.1:2379 \
--initial-advertise-peer-urls http://10.211.55.10:2380 \
--advertise-client-urls http://10.211.55.10:2379 \
--initial-cluster node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380 \
--initial-cluster-token Achilles \
--initial-cluster-state new
2重启脚本(restart.sh):
etcd --name node1 \
--data-dir /ETCD/data/ \
--listen-peer-urls http://10.211.55.10:2380 \
--listen-client-urls http://10.211.55.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.211.55.10:2379 \
注意:为了方便管理,我将两个脚本都放置于/ETCD目录下,启动脚本build.sh只在集群第一次初始化时启用,节点掉线恢复均使用restart.sh
更改脚本的执行权限:
chmod +x ./build.sh
chmod +x ./restart.sh
每台节点均执行以上操作后,分别启动build.sh脚本:
nohup ./build.sh > /ETCD/log/etcd.log 2>&1 &
该命令表示后台执行,并且将日志输出至“/ETCD/log/etcd.log”
此时,3节点的etcd集群已建立,执行健康检查:
健康检查:
etcdctl cluster-health
节点掉线或者重启,则执行相应节点的restart.sh脚本:
nohup ./restart.sh > /ETCD/log/etcd.log 2>&1 &
三.增减成员节点:
新增节点(例如10.211.55.12)
在集群任意节点输入:
etcdctl member add http://10.211.55.12:2380
命令行返回:
added member 9bf1b35fc7761a23 to cluster
ETCD_NAME="node3"
ETCD_INITIAL_CLUSTER="node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380,node3=http://10.211.55.12:2380"
ETCD_INITIAL_CLUSTER_STATE=existing
其中ETCD_INITIAL_CLUSTER是最为重要的信息,记录了集群成员。
编写新增节点脚本:
#!/bin/bash
etcd --name node3 \
--data-dir /ETCD/data/ \
--listen-peer-urls http://10.211.55.12:2380 \
--listen-client-urls http://10.211.55.12:2379,http://127.0.0.1:2379 \
--initial-advertise-peer-urls http://10.211.55.12:2380 \
--advertise-client-urls http://10.211.55.12:2379 \
--initial-cluster node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380,node3=http://10.211.55.12:2380 \
--initial-cluster-token Achilles \
--initial-cluster-state existing
可以发现,新增脚本与启动脚本的区别仅在于--initial-cluster参数多了新增节点的ip:port,--initial-cluster-state参数表示集群状态,existing表示加入一个已存在的集群,如果填new,该脚本会新生成一个集群ID,产生冲突。
执行新增脚本,新节点加入集群。
删除节点:
etcdctl member remove 节点ID
删除节点的etcd服务会自动停止,若要将其加入其他集群,需要先清除/ETCD/data下的数据内容。
备份数据:
etcdctl backup --data-dir /ETCD/data/ --backup-dir /backup/etcd
四.总结:
ETCD集群的操作分两个阶段,即启动与运行阶段,启动阶段的集群有3种配置方式,
静态配置
etcd自发现模式
- DNS自发现模式
本文采用的是静态配置,etcd自发现模式与静态配置的区别在于静态模式预先知道所有节点的ip信息,而etcd自发现则不用,
配置方法与静态配置类似,仅仅将启动脚本build.sh中--initial-cluster参数去掉,新增:
--discovery https://discovery.etcd.io/964f7149b939aa1199536505cc88eacc \
其中,discovery参数的获取方法:
curl https://discovery.etcd.io/new?size=3
返回 https://discovery.etcd.io/f1b620b986fae36fc2994c06c00e30ed
两种启动方法的运行阶段操作基本一致,DNS发现模式暂且不表。