From c04a6254b9330cb9bfcb1f13ab974f83d46f29f8 Mon Sep 17 00:00:00 2001 From: Sergii Golovatiuk Date: Thu, 16 Mar 2017 20:38:13 +0100 Subject: [PATCH] Backup etcd data before restarting etcd etcd is crucial part of kubernetes cluster. Ansible restarts etcd on reconfiguration. Backup helps operator to restore cluster manually in case of any issues. Signed-off-by: Sergii Golovatiuk --- roles/etcd/handlers/backup.yml | 45 ++++++++++++++++++++++++++++++++++ roles/etcd/handlers/main.yml | 3 +++ 2 files changed, 48 insertions(+) create mode 100644 roles/etcd/handlers/backup.yml diff --git a/roles/etcd/handlers/backup.yml b/roles/etcd/handlers/backup.yml new file mode 100644 index 000000000..bfcac72fc --- /dev/null +++ b/roles/etcd/handlers/backup.yml @@ -0,0 +1,45 @@ +--- +- name: Backup etcd data + command: /bin/true + notify: + - Refresh Time Fact + - Set etcd Backup Directory Prefix + - Set Backup Directory + - Create Backup Directory + - Backup etcd v2 data + - Backup etcd v3 data + when: etcd_cluster_is_healthy.rc == 0 + +- name: Refresh Time Fact + setup: filter=ansible_date_time + +- name: Set etcd Backup Directory Prefix + set_fact: + etcd_backup_prefix: '/var/backups' + +- name: Set Backup Directory + set_fact: + etcd_backup_directory: "{{ etcd_backup_prefix }}/etcd-{{ ansible_date_time.date }}_{{ ansible_date_time.time }}" + +- name: Create Backup Directory + file: + path: "{{ etcd_backup_directory }}" + state: directory + owner: root + group: root + mode: 0600 + +- name: Backup etcd v2 data + command: "{{ bin_dir }}/etcdctl backup --data-dir {{ etcd_data_dir }} --backup-dir {{ etcd_backup_directory }}" + environment: + ETCDCTL_API: 2 + retries: 3 + delay: "{{ retry_stagger | random + 3 }}" + +- name: Backup etcd v3 data + command: "{{ bin_dir }}/etcdctl --endpoints={{ etcd_access_addresses }} snapshot save {{ etcd_backup_directory }}/snapshot.db" + environment: + ETCDCTL_API: 3 + retries: 3 + delay: "{{ retry_stagger | random + 3 }}" + diff --git a/roles/etcd/handlers/main.yml b/roles/etcd/handlers/main.yml index 56dd9f431..45da999ee 100644 --- a/roles/etcd/handlers/main.yml +++ b/roles/etcd/handlers/main.yml @@ -2,10 +2,13 @@ - name: restart etcd command: /bin/true notify: + - Backup etcd data - etcd | reload systemd - reload etcd - wait for etcd up +- include: backup.yml + - name: etcd | reload systemd command: systemctl daemon-reload