k8s 中 etcd 的备份和恢复操作

安装一个 etcd后,添加一些 key/value 的操作

helm install -n etcd bitnami/etcd

查看集群状态和 key 列表,可在任意容器中执行

kubectl exec -it -n etcd apisix-etcd-0 -- etcdctl endpoint --cluster=true health
kubectl exec -it -n etcd apisix-etcd-0 -- etcdctl get / --prefix --keys-only

备份数据库

在旧集群中,备份并导出db文件,可在任意容器中执行

kubectl exec -it -n etcd apisix-etcd-0 -- etcdctl snapshot save /tmp/etcd-snapshot-`date +%Y%m%d`.db
{"level":"info","ts":1661336305.1354377,"caller":"snapshot/v3_snapshot.go:119","msg":"created temporary db file","path":"/tmp/etcd-snapshot-20220824.db.part"}
{"level":"info","ts":"2022-08-24T10:18:25.136Z","caller":"clientv3/maintenance.go:200","msg":"opened snapshot stream; downloading"}
{"level":"info","ts":1661336305.1360471,"caller":"snapshot/v3_snapshot.go:127","msg":"fetching snapshot","endpoint":"127.0.0.1:2379"}
{"level":"info","ts":"2022-08-24T10:18:25.217Z","caller":"clientv3/maintenance.go:208","msg":"completed snapshot read; closing"}
{"level":"info","ts":1661336305.31402,"caller":"snapshot/v3_snapshot.go:142","msg":"fetched snapshot","endpoint":"127.0.0.1:2379","size":"17 MB","took":0.17848353}
{"level":"info","ts":1661336305.3141167,"caller":"snapshot/v3_snapshot.go:152","msg":"saved","path":"/tmp/etcd-snapshot-20220824.db"}
Snapshot saved at /tmp/etcd-snapshot-20220824.db

从容器中下载备份文件,注意第二个参数,必须是文件名

kubectl cp -n etcd apisix-etcd-0:/tmp/etcd-snapshot-20220824.db etcd.db

导入数据库

  1. 需要将备份文件分别传到所有容器中,依次执行即可
kubectl cp -n etcd etcd.db apisix-etcd-0:/tmp/etcd.db
  1. 在每个容器中,分别执行导入命令,由于原进程可能在不断的写文件,所以要用 mv

分别执行的好处是可以解决不同容器中不同用户组的问题

mv /bitnami/etcd/data /bitnami/etcd/data1; etcdctl snapshot restore --skip-hash-check=true --data-dir=/bitnami/etcd/data /tmp/etcd.db

恢复数据库

重启 etcd 即可,恢复过程可以多次操作