恢复etcd

130 阅读3分钟

该文命令用于etcd3

查看etcd版本

etcdctl version

etcdctl version: 3.4.15
API version: 3.4

插入数据

etcdctl --endpoints="IP:2379" put key value

查询数据
按key值查询

etcdctl --endpoints="IP:2379" get key

查询所有key

etcdctl --endpoints="IP:2379" --from-key ""

不显示key只显示values

etcdctl --endpoints="IP:2379" get --print-value-only key

按key前缀查找

etcdctl --endpoints="IP:2379" get --prefix key前缀

删除数据
删除key

etcdctl --endpoints="IP:2379" del key

删除key name12时并返回被删除的键值对

etcdctl --endpoints="IP:2379" del --prev-kv key

删除指定前缀的key

etcdctl --endpoints="IP:2379" del --prev-kv --prefix key的前缀

删除所有数据

etcdctl --endpoints="IP:2379" del --prefix ""

更新数据
直接put即可更新
(2)对于 API 3 备份与恢复方法
备份数据:

etcdctl --endpoints localhost:2379 snapshot save snapshot.db

查看快照信息

etcdctl --endpoints localhost:2379 snapshot status snapshot.db --write-out=table

恢复:
etcd 恢复之前配置文件,data目录为/data/etcd/:

cat /etc/etcd/etcd.conf

ETCD_NAME=default
ETCD_DATA_DIR="/data/etcd/default.etcd/"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"

etcdctl --endpoints localhost:2379 snapshot restore snapshot.db --data-dir=./data/etcd/default.etcd

此时会在/data/etcd/ 下新建立 data目录,因此会改变存储路径,此时需要进行数据目录的修改

mv default.etcd default.etcd_bak

mv /data/etcd/data/etcd/default.etcd /data/etcd/

rm -rf /data/etcd/data

保持配置文件不变,重启etcd服务

恢复后的文件需要修改权限为 etcd:etcd
–name:重新指定一个数据目录,可以不指定,默认为 default.etcd
–data-dir:指定数据目录
建议使用时不指定 name 但指定 data-dir,并将 data-dir 对应于 etcd 服务中配置的 data-dir

etcd 集群都是至少 3 台机器,官方也说明了集群容错为 (N-1)/2,所以备份数据一般都是用不到,但是鉴上次 gitlab 出现的问题,对于备份数据也要非常重视。
注意在每个节点进行恢复,一个是恢复数据,一个是重塑身份
ETCDCTL_API=3 etcdctl snapshot restore /root/snap.db \
--name etcd-1 \
--initial-cluster="etcd-1=https://192.168.179.99:2380,etcd-2=https://192.168.179.100:2380,etcd-3=https://192.168.179.101:2380" \
--initial-cluster-token=etcd-cluster \
--initial-advertise-peer-urls=https://192.168.179.99:2380 \
--data-dir=/var/lib/etcd/default.etcd

--name etcd-1 #需要修改为当前节点名称
--initial-advertise-peer-urls=https://192.168.179.99:2380 #当前节点IP

三台集群分别恢复备份数据 
解压数据
tar snap-202011252359.tar.gz

172.18.1.11恢复
ETCDCTL_API=3 etcdctl --name=etcd1 --endpoints="http://172.18.1.11:2379"  --initial-cluster-token=etcd-cluster --initial-advertise-peer-urls=http://172.18.1.11:2380 --initial-cluster=etcd1=http://172.18.1.11:2380,etcd2=http://172.18.1.12:2380,etcd3=http://172.18.1.13:2380 --data-dir=/var/lib/etcd/ snapshot restore  ./snap-202011252359.db

172.18.1.12恢复
ETCDCTL_API=3 etcdctl --name=etcd2 --endpoints="http://172.18.1.12:2379"  --initial-cluster-token=etcd-cluster --initial-advertise-peer-urls=http://172.18.1.12:2380 --initial-cluster=etcd1=http://172.18.1.11:2380,etcd2=http://172.18.1.12:2380,etcd3=http://172.18.1.13:2380 --data-dir=/var/lib/etcd/ snapshot restore  ./snap-202011252359.db

172.18.1.13恢复
ETCDCTL_API=3 etcdctl --name=etcd3 --endpoints="http://172.18.1.13:2379"  --initial-cluster-token=etcd-cluster --initial-advertise-peer-urls=http://172.18.1.13:2380 --initial-cluster=etcd1=http://172.18.1.11:2380,etcd2=http://172.18.1.12:2380,etcd3=http://172.18.1.13:2380 --data-dir=/var/lib/etcd/ snapshot restore  ./snap-202011252359.db

启动etcd集群
systemctl  daemon-reload
systemctl enable etcd
systemctl start etcd

查看数据恢复
ETCDCTL_API=3  etcdctl   --endpoints 172.18.1.11:2379,172.18.1.12:2379,172.18.1.13:2379 get / --prefix

(5)etcd  集群检查
ETCDCTL_API=3  etcdctl   --endpoints 172.18.1.11:2379,172.18.1.12:2379,172.18.1.13:2379 endpoint status  --write-out="table"
+------------------+------------------+---------+---------+-----------+-----------+------------+
|     ENDPOINT     |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+------------------+------------------+---------+---------+-----------+-----------+------------+
| 172.18.1.11:2379 | a665d104822ee7c9 |  3.3.18 |   25 kB |      true |        81 |         25 |
| 172.18.1.12:2379 | f4f9dc017f438b07 |  3.3.18 |   25 kB |     false |        81 |         25 |
| 172.18.1.13:2379 | da1a094d31db677c |  3.3.18 |   25 kB |     false |        81 |         25 |
+------------------+------------------+---------+---------+-----------+-----------+------------+

ETCDCTL_API=3  etcdctl   --endpoints 172.18.1.11:2379,172.18.1.12:2379,172.18.1.13:2379 endpoint health
172.18.1.11:2379 is healthy: successfully committed proposal: took = 2.078969ms
172.18.1.12:2379 is healthy: successfully committed proposal: took = 2.209654ms
172.18.1.13:2379 is healthy: successfully committed proposal: took = 2.469605ms