ETCD 存储 k8s 所有数据信息
ETCD 是k8s集群极为重要的一块服务,存储了集群所有的数据信息。同理,如果发生灾难或者 etcd 的数据丢失,都会影响集群数据的恢复,k8s中只用kube-apiserver和etcd进行数据交互。etcdctl版本: 3.3.18,kubernetes: v1.17.3。均采用二进制安装
ETCD 一些查询操作
查看集群状态
1
2
3
4
5ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://172.21.17.32:2379,https://172.21.17.18:2379,https://172.21.16.204:2379 endpoint health
https://172.21.16.204:2379 is healthy: successfully committed proposal: took = 15.614765ms
https://172.21.17.32:2379 is healthy: successfully committed proposal: took = 40.200694ms
https://172.21.17.18:2379 is healthy: successfully committed proposal: took = 230.022141ms获取某个 key 信息
1
2
3ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://172.21.17.32:2379,https://172.21.17.18:2379,https://172.21.16.204:2379 get /registry/apiregistration.k8s.io/apiservices/v1.apps
{"kind":"APIService","apiVersion":"apiregistration.k8s.io/v1beta1","metadata":{"name":"v1.apps","uid":"5790ef34-84c1-432e-8ed8-4fe842c43dfe","creationTimestamp":"2020-03-26T07:46:37Z","labels":{"kube-aggregator.kubernetes.io/automanaged":"onstart"}},"spec":{"service":null,"group":"apps","version":"v1","groupPriorityMinimum":17800,"versionPriority":15},"status":{"conditions":[{"type":"Available","status":"True","lastTransitionTime":"2020-03-26T07:46:37Z","reason":"Local","message":"Local APIServices are always available"}]}}获取 etcd 版本信息
1
2
3
4ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://172.21.17.32:2379,https://172.21.17.18:2379,https://172.21.16.204:2379 version
etcdctl version: 3.3.18
API version: 3.3获取 ETCD 所有的 key
1
2
3
4ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/cert/ca.pem --cert=/etc/etcd/cert/etcd.pem --key=/etc/etcd/cert/etcd-key.pem --endpoints=https://172.21.17.32:2379,https://172.21.17.18:2379,https://172.21.16.204:2379 get / --prefix --keys-only
# 数据很多
………………
etcd 备份
在其中一台服务器操作即可
命令备份
1 | ENDPOINTS="https://172.21.17.32:2379,https://172.21.17.18:2379,https://172.21.16.204:2379" |
脚本进行备份
1 | cat >etcd-back.sh <<EOF |
恢复
准备工作
停止所有 Master 上 kube-apiserver 服务
1
2
3systemctl stop kube-apiserver
ps -ef | grep kube-apiserver停止集群中所有 ETCD 服务
1
2
3systemctl stop etcd
systemctl status etcd
移除所有 ETCD 存储目录下数据
1 | mkdir /tmp/etcd_backup/{data,wal} |
拷贝 ETCD 备份快照
1 | # 在备份的服务器上拷贝 |
恢复备份
这里etcd
的数据和日志文件是分开存放的,所以在恢复的时候需要分别制定相应的目录。/var/lib/ecd
移除下面的data,wal
目录以后,在恢复的时候会自动的创建。不需要手动创建
1 | # k8s-master01 机器上操作 |
启动和检查etcd
1 | # 三台 ETCD 都恢复完成后,依次登陆三台机器启动 ETCD |
启动kube-apiserver
1 | # ETCD 全部健康,分别到每台 Master 启动 kube-apiserver |
总结
Kubernetes 集群备份主要是备份 ETCD 集群。而恢复时,主要考虑恢复整个顺序:停止kube-apiserver --> 停止ETCD --> 恢复数据 --> 启动ETCD --> 启动kube-apiserve
- 注意:备份ETCD集群时,只需要备份一个ETCD就行,恢复时,拿同一份备份数据恢复。