#备份举例
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 snapshot save /mnt/disk0/back/snapshot.db
#如下命令为查看/路径下10条key,如需导出全部需要把--limit 10去掉
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 get / --prefix --keys-only --limit 10 --command-timeout=60s | sed '/^\s*$/d' | tee a.log
###删除key(删除慢不用—)
export ETCDCTL_API=3
grep '/registry/events/' a.log | xargs -I{} /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 del {}
#如下命令表示删除前缀为/registry/events/所有的键
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 del --prefix /registry/events/
###收缩碎片内存
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 defrag
####消除警告
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 alarm disarm
返回:memberID:13803658152347727308 alarm:NOSPACE
####查看效果
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --cacert=/app/prophet/opt/k8s/etc/kubernetes/ssl/ca.pem --cert=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd.pem --key=/app/prophet/opt/k8s/etc/kubernetes/ssl/etcd-key.pem --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 endpoint status
#####重启etcd
systemctl restart kube-etcd
#####回滚恢复
#首先保证保证所有 etcd 节点服务处于停止状态
systemctl stop etcd && systemctl status etcd
#所有节点 etcd 数据目录是空的
mkdir /mnt/disk1/etcd.bak && mv /mnt/disk1/etcd/* /mnt/disk1/etcd.bak # 或者 rm -rf /mnt/disk1/etcd/*
#恢复数据(注意,如果备份文件来源于 etcd 数据目录 member/snap/db 文件,则在恢复时需要添加 --skip-hash-check=true 参数 )
分别在3个节点执行:
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --name=scorepht-a --initial-cluster-token=etcd-cluster-0 --initial-advertise-peer-urls=https://10.80.88.x1:2380 --initial-cluster=scorepht-a=https://10.80.88.x2:2380,scorepht-b=https://10.80.88.95:2380,scorepht-c=https://10.80.88.x3:2380 --data-dir=/mnt/disk1/etcd --skip-hash-check=true snapshot restore /data/etcd-snapshot-20190920-11\:09\:1568950186.db
#依次启动各节点 etcd 服务:
systemctl start etcd
#测试验证:
ETCDCTL_API=3 /app/prophet/opt/k8s/bin/etcdctl --endpoints=xx.xx.xx.ip1:2379,xx.xx.xx.ip2:2379,xx.xx.xx.ip3:2379 --cert=/etc/kubernetes/cert/kubernetes.pem --key=/etc/kubernetes/cert/kubernetes-key.pem --cacert=/etc/kubernetes/cert/ca.pem get /zj-test
/zj-test
1234