示例服务器:
etcd1:etcd-41 192.168.31.41 etcd2:etcd-42 192.168.31.42 etcd3:etcd-43 192.168.31.43
1、使用注意事项
做快照仅能在etcdctl版本3中来完成,需要配置export ETCDCTL_API=3
,具体方法可以参考文档如下:
参考文档:Kubernetes(k8s) etcd 安装与配置单节点集群方法
2、创建测试数据
在etcd1上创建数据:
[root@etcd1 ~]# etcdctl put k8s1 cjavapy
OK
[root@etcd1 ~]# etcdctl put k8s2 Python
OK
[root@etcd1 ~]# etcdctl get k8s1
k8s1
cjavapy
[root@etcd1 ~]# etcdctl get k8s2
k8s2
Python
3、创建快照
在etcd1上创建快照,命令如下:
[root@etcd1 ~]# etcdctl snap save snap1
Snapshot saved at snap1
[root@etcd1 ~]# ls
snap1
如果有证书,则需加上相关的参数:
etcdctl snap save --cacert=domain1.crt --cert=node1.pem --key=node1.key --endpoints=https://127.0.0.1:2379 snap1
4、测试及恢复数据
1)在etcd1上删除k8s1
和k8s2
[root@etcd1 ~]# etcdctl del k8s1
1
[root@etcd1 ~]# etcdctl del k8s2
1
2)将etcd1上的快照文件snap1传到另外两个服务器上:
scp snap1 192.168.31.42:~ scp snap1 192.168.31.43:~
3)停止所有节点etcd服务并删除数据:
systemctl stop etcd rm -rf /var/lib/etcd/*
4)给每个服务器上快照文件snap1设置权限:
chown etcd.etcd snap1
5)通过快照文件恢复每个节点数据:
etcdctl snapshot restore snap1 --name etcd-41 --initial-cluster etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.31.42:2380,etcd-43=http://192.168.31.43:2380 --initial-advertise-peer-urls http://192.168.31.41:2380 --data-dir /var/lib/etcd/cluster.etcd etcdctl snapshot restore snap1 --name etcd-42 --initial-cluster etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.31.42:2380,etcd-43=http://192.168.31.43:2380 --initial-advertise-peer-urls http://192.168.31.42:2380 --data-dir /var/lib/etcd/cluster.etcd etcdctl snapshot restore snap1 --name etcd-43 --initial-cluster etcd-41=http://192.168.31.41:2380,etcd-42=http://192.168.31.42:2380,etcd-43=http://192.168.31.43:2380 --initial-advertise-peer-urls http://192.168.31.43:2380 --data-dir /var/lib/etcd/cluster.etcd
6)恢复完成后,设置每个节点中/var/lib/etcd/的权限
chown -R etcd.etcd /var/lib/etcd/
7)启动每个服务器上etcd服务
systemctl start etcd
8)测试一下快照之前保存的数据:
[root@etcd1 ~]# etcdctl get k8s1
k8s1
cjavapy
[root@etcd1 ~]# etcdctl get k8s2
k8s2
Python