-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.yml
More file actions
148 lines (130 loc) · 4.85 KB
/
Copy pathclean.yml
File metadata and controls
148 lines (130 loc) · 4.85 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
- name: Clean up Kubernetes Nodes
hosts: all
become: true
vars_files:
- vault_become_pass.yml
vars:
is_master: "{{ inventory_hostname in groups['master_nodes'] | default([]) }}"
is_worker: "{{ inventory_hostname in groups['worker_nodes'] | default([]) }}"
tasks:
- name: Execute Kubernetes cleanup
block:
- name: Stop Kubernetes services
block:
- name: Stop kubelet service
systemd:
name: kubelet
state: stopped
enabled: no
ignore_errors: true
- name: Stop containerd service
systemd:
name: containerd
state: stopped
enabled: no
ignore_errors: true
- name: Reset Kubernetes configuration
block:
- name: Reset kubeadm on all nodes
shell: kubeadm reset --cri-socket=unix:///var/run/containerd/containerd.sock -f
register: reset_result
ignore_errors: true
- name: Remove etcd data (master nodes only)
file:
path: /var/lib/etcd
state: absent
when: is_master
ignore_errors: true
- name: Clean up Kubernetes directories
file:
path: "{{ item }}"
state: absent
loop:
- /var/lib/kubelet
- /etc/kubernetes
- /home/{{ ansible_user }}/.kube
- /etc/cni/net.d
- /var/lib/dockershim
- /var/run/kubernetes
- /var/lib/cni
- /opt/cni/bin
ignore_errors: true
- name: Clean up container runtime
block:
- name: Remove containerd state
file:
path: /var/lib/containerd
state: absent
ignore_errors: true
- name: Remove Docker directory (if used)
file:
path: /var/lib/docker
state: absent
ignore_errors: true
- name: Clean up network configuration
block:
- name: Remove iptables rules
shell: iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
ignore_errors: true
- name: Remove IP forwarding settings
sysctl:
name: net.bridge.bridge-nf-call-iptables
value: '0'
state: absent
ignore_errors: true
- name: Clean up any remaining flannel artifacts
file:
path: /run/flannel/subnet.env
state: absent
ignore_errors: true
- name: Master node specific cleanup
block:
#- name: Backup etcd (if needed)
# shell: mkdir -p /tmp/etcd-backup && ETCDCTL_API=3 etcdctl snapshot save /tmp/etcd-backup/etcd-snapshot-$(date +%Y%m%d-%H%M%S).db --endpoints=localhost:2379
# ignore_errors: true
- name: Remove control plane static pod manifests
file:
path: /etc/kubernetes/manifests
state: absent
ignore_errors: true
when: is_master
- name: Worker node specific cleanup
block:
- name: Remove kubelet config
file:
path: /var/lib/kubelet/config.yaml
state: absent
ignore_errors: true
- name: Drain node (if cluster is still running)
shell: "kubectl drain {{ inventory_hostname }} --ignore-daemonsets --delete-emptydir-data --force"
delegate_to: "{{ groups['master_nodes'][0] }}"
ignore_errors: true
when: groups['master_nodes'] | length > 0
when: is_worker
- name: Restart services
block:
- name: Restart containerd
systemd:
name: containerd
state: restarted
ignore_errors: true
- name: Start kubelet service
systemd:
name: kubelet
state: started
enabled: yes
ignore_errors: true
- name: Reboot node
reboot:
reboot_timeout: 600
ignore_errors: true
- name: Wait for node to come back online
wait_for_connection:
delay: 60
timeout: 300
ignore_errors: true
rescue:
- name: Handle cleanup failure
debug:
msg: "Cleanup failed on {{ inventory_hostname }}. Please manually inspect and clean up the system. Check disk space, network configuration, and Kubernetes component logs."