2019-07-24 23:18:52 +08:00
|
|
|
# WARNNING: this playbook will clean the etcd {{ ETCD_TO_DEL }}
|
2019-06-03 11:25:05 +08:00
|
|
|
|
2019-06-01 22:16:14 +08:00
|
|
|
- hosts: localhost
|
2019-02-17 22:07:27 +08:00
|
|
|
vars_prompt:
|
|
|
|
- name: "ETCD_TO_DEL"
|
|
|
|
prompt: "which etcd node is about to be deleted?(e.g 192.168.1.1)"
|
|
|
|
private: no
|
|
|
|
confirm: yes
|
|
|
|
tasks:
|
2019-07-24 10:19:31 +08:00
|
|
|
# step0: run prechecks
|
|
|
|
- fail: msg="{{ ETCD_TO_DEL }} is NOT a member of etcd cluster!"
|
2019-02-25 22:16:03 +08:00
|
|
|
when: "ETCD_TO_DEL not in groups['etcd']"
|
2019-02-17 22:07:27 +08:00
|
|
|
|
2019-07-24 10:19:31 +08:00
|
|
|
- fail: msg="you CAN NOT delete the last member of etcd cluster!"
|
2019-02-25 22:16:03 +08:00
|
|
|
when: "groups['etcd']|length < 2"
|
2019-02-17 22:07:27 +08:00
|
|
|
|
|
|
|
- block:
|
2019-07-24 10:19:31 +08:00
|
|
|
# step1: find a healthy member in the etcd cluster
|
2019-07-23 11:10:47 +08:00
|
|
|
- name: set NODE_IPS of the etcd cluster
|
|
|
|
set_fact: NODE_IPS="{% for host in groups['etcd'] %}{{ host }} {% endfor %}"
|
|
|
|
|
|
|
|
- name: get etcd cluster status
|
|
|
|
shell: 'for ip in {{ NODE_IPS }};do \
|
|
|
|
ETCDCTL_API=3 {{ base_dir }}/bin/etcdctl \
|
|
|
|
--endpoints=https://"$ip":2379 \
|
2020-12-25 11:53:00 +08:00
|
|
|
--cacert={{ cluster_dir }}/ssl/ca.pem \
|
2020-12-30 11:25:54 +08:00
|
|
|
--cert={{ cluster_dir }}/ssl/etcd.pem \
|
|
|
|
--key={{ cluster_dir }}/ssl/etcd-key.pem \
|
2019-07-23 11:10:47 +08:00
|
|
|
endpoint health; \
|
|
|
|
done'
|
|
|
|
register: ETCD_CLUSTER_STATUS
|
2019-07-24 23:18:52 +08:00
|
|
|
ignore_errors: true
|
2019-07-23 11:10:47 +08:00
|
|
|
|
2020-11-19 20:19:27 +08:00
|
|
|
- debug: var="ETCD_CLUSTER_STATUS"
|
2019-11-26 11:06:12 +08:00
|
|
|
|
2019-07-23 11:10:47 +08:00
|
|
|
- name: get a running ectd node
|
2019-11-26 11:06:12 +08:00
|
|
|
shell: 'echo -e "{{ ETCD_CLUSTER_STATUS.stdout }}" \
|
|
|
|
"{{ ETCD_CLUSTER_STATUS.stderr }}" \
|
|
|
|
|grep "is healthy"|sed -n "1p"|cut -d: -f2|cut -d/ -f3'
|
2019-07-23 11:10:47 +08:00
|
|
|
register: RUNNING_NODE
|
|
|
|
|
|
|
|
- debug: var="RUNNING_NODE.stdout"
|
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
# step2: remove jobs run on the healthy member
|
2019-02-17 22:07:27 +08:00
|
|
|
- name: get ID of etcd node to delete
|
2019-11-26 11:06:12 +08:00
|
|
|
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member list \
|
|
|
|
|grep {{ ETCD_TO_DEL }}:2380|cut -d',' -f1"
|
2019-02-17 22:07:27 +08:00
|
|
|
register: ETCD_ID
|
2019-07-23 11:10:47 +08:00
|
|
|
delegate_to: "{{ RUNNING_NODE.stdout }}"
|
2019-02-17 22:07:27 +08:00
|
|
|
|
|
|
|
- name: get NAME of etcd node to delete
|
2019-11-26 11:06:12 +08:00
|
|
|
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member list \
|
|
|
|
|grep {{ ETCD_TO_DEL }}:2380|cut -d' ' -f3|cut -d',' -f1"
|
2019-02-17 22:07:27 +08:00
|
|
|
register: ETCD_NAME
|
2019-07-23 11:10:47 +08:00
|
|
|
delegate_to: "{{ RUNNING_NODE.stdout }}"
|
2019-02-17 22:07:27 +08:00
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
- debug: var="ETCD_NAME.stdout"
|
|
|
|
|
2019-02-17 22:07:27 +08:00
|
|
|
- name: delete a etcd member
|
|
|
|
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member remove {{ ETCD_ID.stdout }}"
|
2019-07-23 11:10:47 +08:00
|
|
|
delegate_to: "{{ RUNNING_NODE.stdout }}"
|
2019-02-17 22:07:27 +08:00
|
|
|
when: "ETCD_ID.stdout != ''"
|
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
- name: clean etcd {{ ETCD_TO_DEL }}
|
2021-01-20 23:03:47 +08:00
|
|
|
shell: "cd {{ base_dir }} && ansible-playbook -i clusters/{{ CLUSTER }}/hosts \
|
|
|
|
roles/clean/clean_node.yml \
|
2019-07-24 23:18:52 +08:00
|
|
|
-e NODE_TO_CLEAN={{ ETCD_TO_DEL }} \
|
|
|
|
-e DEL_ETCD=yes >> /tmp/ansible-`date +'%Y%m%d%H%M%S'`.log 2>&1 \
|
2019-07-24 10:19:31 +08:00
|
|
|
|| echo 'data not cleaned on {{ ETCD_TO_DEL }}'"
|
|
|
|
register: CLEAN_STATUS
|
2019-07-23 22:22:52 +08:00
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
- debug: var="CLEAN_STATUS"
|
2019-07-23 22:22:52 +08:00
|
|
|
|
2019-07-24 10:19:31 +08:00
|
|
|
# lineinfile is inadequate to delete lines between some specific line range
|
2019-07-23 11:10:47 +08:00
|
|
|
- name: remove the etcd's node entry in hosts
|
2021-03-31 23:04:07 +08:00
|
|
|
shell: 'sed -i "/^\[etcd/,/^\[kube_master/ {/^{{ ETCD_TO_DEL }}$/d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
|
|
|
|
|
|
|
|
# lineinfile is inadequate to delete lines between some specific line range
|
|
|
|
- name: remove the etcd's node entry in hosts
|
|
|
|
shell: 'sed -i "/^\[etcd/,/^\[kube_master/ {/^{{ ETCD_TO_DEL }} /d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
|
2019-02-17 22:07:27 +08:00
|
|
|
when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"
|