etcd 发表于 2024-09-10 | 更新于 2024-09-10
| 字数总计: 935 | 阅读时长: 4分钟
概述 Kubernetes 使用 Etcd 数据库实时存储集群中的数据,安全起见,一定要备份。 Kubeadm 部署的 etcd 没有 etcdctl 命令,需要单独下载 etcd 二进制包。
注意事项
配置 etcdctl 下载 etcdcli [root@master ~]# docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdctl version" etcdctl version: 3.5.1 API version: 3.5
安装 etcdcli wget https://github.com/etcd-io/etcd/releases/download/v3.5.1/etcd-v3.5.1-linux-arm64.tar.gz tar xf etcd-v3.5.1-linux-arm64.tar.gz cd etcd-v3.5.1-linux-arm64/mkdir /opt/etcd/bincp etcd etcdctl etcdutl /opt/etcd/bin/echo 'export PATH=$PATH:/opt/etcd/bin' >> /etc/profile
备份etcd ETCDCTL_API=3 etcdctl \ snapshot save /opt/etcd/etcd_backup/snap-etcd-$(date +%F-%H-%M-%S).db \ --endpoints=https://127.0.0.1:2379 \ --cacert=/etc/kubernetes/pki/etcd/ca.crt \ --cert=/etc/kubernetes/pki/etcd/server.crt \ --key=/etc/kubernetes/pki/etcd/server.key
注: 会生成一个路径为”/opt/etcd/etcd_backup/snap-etcd-2023-01-20-07-10-54.db”的etcd备份文件。
检查 etcd 备份 etcdctl snapshot status /var /lib/backup/etcd-snapshot.db -wtable
恢复etcd快照 etcd 容器 [root@master etcd_backup]# k get pods NAME READY STATUS RESTARTS AGE nginx-85b98978db-6tr66 1/1 Running 0 9h [root@master etcd_backup]# k delete deployments.apps nginx deployment.apps "nginx" deleted [root@master etcd_backup]# k get pods No resources found in default namespace.
mv /etc/kubernetes/manifests/ /etc/kubernetes/manifests-backup/
mv /var/lib/etcd/ /var/lib/etcd-$(date +%F-%H-%M-%S)-backup/
ETCDCTL_API=3 etcdctl \ snapshot restore /opt/etcd/etcd_backup/snap-etcd-2023-01-20-07-10-54.db \ --data-dir=/var/lib/etcd/
注: /var/lib/etcd/目录不存在时,还原时会自动创建
mv /etc/kubernetes/manifests-backup/ /etc/kubernetes/manifests/
注: 容器启动需要一定时间,请耐心等待还原完成
kubectl delete pod/<kube-proxy容器名> -n kube-system systemctl restart kubelet.service
注:delete kube-proxy容器的时候可能会卡住,这时候直接重启对应节点的 kubele进程即可。
etcd systemd 方式 1. 首先确认 etcd 数据目录 ps -ef |grep etcd 一般默认为/var /lib/etcd 2. 停止 etcd 服务 sudo systemctl stop etcd 3. 先移动备份 etcd 原目录 sudo mv /var /lib/etcd /var /lib/etcd.bak 4. 开始还原(还原时,可以不加证书和秘钥) sudo ETCDCTL_API =3 etcdctl --data-dir=/var/ lib/etcd snapshot restore /data/backup/etcd-snapshot-previous.db 5. 更改文件属主 sudo chown -R etcd :etcd /var /lib/etcd 6. 启动 etcd 服务 sudo systemctl start etcd
备份恢复etcd (不推荐方式) export ETCDCTL_API =3 etcdctl --endpoints=https : etcdctl snapshot status /var /lib/backup/etcd-snapshot.db -wtable ## 移除且备份 /etc/kubernetes/manifests 目录 sudo mv /etc/kubernetes/manifests /etc/kubernetes/manifests.bak ## 查看 kube-apiserver、etcd 镜像是否停止 crictl ps|grep etcd && crictl ps|grep kube-apiserver ## 备份现有 Etcd 数据 sudo mv /var /lib/etcd /var /lib/etcd.bak ##开始还原(还原时,可以不加证书和秘钥) sudo ETCDCTL_API =3 etcdctl --endpoints=https : (之前备份的 HASH 为 68a972d6) ## 恢复 Kube -Apiserver 与 Etcd 镜像 sudo mv /etc/kubernetes/manifests.bak /etc/kubernetes/manifests
参考 链接地址:https://www.cnblogs.com/LiuChang-blog/p/15352764.html