k8s集群中etcd的备份与恢复

etcd备份与恢复

  1. 创建备份目录mkdir /var/lib/etcd_backup
  2. 从etcd中的一个正常节点上备份etcd数据
ETCDCTL_API=3 /opt/etcd/bin/etcdctl \
--cacert=/opt/etcd/ssl/ca.pem \
--cert=/opt/etcd/ssl/server.pem \
--key=/opt/etcd/ssl/server-key.pem \
--endpoints=https://192.168.113.4:2379 \
snapshot save /var/lib/etcd_backup/etcd_20230616.db
  1. 停止所节点的kube-apiserver和etcd
systemctl stop kube-apiserver && systemctl stop etcd
  1. 备份原始etcd的路径中的文件
mv /var/lib/etcd/default.etcd /var/lib/etcd/default.etcd_bak
  1. 所有节点恢复etcd数据库

master01执行{etcd01节点}

ETCDCTL_API=3  /opt/etcd/bin/etcdctl snapshot restore  /var/lib/etcd_backup/etcd_20230616.db --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --name etcd-1 --initial-cluster "etcd-1=https://192.168.113.4:2380,etcd-2=https://192.168.113.6:2380,etcd-3=https://192.168.113.5:2380"  --initial-cluster-token etcd-cluster  --initial-advertise-peer-urls https://192.168.113.4:2380  --data-dir=/var/lib/etcd/default.etcd

work01执行{etcd02节点}

ETCDCTL_API=3  /opt/etcd/bin/etcdctl snapshot restore  /var/lib/etcd_backup/etcd_20230616.db --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --name etcd-3 --initial-cluster "etcd-1=https://192.168.113.4:2380,etcd-2=https://192.168.113.6:2380,etcd-3=https://192.168.113.5:2380"  --initial-cluster-token etcd-cluster  --initial-advertise-peer-urls https://192.168.113.5:2380  --data-dir=/var/lib/etcd/default.etcd

work02执行{etcd03节点}

ETCDCTL_API=3  /opt/etcd/bin/etcdctl snapshot restore  /var/lib/etcd_backup/etcd_20230616.db --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --name etcd-2 --initial-cluster "etcd-1=https://192.168.113.4:2380,etcd-2=https://192.168.113.6:2380,etcd-3=https://192.168.113.5:2380"  --initial-cluster-token etcd-cluster  --initial-advertise-peer-urls https://192.168.113.6:2380  --data-dir=/var/lib/etcd/default.etcd
  1. 为目录赋予权限
  2. 每个节点启动etcd
systemctl start etcd
  1. 查看状态
ETCDCTL_API=3 /opt/etcd/bin/etcdctl \
--cacert=/opt/etcd/ssl/ca.pem \
--cert=/opt/etcd/ssl/server.pem \
--key=/opt/etcd/ssl/server-key.pem \
--endpoints=https://192.168.113.4:2379,https://192.168.113.6:2379,https://192.168.113.5:2379 \
endpoint health --write-out=table
  1. 启动kubeapi-server
systemctl start kube-apiserver

10.查看k8s集群状态

kubectl get cs

你可能感兴趣的:(kubernetes,etcd,运维)