etcd数据备份与恢复
程序员文章站
2022-07-13 22:37:03
...
测试集群
节点:
192.168.0.105
192.168.0.106
192.168.0.107
1、创建目录
mkdir -p /data/etcd_backup_dir/
2、略
3、备份(暂存)
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.105:2379 snapshot save /data/etcd_backup_dir/etcd1-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.106:2379 snapshot save /data/etcd_backup_dir/etcd2-snapshot-`date +%Y%m%d`.db
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.107:2379 snapshot save /data/etcd_backup_dir/etcd3-snapshot-`date +%Y%m%d`.db
4、加定时任务(备份)
4、1 脚本
cat > /data/etcd_backup_dir/etcdbak.sh << EOF
#/bin/bash
# etcd备份脚本
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.105:2379 snapshot save /data/etcd_backup_dir/etcd1-snapshot-`date +%Y%m%d`.db
EOF
cat > /data/etcd_backup_dir/etcdbak.sh << EOF
#/bin/bash
# etcd备份脚本
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.106:2379 snapshot save /data/etcd_backup_dir/etcd2-snapshot-`date +%Y%m%d`.db
EOF
cat > /data/etcd_backup_dir/etcdbak.sh << EOF
#/bin/bash
# etcd备份脚本
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.106:2379 snapshot save /data/etcd_backup_dir/etcd3-snapshot-`date +%Y%m%d`.db
EOF
4.2 授权
chmod +x /data/etcd_backup_dir/etcdbak.sh
4.3 定时任务
crontab -e
0 20 * * * /data/etcd_backup_dir/etcdbak.sh
5、恢复
5、1 关闭apiserver和etcd
systemctl stop kube-apiserver && systemctl stop etcd
5.2 备份数据
mv /var/lib/etcd /var/lib/etcd-$(date +%Y%m%d).bak
5.3 恢复数据(最后备份时间),
etcdctl snapshot restore etcd1-snapshot-`date +%Y%m%d`.db \
--name etcd-192.168.0.105 \
--initial-cluster etcd-192.168.0.105=https://192.168.0.105:2380,etcd-192.168.0.106=https://192.168.0.106:2380,etcd-192.168.0.107=https://192.168.0.107:2380 \
--initial-cluster-token etcd-cluster-0 --initial-advertise-peer-urls https://192.168.0.105:2380 --data-dir=/var/lib/etcd
etcdctl snapshot restore etcd1-snapshot-`date +%Y%m%d`.db \
--name etcd-192.168.0.106 \
--initial-cluster etcd-192.168.0.105=https://192.168.0.105:2380,etcd-192.168.0.106=https://192.168.0.106:2380,etcd-192.168.0.107=https://192.168.0.107:2380 \
--initial-cluster-token etcd-cluster-0 --initial-advertise-peer-urls https://192.168.0.106:2380 --data-dir=/var/lib/etcd
etcdctl snapshot restore etcd1-snapshot-`date +%Y%m%d`.db \
--name etcd-192.168.0.107 \
--initial-cluster etcd-192.168.0.105=https://192.168.0.105:2380,etcd-192.168.0.106=https://192.168.0.106:2380,etcd-192.168.0.107=https://192.168.0.107:2380 \
--initial-cluster-token etcd-cluster-0 --initial-advertise-peer-urls https://192.168.0.107:2380 --data-dir=/var/lib/etcd
5.4 启动apiserver和etcd
systemctl start kube-apiserver && systemctl start etcd
5.5 查看集群状态
etcdctl --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.0.105:2379 --endpoints=https://192.168.0.106:2379 --endpoints=https://192.168.0.107:2379 endpoint status --write-out=table
或者
kubectl get cs
上一篇: c# 数据结构与算法解读篇(键值对key-value的使用)
下一篇: Ext Combobox