1. 程式人生 > >ETCD資料備份與恢復驗證

ETCD資料備份與恢復驗證

ETCD資料備份與恢復驗證

一、單機

說明:執行etcd備份資料的恢復的機器必須和原先etcd所在機器一致

1、單機備份

ETCDCTL_API=3 etcdctl --endpoints="https://10.25.72.62:2379" \
        --cert=/etc/etcd/ssl/etcd.pem \
        --key=/etc/etcd/ssl/etcd-key.pem \
        --cacert=/etc/kubernetes/ssl/ca.pem \
        snapshot save snapshot.db

2、單機資料恢復

[[email protected]
~]# systemctl stop etcd # 停止etcd服務 [[email protected] ~]# export ETCDCTL_API=3; # 使用ETCDCTL API 3 [[email protected] ~]# etcdctl snapshot restore snapshot.db \ --name=SZD-L0105331 \ --endpoints=https://10.25.72.62:2379 \ --cacert=/etc/kubernetes/ssl/ca.pem \ --cert=/etc/etcd/ssl/etcd.pem \ --key=/etc/etcd/ssl/etcd-key.pem \ --initial-cluster=SZD-L0105331=https://10.25.72.62:2380 \ --initial-advertise-peer-urls=https://10.25.72.62:2380 \ --initial-cluster-token=etcd-cluster-0 \ --data-dir=/var/lib/etcd4 # 注意--data-dir引數,以下步驟需要使用 [
[email protected]
~]# # 修改etcd啟動引數--data-dir指向上一步的資料回覆目錄,一般在/etc/etcd/etcd檔案中 [[email protected] ~]# cat /etc/etcd/etcd # 完整引數檔案如下 ETCD_OPTIONS="--name=SZD-L0105331 \ --client-cert-auth=true \ --cert-file=/etc/etcd/ssl/etcd.pem \ --key-file=/etc/etcd/ssl/etcd-key.pem \ --peer-cert-file=/etc/etcd/ssl/etcd.pem \ --peer-key-file=/etc/etcd/ssl/etcd-key.pem \ --trusted-ca-file=/etc/kubernetes/ssl/ca.pem \ --peer-trusted-ca-file=/etc/kubernetes/ssl/ca.pem \ --initial-advertise-peer-urls=https://10.25.72.62:2380 \ --listen-peer-urls=https://10.25.72.62:2380 \ --listen-client-urls=https://10.25.72.62:2379,https://127.0.0.1:2379 \ --advertise-client-urls=https://10.25.72.62:2379 \ --initial-cluster-token=etcd-cluster-0 \ --initial-cluster=SZD-L0105331=https://10.25.72.62:2380 \ --initial-cluster-state=new \ --data-dir=/var/lib/etcd" [
[email protected]
~]# systemctl start etcd # 啟動etcd服務

二、叢集

1、模擬寫入資料到Etcd叢集

# 使用API 3寫入資料庫
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem put /name/1 zxg
# 讀取資料
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem get  /name/1
2018-08-16 20:47:13.017586 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
/name/1
zxg

# 使用API 2寫入資料庫
[[email protected] etcd-cluster]# ETCDCTL_API=2 etcdctl --cert-file /root/etcd-cluster/etcd.pem --ca-file /root/etcd-cluster/ca.pem --key-file /root/etcd-cluster/etcd-key.pem --endpoints="https://10.25.73.25:2379,https://10.25.73.150:2379,https://10.25.84.251:2379" set /name1 zxg1
2018-08-16 18:27:35.060559 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
zxg1
# 讀取資料
[[email protected] etcd-cluster]# ETCDCTL_API=2 etcdctl --cert-file /root/etcd-cluster/etcd.pem --ca-file /root/etcd-cluster/ca.pem --key-file /root/etcd-cluster/etcd-key.pem --endpoints="https://10.25.73.25:2379,https://10.25.73.150:2379,https://10.25.84.251:2379" get /name1
2018-08-16 18:27:47.884908 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
zxg1

2、備份etcd資料

[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem  snapshot save mysnapshot.db
2018-08-16 20:50:47.206334 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
Snapshot saved at mysnapshot.db
[[email protected] etcd-cluster]#

3、停止etcd叢集

  • 停止方法:分別在3臺etcd的宿主機上執行以下命令停止etcd服務
systemctl stop etcd
  • 停掉Leader 10.25.73.25, 檢視叢集狀況,重新選舉出了leader,叢集可正常使用
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem  endpoint status --write-out=table
2018-08-16 18:31:50.686352 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
Failed to get the status of endpoint https://10.25.73.25:2379 (context deadline exceeded)
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
|         ENDPOINT          |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
| https://10.25.84.251:2379 | 2a53eb67dfa37cc0 | 3.1.10  | 25 kB   | false     |        14 |         19 |
| https://10.25.73.150:2379 | c267a0ca02c6bff7 | 3.1.10  | 25 kB   | true      |        14 |         19 |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
[[email protected] etcd-cluster]#
  • 停掉10.25.73.150,檢視叢集狀況,叢集已經無法正常使用,說明3節點的Etcd容錯為1
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem  endpoint status --write-out=table
2018-08-16 18:33:44.526382 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
Failed to get the status of endpoint https://10.25.73.25:2379 (context deadline exceeded)
Failed to get the status of endpoint https://10.25.73.150:2379 (context deadline exceeded)
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
|         ENDPOINT          |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
| https://10.25.84.251:2379 | 2a53eb67dfa37cc0 | 3.1.10  | 25 kB   | false     |        14 |         19 |
+---------------------------+------------------+---------+---------+-----------+-----------+------------+
  • 刪除etcd資料,方法:登入etcd所在主機執行:(注意:危險操作,請謹慎操作,確保在有資料備份並且確定Etcd叢集無法正常工作後操作)
rm -rf /var/lib/etcd

4、使用備份資料進行恢復

  • 恢復10.25.84.251節點資料到/var/lib/etcd
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --name=SZD-L0097856 --endpoints="https://10.25.84.251:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem --initial-cluster-token=etcd-cluster-0 --initial-advertise-peer-urls=https://10.25.84.251:2380 --initial-cluster=SZD-L0101798=https://10.25.73.25:2380,SZD-L0103739=https://10.25.73.150:2380,SZD-L0097856=https://10.25.84.251:2380 --data-dir=/varlib/etcd snapshot restore mysnapshot.db
2018-08-16 19:52:33.409406 I | etcdserver/membership: added member 2a53eb67dfa37cc0 [https://10.25.84.251:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:52:33.409481 I | etcdserver/membership: added member 304bcbfa92e84c75 [https://10.25.73.25:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:52:33.409499 I | etcdserver/membership: added member c267a0ca02c6bff7 [https://10.25.73.150:2380] to cluster 4741a9e2cf17e1fa
[[email protected] etcd-cluster]#
  • 恢復10.25.73.25節點資料到/var/lib/etcd
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --name=SZD-L0101798 --endpoints="https://10.25.73.25:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem --initial-cluster-token=etcd-cluster-0 --initial-advertise-peer-urls=https://10.25.73.25:2380 --initial-cluster=SZD-L0101798=https://10.25.73.25:2380,SZD-L0103739=https://10.25.73.150:2380,SZD-L0097856=https://10.25.84.251:2380 --data-dir=/varlib/etcd snapshot restore mysnapshot.db
2018-08-16 19:55:30.735518 I | etcdserver/membership: added member 2a53eb67dfa37cc0 [https://10.25.84.251:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:55:30.735601 I | etcdserver/membership: added member 304bcbfa92e84c75 [https://10.25.73.25:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:55:30.735617 I | etcdserver/membership: added member c267a0ca02c6bff7 [https://10.25.73.150:2380] to cluster 4741a9e2cf17e1fa
[[email protected] etcd-cluster]#
  • 恢復10.25.73.150節點資料到/var/lib/etcd
[[email protected] etcd-cluster]# ETCDCTL_API=3 etcdctl --name=SZD-L0103739 --endpoints="https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem --initial-cluster-token=etcd-cluster-0 --initial-advertise-peer-urls=https://10.25.73.150:2380 --initial-cluster=SZD-L0101798=https://10.25.73.25:2380,SZD-L0103739=https://10.25.73.150:2380,SZD-L0097856=https://10.25.84.251:2380 --data-dir=/varlib/etcd snapshot restore mysnapshot.db
2018-08-16 19:58:21.892719 I | etcdserver/membership: added member 2a53eb67dfa37cc0 [https://10.25.84.251:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:58:21.892796 I | etcdserver/membership: added member 304bcbfa92e84c75 [https://10.25.73.25:2380] to cluster 4741a9e2cf17e1fa
2018-08-16 19:58:21.892812 I | etcdserver/membership: added member c267a0ca02c6bff7 [https://10.25.73.150:2380] to cluster 4741a9e2cf17e1fa
[[email protected] etcd-cluster]#

5、啟動Etcd服務

  • 分別在etcd所在主機執行如下命令:
systemctl start etcd

6、驗證資料完整性

  • 經過驗證,使用ETCDCTL_API=2 存放的資料會丟失,使用ETCDCTL_API=3存放的資料能正常恢復

附錄:

  • 查詢etcd API3的鍵
[[email protected] ~]#  ETCDCTL_API=3 etcdctl --endpoints="https://10.25.84.251:2379,https://10.25.73.25:2379,https://10.25.73.150:2379" --cert=/root/etcd-cluster/etcd.pem --key=/root/etcd-cluster/etcd-key.pem --cacert=/root/etcd-cluster/ca.pem get / --prefix --keys-only
2018-08-16 20:38:06.954368 I | warning: ignoring ServerName for user-provided CA for backwards compatibility is deprecated
/name/1

/name/2

/name/3

/name/4