etcd v3 集群备份和恢复
最编程
2024-05-03 13:06:50
...
官方文档
https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/recovery.md
一、运行3个etcd节点
我们用一台机器的不同商品来模拟3个etcd节点
启动脚本差不多,这里我写成了一个shell如下,vim /home/chenqionghe/etcdTest/run-node.sh
#!/usr/bin/env bash
usage() {
echo "Usage: `basename $0` nodeName dataDir clientPort peerPort cluster "
echo "例如:`basename $0` node1 /data/node1.etcd 11379 11380 \"node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380\""
exit 1;
}
# 检测参数
if [ $# -lt 5 ];then
usage
exit 1
fi
name=$1
dataDir=$2
clientPort=$3
peerPort=$4
cluster=$5
token="light-weight-baby"
# 运行节点
etcd --name ${name} \
--data-dir ${dataDir} \
--initial-cluster-token ${token} \
--initial-advertise-peer-urls http://127.0.0.1:${peerPort} \
--listen-peer-urls http://0.0.0.0:${peerPort} \
--listen-client-urls http://0.0.0.0:${clientPort} \
--advertise-client-urls http://0.0.0.0:${clientPort} \
--initial-cluster ${cluster} \
--initial-cluster-state new
使用方式如下
Usage: run-node.sh nodeName dataDir clientPort peerPort cluster
接下来我们启动3个节点
export cluster="node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380"
./run-node.sh node1 /home/chenqionghe/etcdTest/node1.etcd 11379 11380 ${cluster}
./run-node.sh node2 /home/chenqionghe/etcdTest/node2.etcd 12379 12380 ${cluster}
./run-node.sh node3 /home/chenqionghe/etcdTest/node3.etcd 13379 13380 ${cluster}
ok,我们看到3个节点都已经起来了
二、准备数据
这里我们设置几个数据项
export ETCD_ENDPOINTS="http://127.0.0.1:11379,http://127.0.0.1:12379,http://127.0.0.1:13379"
# 设置数据
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} put /cqh/deep_squal 140kg
测试集群和设置的数据
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} member list
25ad84e18438ca4e, started, node2, http://127.0.0.1:12380, http://0.0.0.0:12379
3d78c9dc937b8bce, started, node3, http://127.0.0.1:13380, http://0.0.0.0:13379
9328257f7d3eded0, started, node1, http://127.0.0.1:11380, http://0.0.0.0:11379
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
/cqh
/cqh/bench_press
/cqh/dead_lift
/cqh/deep_squal
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
chenqionghe
100kg
160kg
140kg
都已经生效
三、备份数据
很简单,就是设置一个保存的文件
export ETCD_ENDPOINTS="http://127.0.0.1:11379,http://127.0.0.1:12379,http://127.0.0.1:13379"
etcdctl --endpoints=${ETCD_ENDPOINTS} snapshot save "/home/chenqionghe/etcdTest/backup/snapshot.db"
四、恢复数据
执行恢复,我们指定恢复的目录为nodeName.etcd.restore,比之前的数据目录多了个.restore
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node1.etcd.restore" \
--name node1 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:11380
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node2.etcd.restore" \
--name node2 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:12380
export ETCDCTL_API=3
etcdctl snapshot restore /home/chenqionghe/etcdTest/backup/snapshot.db \
--data-dir="/home/chenqionghe/etcdTest/node3.etcd.restore" \
--name node3 \
--initial-cluster "node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380" \
--initial-cluster-token etcdv3-cluster \
--initial-advertise-peer-urls http://127.0.0.1:13380
测试恢复,我们先删除所有示例数据
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/deep_squal 140kg
确认已经没有数据了
# 测试恢复,先删除所有示例数据
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh chenqionghe
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/bench_press 100kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/dead_lift 160kg
etcdctl --endpoints=${ETCD_ENDPOINTS} del /cqh/deep_squal 140kg
# 确认已经没有数据了
etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
停止之前的3个节点,再重新根据.restore目录分别运行3个节点
export cluster="node1=http://127.0.0.1:11380,node2=http://127.0.0.1:12380,node3=http://127.0.0.1:13380"
./run-node.sh node1 /home/chenqionghe/etcdTest/node1.etcd.restore 11379 11380 ${cluster}
./run-node.sh node2 /home/chenqionghe/etcdTest/node2.etcd.restore 12379 12380 ${cluster}
./run-node.sh node3 /home/chenqionghe/etcdTest/node3.etcd.restore 13379 13380 ${cluster}
重新执行命令
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --keys-only
/cqh
/cqh/bench_press
/cqh/dead_lift
/cqh/deep_squal
root@ubuntu:/home/chenqionghe/test/etcd# etcdctl --endpoints=${ETCD_ENDPOINTS} get /cqh --prefix --print-value-only
chenqionghe
100kg
160kg
140kg
看到数据恢复了,ok,就这么简单
上一篇: Etcd 概述
下一篇: etcd 配置文件描述