forked from easzlab/kubeasz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
32.delnode.yml
54 lines (41 loc) · 2.13 KB
/
32.delnode.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# WARNNING: this playbook will clean the node {{ NODE_TO_DEL }}
- hosts: localhost
tasks:
- 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']"
- 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"
- 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 }}"
#ignore_errors: true
- name: clean node {{ NODE_TO_DEL }}
shell: "cd {{ base_dir }} && ansible-playbook -i clusters/{{ CLUSTER }}/hosts \
roles/clean/clean_node.yml \
-e NODE_TO_CLEAN={{ NODE_TO_DEL }} \
-e DEL_NODE=yes \
-e DEL_LB=yes >> /tmp/ansible-`date +'%Y%m%d%H%M%S'`.log 2>&1 \
|| echo 'data not cleaned on {{ NODE_TO_DEL }}'"
register: CLEAN_STATUS
- debug: var="CLEAN_STATUS"
- name: run kubectl delete node {{ NODE_NAME.stdout }}
shell: "{{ base_dir }}/bin/kubectl delete node {{ NODE_NAME.stdout }}"
ignore_errors: true
# lineinfile is inadequate to delete lines between some specific line range
- name: remove the node's entry in hosts
shell: 'sed -i "/^\[kube_node/,/^\[harbor/ {/^{{ NODE_TO_DEL }}$/d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
# lineinfile is inadequate to delete lines between some specific line range
- name: remove the node's entry in hosts
shell: 'sed -i "/^\[kube_node/,/^\[harbor/ {/^{{ NODE_TO_DEL }} /d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'