kubernetes集群数据备份与恢复
目录:
kubernetes集群的备份就是etcd的备份
etcd操作
查看集群状态
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://10.16.2.17:2379,https://10.16.2.18:2379,https://10.16.2.19:2379 endpoint health
获取某个key信息
二进制:
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://10.16.2.17:2379,https://10.16.2.18:2379,https://10.16.2.19:2379 get /registry/apiregistration.k8s.io/apiservices/v1.apps
获取所有key
二进制:
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://10.16.2.17:2379,https://10.16.2.18:2379,https://10.16.2.19:2379 get / --prefix --keys-only
备份
二进制举例
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://10.16.2.17:2379 snapshot save /data/etcd_backup_dir/etcd-snapshot-`date +%Y%m%d`.db
恢复
停止服务
systemctl stop kube-apiserver
systemctl stop etcd
移除etcd数据
mv /data/k8s/etcd/data /data/k8s/etcd/data.bak
mv /data/k8s/etcd/wal /data/k8s/etcd/wal.bak
拷贝etcd备份快照
scp /var/lib/etcd_backup/etcd-snapshot-20200414.db root@master2:/data/etcd_backup_dir/
scp /var/lib/etcd_backup/etcd-snapshot-20200414.db root@master3:/data/etcd_backup_dir/
恢复数据
ETCDCTL_API=3 etcdctl snapshot restore /data/etcd_backup_dir/etcd-snapshot-20200414.db \
--name bjxg-sy-test \
--initial-cluster "bjxg-sy-test=https://10.16.2.17:2380" \
--initial-cluster-token etcd-cluster-0 \
--initial-advertise-peer-urls https://10.16.2.17:2380 \
--data-dir=/data/k8s/etcd/data --wal-dir=/data/k8s/etcd/wal
启动服务
systemctl start etcd
systemctl start kube-apiserver