2021-01-19 17:41:00 +08:00
|
|
|
# WARNNING: this playbook will clean the kube_master node {{ NODE_TO_DEL }}
|
2019-06-03 11:25:05 +08:00
|
|
|
|
2019-07-24 10:19:31 +08:00
|
|
|
- hosts: localhost
|
2019-06-03 11:25:05 +08:00
|
|
|
tasks:
|
2021-01-19 17:41:00 +08:00
|
|
|
- fail: msg="you CAN NOT delete the last member of kube_master!"
|
|
|
|
when: "groups['kube_master']|length < 2 and NODE_TO_DEL in groups['kube_master']"
|
2019-06-03 11:25:05 +08:00
|
|
|
|
2021-10-26 19:24:25 +08:00
|
|
|
- name: 注册变量 K8S 主版本
|
|
|
|
shell: echo {{ K8S_VER }}|awk -F. '{print $1"."$2}'
|
|
|
|
register: K8S_VER_MAIN
|
|
|
|
|
|
|
|
- name: 设置kubectl drain 参数
|
|
|
|
set_fact: DRAIN_OPT="--delete-emptydir-data --ignore-daemonsets --force"
|
|
|
|
when: "K8S_VER_MAIN.stdout|float > 1.19"
|
|
|
|
|
|
|
|
- name: 设置kubectl drain 参数
|
|
|
|
set_fact: DRAIN_OPT="--delete-local-data --ignore-daemonsets --force"
|
|
|
|
when: "K8S_VER_MAIN.stdout|float < 1.20"
|
|
|
|
|
|
|
|
- name: debug info
|
|
|
|
debug: var="DRAIN_OPT"
|
|
|
|
|
2023-01-16 12:33:56 +08:00
|
|
|
- name: get the node name to delete
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl get node -owide|grep ' {{ NODE_TO_DEL }} '|awk '{print $1}'"
|
|
|
|
register: NODE_NAME
|
|
|
|
|
|
|
|
- debug: var="NODE_NAME.stdout"
|
|
|
|
|
|
|
|
- name: run kubectl drain @{{ NODE_NAME.stdout }}
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl drain {{ NODE_NAME.stdout }} {{ DRAIN_OPT }}"
|
2021-01-07 09:30:50 +08:00
|
|
|
#ignore_errors: true
|
2019-06-03 11:25:05 +08:00
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
- name: clean node {{ NODE_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 10:19:31 +08:00
|
|
|
-e NODE_TO_CLEAN={{ NODE_TO_DEL }} \
|
|
|
|
-e DEL_MASTER=yes \
|
|
|
|
-e DEL_NODE=yes \
|
2019-07-24 23:18:52 +08:00
|
|
|
-e DEL_LB=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 {{ NODE_TO_DEL }}'"
|
|
|
|
register: CLEAN_STATUS
|
|
|
|
|
2021-01-07 09:30:50 +08:00
|
|
|
- debug: var="CLEAN_STATUS"
|
2019-11-26 11:06:12 +08:00
|
|
|
|
2023-01-16 12:33:56 +08:00
|
|
|
- name: run kubectl delete node {{ NODE_NAME.stdout }}
|
|
|
|
shell: "{{ base_dir }}/bin/kubectl delete node {{ NODE_NAME.stdout }}"
|
2019-07-24 10:19:31 +08:00
|
|
|
ignore_errors: true
|
2019-07-24 23:18:52 +08:00
|
|
|
|
|
|
|
# lineinfile is inadequate to delete lines between some specific line range
|
|
|
|
- name: remove the master's entry in hosts
|
2021-03-31 23:04:07 +08:00
|
|
|
shell: 'sed -i "/^\[kube_master/,/^\[harbor/ {/^{{ NODE_TO_DEL }}$/d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
|
|
|
|
|
|
|
|
# lineinfile is inadequate to delete lines between some specific line range
|
|
|
|
- name: remove the master's entry in hosts
|
|
|
|
shell: 'sed -i "/^\[kube_master/,/^\[harbor/ {/^{{ NODE_TO_DEL }} /d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
|