一般在生产环境下单机的etcd不满足高可用的需求,所以我们需要部署高可用的etcd集群,确保集群节点数N*2+1,etcd的raft协议可以保障各个节点数据的一致性。
我们目前规划集群信息如下:
192.168.1.76 etcd1 192.168.1.77 etcd2 192.168.1.79 etcd3
然后就开始部署,部署方式基本和单机一致,主要是配置文件需要增加一些配置,配置启动可以查看笔记:https://sulao.cn/post/965.html
在每个节点操作
wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz tar -zxf etcd-v3.5.0-linux-amd64.tar.gz mv etcd-v3.5.0-linux-amd64 /usr/local/etcd
添加环境变量并使其生效
echo 'export PATH=$PATH:/usr/local/etcd' >> /etc/profile source /etc/profile
创建存储目录
mkdir -p /data/etcd
接着我们创建etcd.conf配置文件目录
mkdir -p /etc/etcd
etcd1节点上的etcd.conf配置文件如下
vi /etc/etcd/etcd.conf name: "etcd1" #节点名称 data-dir: "/data/etcd" # 数据存储目录 advertise-client-urls: "http://192.168.1.76:2379" # 客户端监听地址 listen-client-urls: "http://192.168.1.76:2379,http://127.0.0.1:2379" # 客户端请求的地址列表 listen-peer-urls: "http://192.168.1.76:2380" # 监听URL,用于节点之间通信监听地址 initial-advertise-peer-urls: "http://192.168.1.76:2380" # 服务端之间通讯地址列表 initial-cluster: "etcd1=http://192.168.1.76:2380,etcd2=http://192.168.1.77:2380,etcd3=http://192.168.1.79:2380" #etcd集群的节点地址列表 initial-cluster-token: "etcd-cluster" # etcd集群的初始集群令牌 initial-cluster-state: 'new' # etcd集群初始化的状态,new代表新建集群,existing表示加入现有集群
etcd2节点上的etcd.conf配置如下
vi /etc/etcd/etcd.conf name: "etcd2" data-dir: "/data/etcd" advertise-client-urls: "http://192.168.1.77:2379" listen-client-urls: "http://192.168.1.77:2379,http://127.0.0.1:2379" listen-peer-urls: "http://192.168.1.77:2380" initial-advertise-peer-urls: "http://192.168.1.77:2380" initial-cluster: "etcd1=http://192.168.1.76:2380,etcd2=http://192.168.1.77:2380,etcd3=http://192.168.1.79:2380" initial-cluster-token: 'etcd-cluster' initial-cluster-state: 'new'
etcd3节点上的etcd.conf配置如下
vi /etc/etcd/etcd.conf name: "etcd3" data-dir: "/data/etcd" advertise-client-urls: "http://192.168.1.79:2379" listen-client-urls: "http://192.168.1.79:2379,http://127.0.0.1:2379" listen-peer-urls: "http://192.168.1.79:2380" initial-advertise-peer-urls: "http://192.168.1.79:2380" initial-cluster: "etcd1=http://192.168.1.76:2380,etcd2=http://192.168.1.77:2380,etcd3=http://192.168.1.79:2380" initial-cluster-token: 'etcd-cluster' initial-cluster-state: 'new'
然后在每个节点创建etcd服务
cat >/usr/lib/systemd/system/etcd.service<<EOF [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target [Service] Type=notify EnvironmentFile=/etc/etcd/etcd.conf ExecStart=/usr/local/etcd/etcd --config-file=/etc/etcd/etcd.conf Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF
然后每个节点设置开机启动并启动etcd
systemctl daemon-reload systemctl start etcd systemctl enable etcd
三个节点的etcd部署启动好以后可以通过命令验证集群状态
etcdctl endpoint status --cluster -w table