https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/recovery.mdnode
咱们用一台机器的不一样商品来模拟3个etcd节点
启动脚本差很少,这里我写成了一个shell以下,vim /home/chenqionghe/etcdTest/run-node.shgit
#!/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
使用方式以下github
Usage: run-node.sh nodeName dataDir clientPort peerPort cluster
接下来咱们启动3个节点shell
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个节点都已经起来了ubuntu
这里咱们设置几个数据项vim
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
测试集群和设置的数据bash
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
都已经生效ide
很简单,就是设置一个保存的文件测试
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
ui
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,就这么简单