etcd是一个开源的分布式键值对数据库,他的每一个节点都有一份数据的copy,当有节点故障时保证了高可用性。etcd使用Raft算法来保证一致性。
特别提醒:若使用v3备份数据时存在v2的数据则不影响恢复
若使用v2备份数据时存在v3的数据则恢复失败
etcd V2新增节点(删除数据后重新加入)
适用于单节点故障,重新加入集群
V2与V3新增节点的区别仅在于member add参数
停止etcd服务
# systemctl stop etcd
# 清空etcd data数据
# mv /var/lib/etcd/default.etcd /tmp
# 查看etcd列表
# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://172.30.0.89:2379,https://172.30.0.81:2379,https://172.30.2.10:2379" member list
# remove 已存在的etcd name
# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://172.30.0.89:2379,https://172.30.0.81:2379,https://172.30.2.10:2379" member remove a4ec138b3529c929
# 新增etcd节点
# V2
# /opt/kubernetes/bin/etcdctl --ca-file=/opt/kubernetes/ssl/ca.pem --cert-file=/opt/kubernetes/ssl/server.pem --key-file=/opt/kubernetes/ssl/server-key.pem member add etcd03 https://172.30.0.89:2380
# V3
# ETCDCTL_API=3 /opt/kubernetes/bin/etcdctl --endpoints=https://172.30.2.10:2379,etcd02=https://172.30.0.81:2379,etcd03=https://172.30.0.89:2379 --cacert=/opt/kubernetes/ssl/ca.pem --cert=/opt/kubernetes/ssl/server.pem --key=/opt/kubernetes/ssl/server-key.pem member add etcd03 --peer-urls="https://172.30.0.89:2380"
# 修改该节点etcd配置ETCD_INITIAL_CLUSTER_STATE="existing"
# 同时重启所有etcd
# systemctl restart etcd
etcd数据备份恢复(集群不可用,灾难恢复)
注意:单节点备份一份当前或历史数据即可,在恢复的时候需要同步到所有节点,若只想恢复一台节点的,则会在系统日志里报request cluster ID mismatch
没有节点加入,因为是会创建一个新的集群id ,相当于重新搭建一台新的cluster(数据保留,cluster id变更)
# 停止apiserver(集群不可用状态,确认无写入的话也可以不停)
# 停止etcd服务
systemctl stop etcd
# 数据备份
# ETCDCTL_API=3 /opt/kubernetes/bin/etcdctl snapshot save /var/lib/etcd/etcd_backup/etcd_$(date "+%Y%m%d%H%M%S").db
# 同步备份数据到各个节点
# scp etcd_$(date "+%Y%m%d%H%M%S").db 集群各etcd节点
# 移除数据目录
mv /var/lib/etcd/default.etcd /tmp
# 快照恢复数据 修改name 和 initial-advertise-peer-urls为当前节点的即可
# ETCDCTL_API=3 /opt/kubernetes/bin/etcdctl snapshot --cacert=/opt/kubernetes/ssl/ca.pem --cert=/opt/kubernetes/ssl/server.pem --key=/opt/kubernetes/ssl/server-key.pem restore /var/lib/etcd/etcd_20190903102702.db --name=etcd01 --data-dir=/var/lib/etcd/default.etcd --initial-cluster="etcd01=https://172.30.2.10:2380,etcd02=https://172.30.0.81:2380,etcd03=https://172.30.0.89:2380" --initial-cluster-token="etcd-cluster" --initial-advertise-peer-urls=https://172.30.2.10:2380
# 重启etcd