|
1 | 1 | # WARNING: This playbook will erase the entire k8s-cluster, include PODs, ETCD data etc.
|
2 | 2 | # Make sure you know what you are doing.
|
3 | 3 |
|
4 |
| -# to clean 'kube-node' nodes |
5 | 4 | - hosts:
|
6 | 5 | - kube-master
|
7 |
| - - kube-node |
8 |
| - tasks: |
9 |
| - - name: stop and disable kube-node service |
10 |
| - service: name={{ item }} state=stopped enabled=no |
11 |
| - with_items: |
12 |
| - - kubelet |
13 |
| - - kube-proxy |
14 |
| - ignore_errors: true |
15 |
| - |
16 |
| - - name: umount kubelet filesystems |
17 |
| - shell: "mount | grep '/var/lib/kubelet'| awk '{print $3}'|xargs umount || exit 0" |
18 |
| - args: |
19 |
| - warn: false |
20 |
| - ignore_errors: true |
21 |
| - |
22 |
| - - name: remove files and dirs of 'kube-node' nodes |
23 |
| - file: name={{ item }} state=absent |
24 |
| - with_items: |
25 |
| - - "/var/lib/kubelet/" |
26 |
| - - "/var/lib/kube-proxy/" |
27 |
| - - "/etc/systemd/system/kubelet.service" |
28 |
| - - "/etc/systemd/system/kube-proxy.service" |
29 |
| - - "/opt/kube/kube-system/" |
30 |
| - |
31 |
| -# to clean 'kube-master' nodes |
32 |
| -- hosts: |
33 |
| - - kube-master |
34 |
| - tasks: |
35 |
| - - name: stop and disable kube-master service |
36 |
| - service: name={{ item }} state=stopped enabled=no |
37 |
| - with_items: |
38 |
| - - kube-apiserver |
39 |
| - - kube-controller-manager |
40 |
| - - kube-scheduler |
41 |
| - ignore_errors: true |
42 |
| - |
43 |
| - - name: remove files and dirs of 'kube-master' nodes |
44 |
| - file: name={{ item }} state=absent |
45 |
| - with_items: |
46 |
| - - "/var/run/kubernetes" |
47 |
| - - "/etc/systemd/system/kube-apiserver.service" |
48 |
| - - "/etc/systemd/system/kube-controller-manager.service" |
49 |
| - - "/etc/systemd/system/kube-scheduler.service" |
50 |
| - |
51 |
| -# to clean container runtime and networking |
52 |
| -- hosts: |
53 |
| - - kube-master |
54 |
| - - kube-node |
55 |
| - tasks: |
56 |
| - - block: |
57 |
| - - name: to check if container 'kubeasz' is running |
58 |
| - shell: 'docker ps|grep kubeasz || echo "NOT FOUND"' |
59 |
| - register: install_info |
60 |
| - |
61 |
| - - name: fail info |
62 |
| - fail: msg="you CAN NOT delete dockerd, because container 'kubeasz' is running!" |
63 |
| - when: "'kubeasz' in install_info.stdout" |
64 |
| - |
65 |
| - - name: stop and disable docker service |
66 |
| - service: |
67 |
| - name: docker |
68 |
| - state: stopped |
69 |
| - enabled: no |
70 |
| - ignore_errors: true |
71 |
| - |
72 |
| - # as k8s-network-plugins use host-network, '/var/run/docker/netns/default' must be umounted |
73 |
| - - name: unmount docker filesystem-1 |
74 |
| - mount: path=/var/run/docker/netns/default state=unmounted |
75 |
| - |
76 |
| - - name: unmount docker filesystem-2 |
77 |
| - mount: path=/var/lib/docker/overlay state=unmounted |
78 |
| - |
79 |
| - - name: remove files and dirs |
80 |
| - file: name={{ item }} state=absent |
81 |
| - with_items: |
82 |
| - - "/var/lib/docker/" |
83 |
| - - "/var/run/docker/" |
84 |
| - - "/etc/systemd/system/docker.service" |
85 |
| - - "/etc/systemd/system/docker.service.requires/" |
86 |
| - - "/etc/systemd/system/docker.service.d/" |
87 |
| - - "/etc/bash_completion.d/docker" |
88 |
| - - "/usr/bin/docker" |
89 |
| - when: CONTAINER_RUNTIME == 'docker' |
90 |
| - |
91 |
| - - block: |
92 |
| - - name: stop and disable containerd service |
93 |
| - service: |
94 |
| - name: containerd |
95 |
| - state: stopped |
96 |
| - enabled: no |
97 |
| - ignore_errors: true |
98 |
| - |
99 |
| - - name: umount containerd filesystems |
100 |
| - shell: "mount | grep 'containerd/io.containerd'| awk '{print $3}'|xargs umount || exit 0" |
101 |
| - args: |
102 |
| - warn: false |
103 |
| - ignore_errors: true |
104 |
| - |
105 |
| - - name: remove files and dirs |
106 |
| - file: name={{ item }} state=absent |
107 |
| - with_items: |
108 |
| - - "/etc/containerd/" |
109 |
| - - "/etc/crictl.yaml" |
110 |
| - - "/etc/systemd/system/containerd.service" |
111 |
| - - "/opt/containerd/" |
112 |
| - - "/var/lib/containerd/" |
113 |
| - - "/var/run/containerd/" |
114 |
| - when: CONTAINER_RUNTIME == 'containerd' |
115 |
| - |
116 |
| - - name: remove files and dirs2 |
117 |
| - file: name={{ item }} state=absent |
118 |
| - with_items: |
119 |
| - - "/etc/cni/" |
120 |
| - - "/run/flannel/" |
121 |
| - - "/etc/calico/" |
122 |
| - - "/var/run/calico/" |
123 |
| - - "/var/lib/calico/" |
124 |
| - - "/var/log/calico/" |
125 |
| - - "/etc/cilium/" |
126 |
| - - "/var/run/cilium/" |
127 |
| - - "/sys/fs/bpf/tc/" |
128 |
| - - "/var/lib/cni/" |
129 |
| - - "/var/lib/kube-router/" |
130 |
| - - "/opt/kube/kube-system/" |
131 |
| - - "/var/run/openvswitch/" |
132 |
| - - "/etc/origin/openvswitch/" |
133 |
| - - "/etc/openvswitch/" |
134 |
| - - "/var/log/openvswitch/" |
135 |
| - |
136 |
| - - name: cleanup iptables |
137 |
| - shell: "iptables -F && iptables -X \ |
138 |
| - && iptables -F -t nat && iptables -X -t nat \ |
139 |
| - && iptables -F -t raw && iptables -X -t raw \ |
140 |
| - && iptables -F -t mangle && iptables -X -t mangle" |
141 |
| - |
142 |
| - - name: cleanup networks1 |
143 |
| - shell: "ip link del tunl0; \ |
144 |
| - ip link del flannel.1; \ |
145 |
| - ip link del cni0; \ |
146 |
| - ip link del mynet0; \ |
147 |
| - ip link del kube-bridge; \ |
148 |
| - ip link del dummy0; \ |
149 |
| - ip link del kube-ipvs0; \ |
150 |
| - ip link del cilium_net; \ |
151 |
| - ip link del cilium_vxlan; \ |
152 |
| - ip link del ovn0; \ |
153 |
| - ip link del ovs-system" |
154 |
| - ignore_errors: true |
155 |
| - |
156 |
| - - name: cleanup networks2 |
157 |
| - shell: "ip link del docker0; \ |
158 |
| - systemctl restart networking; \ |
159 |
| - systemctl restart network" |
160 |
| - ignore_errors: true |
161 |
| - |
162 |
| - - name: cleanup 'calico' routes |
163 |
| - shell: "for rt in `ip route|grep bird|sed 's/blackhole //'|awk '{print $1}'`;do ip route del $rt;done;" |
164 |
| - when: "CLUSTER_NETWORK == 'calico'" |
165 |
| - ignore_errors: true |
166 |
| - |
167 |
| -# to clean 'etcd' nodes |
168 |
| -- hosts: etcd |
169 |
| - tasks: |
170 |
| - - name: stop and disable etcd service |
171 |
| - service: |
172 |
| - name: etcd |
173 |
| - state: stopped |
174 |
| - enabled: no |
175 |
| - ignore_errors: true |
176 |
| - |
177 |
| - - name: remove files and dirs |
178 |
| - file: name={{ item }} state=absent |
179 |
| - with_items: |
180 |
| - - "/var/lib/etcd" |
181 |
| - - "/etc/etcd/" |
182 |
| - - "/backup/k8s" |
183 |
| - - "/etc/systemd/system/etcd.service" |
184 |
| - |
185 |
| -# to clean 'lb' nodes |
186 |
| -- hosts: |
187 | 6 | - kube-node
|
188 | 7 | - ex-lb
|
189 |
| - tasks: |
190 |
| - - name: stop keepalived service |
191 |
| - shell: systemctl disable keepalived && systemctl stop keepalived |
192 |
| - ignore_errors: true |
193 |
| - |
194 |
| - - name: stop haproxy service |
195 |
| - shell: systemctl disable haproxy && systemctl stop haproxy |
196 |
| - ignore_errors: true |
197 |
| - |
198 |
| - - name: remove files and dirs |
199 |
| - file: name={{ item }} state=absent |
200 |
| - with_items: |
201 |
| - - "/etc/haproxy" |
202 |
| - - "/etc/keepalived" |
203 |
| - |
204 |
| -# to clean ntp, certs and keys, env path |
205 |
| -- hosts: |
206 |
| - - kube-master |
207 |
| - - kube-node |
208 | 8 | - etcd
|
209 |
| - - ex-lb |
210 |
| - tasks: |
211 |
| - - block: |
212 |
| - - name: stop and disable chrony in Ubuntu |
213 |
| - service: name=chrony state=stopped enabled=no |
214 |
| - ignore_errors: true |
215 |
| - when: 'ansible_distribution in ["Ubuntu","Debian"]' |
216 |
| - |
217 |
| - - name: stop and disable chronyd in CentOS/RedHat |
218 |
| - service: name=chronyd state=stopped enabled=no |
219 |
| - ignore_errors: true |
220 |
| - when: 'ansible_distribution in ["CentOS","RedHat","Amazon"]' |
221 |
| - when: "groups['chrony']|length > 0" |
222 |
| - |
223 |
| - - name: clean certs and keys |
224 |
| - file: name={{ item }} state=absent |
225 |
| - with_items: |
226 |
| - - "/etc/kubernetes/" |
227 |
| - - "{{ ca_dir }}" |
228 |
| - - "/root/.kube/config" |
229 |
| - - "/etc/docker/" |
230 |
| - |
231 |
| - - name: clean 'ENV PATH' |
232 |
| - lineinfile: |
233 |
| - dest: ~/.bashrc |
234 |
| - state: absent |
235 |
| - regexp: '{{ item }}' |
236 |
| - with_items: |
237 |
| - - 'kubeasz' |
238 |
| - - 'helm completion' |
239 |
| - - 'kubectl completion' |
240 |
| - - 'crictl completion' |
241 |
| - - 'HELM_TLS_ENABLE' |
| 9 | + vars: |
| 10 | + DEL_MASTER: "yes" |
| 11 | + DEL_NODE: "yes" |
| 12 | + DEL_ETCD: "yes" |
| 13 | + DEL_LB: "yes" |
| 14 | + DEL_CHRONY: "yes" |
| 15 | + DEL_ENV: "yes" |
| 16 | + roles: |
| 17 | + - clean |
0 commit comments