etcd单机集群部署
下载etcd 二进制安装包。
https://github.com/etcd-io/etcd/releases
创建相关目录
mkdir –p /data/etcd/node1
mkdir –p /data/etcd/node2
mkdir –p /data/etcd/node3
在 /data/etcd/node1,/data/etcd/node2,/data/etcd/node3创建三个配置文件
node1.yml
name: node1
data-dir: /data/etcd/node1
listen-client-urls: 'http://192.168.73.222:9002'
advertise-client-urls: 'http://192.168.73.222:9002'
listen-peer-urls: 'http://192.168.73.222:9001'
initial-advertise-peer-urls: 'http://192.168.73.222:9001'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node2.yml
name: node2
data-dir: /data/etcd/node2
listen-client-urls: 'http://192.168.73.222:9004'
advertise-client-urls: 'http://192.168.73.222:9004'
listen-peer-urls: 'http://192.168.73.222:9003'
initial-advertise-peer-urls: 'http://192.168.73.222:9003'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node3.yml
name: node3
data-dir: /data/etcd/node3
listen-client-urls: 'http://192.168.73.222:9006'
advertise-client-urls: 'http://192.168.73.222:9006'
listen-peer-urls: 'http://192.168.73.222:9005'
initial-advertise-peer-urls: 'http://192.168.73.222:9005'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
启动3个节点
/data/etcd/etcd/etcd --config-file=/data/etcd/node1/node1.yml &
/data/etcd/etcd/etcd --config-file=/data/etcd/node2/node2.yml &
/data/etcd/etcd/etcd --config-file=/data/etcd/node3/node3.yml &
查看集群状态
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 member list
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 endpoint health
--name:etcd集群中的节点名,这里可以随意,可区分且不重复就行
--listen-peer-urls:监听的用于节点之间通信的url,可监听多个,集群内部将通过这些url进行数据交互(如选举,数据同步等)
--initial-advertise-peer-urls:建议用于节点之间通信的url,节点间将以该值进行通信。
--listen-client-urls:监听的用于客户端通信的url,同样可以监听多个。
--advertise-client-urls:建议使用的客户端通信 url,该值用于 etcd 代理或 etcd 成员与 etcd 节点通信。
--initial-cluster-token: etcd-cluster-1,节点的 token 值,设置该值后集群将生成唯一 id,并为每个节点也生成唯一 id,当使用相同配置文件再启动一个集群时,只要该 token 值不一样,etcd 集群就不会相互影响。
--initial-cluster:也就是集群中所有的 initial-advertise-peer-urls 的合集。
--initial-cluster-state:new,新建集群的标志
etcd备份脚本
etcd-bak.sh
#!/usr/bin/env bash
date;
ENDPOINTS="http://192.168.73.222:9001"
ETCDCTL_API=3 /data/etcd/etcd/etcdctl \
--endpoints=${ENDPOINTS} \
snapshot save /data/etcdbak/etcd-data-`date +%Y%m%d%H%M%S`.db
# 备份保留30天
find /data/etcdbak/ -name *.db -mtime +30 -exec rm -f {} \;
运行备份脚本
sh -x etcd-bak.sh
etcd数据恢复
停止etcd
ps -ef |grep etcd|grep -v grep|awk '{print $2}'|xargs kill -9
导入数据,如下
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db \
--name node1 \
--data-dir /data/etcd/node11 \
--initial-advertise-peer-urls http://192.168.73.222:9001 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 \
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db \
--name node2 \
--data-dir /data/etcd/node22 \
--initial-advertise-peer-urls http://192.168.73.222:9003 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 \
ETCDCTL_API=3 /data/etcd/etcd/etcdctl snapshot restore /data/etcdbak/etcd-data-20210608180220.db \
--name node3 \
--data-dir /data/etcd/node33 \
--initial-advertise-peer-urls http://192.168.73.222:9005 \
--initial-cluster 'node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005' \
--initial-cluster-token etcd-cluster-1 \
更改启动文件中data-dir为导入时的目录
node1.yml
name: node1
data-dir: /data/etcd/node11
listen-client-urls: 'http://192.168.73.222:9002'
advertise-client-urls: 'http://192.168.73.222:9002'
listen-peer-urls: 'http://192.168.73.222:9001'
initial-advertise-peer-urls: 'http://192.168.73.222:9001'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node2.yml
name: node2
data-dir: /data/etcd/node22
listen-client-urls: 'http://192.168.73.222:9004'
advertise-client-urls: 'http://192.168.73.222:9004'
listen-peer-urls: 'http://192.168.73.222:9003'
initial-advertise-peer-urls: 'http://192.168.73.222:9003'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
node3.yml
name: node3
data-dir: /data/etcd/node33
listen-client-urls: 'http://192.168.73.222:9006'
advertise-client-urls: 'http://192.168.73.222:9006'
listen-peer-urls: 'http://192.168.73.222:9005'
initial-advertise-peer-urls: 'http://192.168.73.222:9005'
initial-cluster: node1=http://192.168.73.222:9001,node2=http://192.168.73.222:9003,node3=http://192.168.73.222:9005
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
启动etcd
/data/etcd/etcd/etcd --config-file=/data/etcd/node1/node1.yml &
/data/etcd/etcd/etcd --config-file=/data/etcd/node2/node2.yml &
/data/etcd/etcd/etcd --config-file=/data/etcd/node3/node3.yml &
查看集群状态
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 member list
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 endpoint health
查看etcd中的key值
/data/etcd/etcd/etcdctl --endpoints http://192.168.73.222:9002,http://192.168.73.222:9004,http://192.168.73.222:9006 get --prefix / |wc -l