背景:
对于k8s集群,etcd集群的健康非常关键,我们要对etcd进行备份,以免出现问题
本文,代码,针对容器化部署的k8s (kubeadm安装方式)
备份:
脚本名: etcd-backup.sh
#!/bin/bash
#backup etcd data
set -e
etcdurl=`docker exec etcd ps -o args |grep -v ps |grep -v COMMAND |awk '{print $7}'`
health=`docker exec etcd etcdctl --endpoints ${etcdurl} cluster-health |grep 'cluster is healthy'`
if [ "$health" = "cluster is healthy" ]; then
echo cluster is healthy
backuptime=`date +%F-%H-%M-%S`
etcdbackup=`docker exec -e ETCDCTL_API=3 etcd etcdctl --endpoints ${etcdurl} snapshot save snapshotdb-${backuptime} |awk '{print $4}'`
mv ${etcdbackup} /data/backup/etcd-backup
echo cluster backup is done!
else
echo cluster is not healthy
exit 1
fi
测试
运行之后就可以在备份存储路径下面看到 新的备份文件
注意:代码默认的备份路径为:/data/backup/etcd-backup
可自行修改

编写定时任务
crontab -e 编辑

恢复:
脚本
#注意手动修改最近备份文件名为snapshot.db
#!/bin/bash
#restore etcd data
set -e
etcdurl=`docker exec etcd ps -o args |grep -v ps |grep -v COMMAND |awk '{print $7}'`
health=`docker exec etcd etcdctl --endpoints ${etcdurl} cluster-health |grep 'cluster is healthy'`
if [ "$health" = "cluster is healthy" ]; then
echo "cluster is healthy"
#注意手动修改最近备份文件名为snapshot.db
docker exec etcd etcdctl --endpoints ${etcdurl} snapshot restore snapshot.db --data-dir=/var/lib/etcd
echo "etcd restore is done!"
else
echo "cluster is not healthy"
exit 1
fi
本文介绍了一个用于Kubernetes集群中etcd备份与恢复的脚本,通过使用Docker和etcdctl命令实现自动化备份流程。脚本首先检查etcd集群的健康状态,然后在确认健康的情况下进行备份,并将备份文件保存到指定路径。此外,还提供了一个恢复脚本,用于从最近的备份文件中恢复etcd数据。
4681

被折叠的 条评论
为什么被折叠?



