Skip to content

Commit

Permalink
fix: etcd集群恢复选主问题(#1193 引入)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjmzj committed Apr 16, 2023
1 parent 608b0bc commit 6693340
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
8 changes: 8 additions & 0 deletions docs/setup/02-install_etcd.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ for ip in ${NODE_IPS}; do
--cert=/etc/kubernetes/ssl/etcd.pem \
--key=/etc/kubernetes/ssl/etcd-key.pem \
endpoint health; done

for ip in ${NODE_IPS}; do
ETCDCTL_API=3 etcdctl \
--endpoints=https://${ip}:2379 \
--cacert=/etc/kubernetes/ssl/ca.pem \
--cert=/etc/kubernetes/ssl/etcd.pem \
--key=/etc/kubernetes/ssl/etcd-key.pem \
--write-out=table endpoint status; done
```
预期结果:
Expand Down
2 changes: 0 additions & 2 deletions playbooks/94.backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
--cert={{ cluster_dir }}/ssl/etcd.pem \
--key={{ cluster_dir }}/ssl/etcd-key.pem \
snapshot save snapshot_{{ timestamp.stdout }}.db"
args:
warn: false

- name: update the latest backup
shell: 'cd {{ cluster_dir }}/backup/ && /bin/cp -f snapshot_{{ timestamp.stdout }}.db snapshot.db'
31 changes: 19 additions & 12 deletions roles/cluster-restore/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
- name: 清除etcd 数据目录
file: name={{ ETCD_DATA_DIR }}/member state=absent

- name: 清除 etcd 备份目录
file: name={{ cluster_dir }}/backup/etcd-restore state=absent
delegate_to: 127.0.0.1
run_once: true
- name: 清理上次备份恢复数据
file: name=/etcd_backup state=absent

- name: 生成备份目录
file: name=/etcd_backup state=directory

- name: 准备指定的备份etcd 数据
copy:
src: "{{ cluster_dir }}/backup/{{ db_to_restore }}"
dest: "/etcd_backup/snapshot.db"

- name: etcd 数据恢复
shell: "cd {{ cluster_dir }}/backup && \
ETCDCTL_API=3 {{ base_dir }}/bin/etcdctl snapshot restore snapshot.db \
--data-dir={{ cluster_dir }}/backup/etcd-restore"
delegate_to: 127.0.0.1
run_once: true

- name: 分发恢复文件到 etcd 各个节点
copy: src={{ cluster_dir }}/backup/etcd-restore/member dest={{ ETCD_DATA_DIR }}
shell: "cd /etcd_backup && \
ETCDCTL_API=3 {{ bin_dir }}/etcdctl snapshot restore snapshot.db \
--name etcd-{{ inventory_hostname }} \
--initial-cluster {{ ETCD_NODES }} \
--initial-cluster-token etcd-cluster-0 \
--initial-advertise-peer-urls https://{{ inventory_hostname }}:2380"

- name: 恢复数据至etcd 数据目录
shell: "cp -rf /etcd_backup/etcd-{{ inventory_hostname }}.etcd/member {{ ETCD_DATA_DIR }}/"

- name: 重启etcd 服务
service: name=etcd state=restarted
Expand Down

0 comments on commit 6693340

Please sign in to comment.