2019-02-26 15:04:16 +08:00
|
|
|
|
# 警告:此脚本将清理单个node节点,使用请详细参阅 docs/op/clean_one_node.md
|
2018-06-20 11:48:29 +08:00
|
|
|
|
# 请三思后运行此脚本,特别的:如果有pod使用了本地存储类型,请自行判断重要性
|
|
|
|
|
# 使用:
|
2019-02-19 16:38:10 +08:00
|
|
|
|
# 1. 执行 ansible-playbook /etc/ansible/tools/clean_one_node.yml
|
|
|
|
|
# 2. 按照提示输入待删除节点
|
2018-06-20 11:48:29 +08:00
|
|
|
|
|
|
|
|
|
- hosts: deploy
|
2019-02-19 16:38:10 +08:00
|
|
|
|
vars_prompt:
|
|
|
|
|
- name: "NODE_TO_DEL"
|
|
|
|
|
prompt: "which node is about to be deleted?(e.g 192.168.1.1)"
|
|
|
|
|
private: no
|
|
|
|
|
confirm: yes
|
2018-06-20 11:48:29 +08:00
|
|
|
|
tasks:
|
2019-02-25 22:16:03 +08:00
|
|
|
|
- name: fail info1
|
|
|
|
|
fail: msg="you CAN NOT delete the last member of etcd cluster!"
|
|
|
|
|
when: "groups['etcd']|length < 2 and NODE_TO_DEL in groups['etcd']"
|
|
|
|
|
|
|
|
|
|
- name: fail info2
|
|
|
|
|
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']"
|
|
|
|
|
|
2018-06-20 11:48:29 +08:00
|
|
|
|
- name: 执行kubectl drain(节点可能是kube-node节点)
|
2019-02-19 16:38:10 +08:00
|
|
|
|
shell: "{{ bin_dir }}/kubectl drain {{ NODE_TO_DEL }} --ignore-daemonsets --delete-local-data"
|
2018-06-20 11:48:29 +08:00
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
2019-02-19 16:38:10 +08:00
|
|
|
|
- block:
|
|
|
|
|
# 清理 kube-node 相关服务
|
|
|
|
|
- name: stop and disable kube-node service
|
|
|
|
|
service: name={{ item }} state=stopped enabled=no
|
|
|
|
|
with_items:
|
|
|
|
|
- kubelet
|
|
|
|
|
- kube-proxy
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: umount kubelet 挂载的目录
|
|
|
|
|
shell: "mount | grep '/var/lib/kubelet'| awk '{print $3}'|xargs umount"
|
|
|
|
|
args:
|
|
|
|
|
warn: false
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: 清理目录和文件
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/var/lib/kubelet/"
|
|
|
|
|
- "/var/lib/kube-proxy/"
|
|
|
|
|
- "/etc/systemd/system/kubelet.service"
|
|
|
|
|
- "/etc/systemd/system/kube-proxy.service"
|
|
|
|
|
- "/opt/kube/kube-system/"
|
|
|
|
|
|
|
|
|
|
# 清理 kube-master 相关
|
|
|
|
|
- name: stop and disable kube-master service
|
|
|
|
|
service: name={{ item }} state=stopped enabled=no
|
|
|
|
|
with_items:
|
|
|
|
|
- kube-apiserver
|
|
|
|
|
- kube-controller-manager
|
|
|
|
|
- kube-scheduler
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: 清理目录和文件
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/var/run/kubernetes"
|
|
|
|
|
- "/etc/systemd/system/kube-apiserver.service"
|
|
|
|
|
- "/etc/systemd/system/kube-controller-manager.service"
|
|
|
|
|
- "/etc/systemd/system/kube-scheduler.service"
|
|
|
|
|
|
|
|
|
|
# 清理集群docker服务、网络相关
|
2019-05-19 08:51:01 +08:00
|
|
|
|
- block:
|
|
|
|
|
- name: 清理kube-router相关
|
|
|
|
|
shell: "{{ bin_dir }}/docker run --privileged --net=host cloudnativelabs/kube-router --cleanup-config"
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
when: "CLUSTER_NETWORK == 'kube-router'"
|
|
|
|
|
|
|
|
|
|
- name: 获取是否运行名为'kubeasz'的容器
|
|
|
|
|
shell: 'docker ps|grep kubeasz || echo "NOT FOUND"'
|
|
|
|
|
register: install_info
|
|
|
|
|
|
|
|
|
|
- name: stop and disable docker service
|
|
|
|
|
service:
|
|
|
|
|
name: docker
|
|
|
|
|
state: stopped
|
|
|
|
|
enabled: no
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
when: "'kubeasz' not in install_info.stdout"
|
|
|
|
|
|
|
|
|
|
- name: unmount docker filesystem-1
|
|
|
|
|
mount: path=/var/run/docker/netns/default state=unmounted
|
|
|
|
|
when: "'kubeasz' not in install_info.stdout"
|
|
|
|
|
|
|
|
|
|
- name: unmount docker filesystem-2
|
|
|
|
|
mount: path=/var/lib/docker/overlay state=unmounted
|
|
|
|
|
when: "'kubeasz' not in install_info.stdout"
|
|
|
|
|
|
|
|
|
|
- name: remove files and dirs
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/var/lib/docker/"
|
|
|
|
|
- "/var/run/docker/"
|
|
|
|
|
- "/etc/systemd/system/docker.service"
|
|
|
|
|
- "/etc/systemd/system/docker.service.requires/"
|
|
|
|
|
- "/etc/systemd/system/docker.service.d/"
|
|
|
|
|
- "/etc/bash_completion.d/docker"
|
|
|
|
|
- "/usr/bin/docker"
|
|
|
|
|
when: "'kubeasz' not in install_info.stdout"
|
|
|
|
|
when: CONTAINER_RUNTIME == 'docker'
|
|
|
|
|
|
|
|
|
|
- block:
|
|
|
|
|
- name: stop and disable containerd service
|
|
|
|
|
service:
|
|
|
|
|
name: containerd
|
|
|
|
|
state: stopped
|
|
|
|
|
enabled: no
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: umount containerd filesystems
|
|
|
|
|
shell: "mount | grep 'containerd/io.containerd'| awk '{print $3}'|xargs umount || exit 0"
|
|
|
|
|
args:
|
|
|
|
|
warn: false
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: remove files and dirs
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/etc/containerd/"
|
|
|
|
|
- "/etc/crictl.yaml"
|
|
|
|
|
- "/etc/systemd/system/containerd.service"
|
|
|
|
|
- "/opt/containerd/"
|
|
|
|
|
- "/var/lib/containerd/"
|
|
|
|
|
- "/var/run/containerd/"
|
|
|
|
|
when: CONTAINER_RUNTIME == 'containerd'
|
|
|
|
|
|
2019-02-28 18:27:57 +08:00
|
|
|
|
- name: remove files and dirs2
|
2019-02-19 16:38:10 +08:00
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/etc/cni/"
|
|
|
|
|
- "/run/flannel/"
|
|
|
|
|
- "/etc/calico/"
|
|
|
|
|
- "/var/run/calico/"
|
|
|
|
|
- "/var/lib/calico/"
|
|
|
|
|
- "/var/log/calico/"
|
|
|
|
|
- "/etc/cilium/"
|
|
|
|
|
- "/var/run/cilium/"
|
|
|
|
|
- "/sys/fs/bpf/tc/"
|
|
|
|
|
- "/var/lib/cni/"
|
|
|
|
|
- "/var/lib/kube-router/"
|
|
|
|
|
- "/opt/kube/kube-system/"
|
|
|
|
|
|
2019-02-28 18:27:57 +08:00
|
|
|
|
- name: cleanup iptables
|
2019-02-19 16:38:10 +08:00
|
|
|
|
shell: "iptables -F && iptables -X \
|
2019-02-28 18:27:57 +08:00
|
|
|
|
&& iptables -F -t nat && iptables -X -t nat \
|
|
|
|
|
&& iptables -F -t raw && iptables -X -t raw \
|
|
|
|
|
&& iptables -F -t mangle && iptables -X -t mangle"
|
2019-02-19 16:38:10 +08:00
|
|
|
|
|
2019-02-28 18:27:57 +08:00
|
|
|
|
- name: cleanup networks1
|
|
|
|
|
shell: "ip link del tunl0; \
|
2019-02-19 16:38:10 +08:00
|
|
|
|
ip link del flannel.1; \
|
|
|
|
|
ip link del cni0; \
|
|
|
|
|
ip link del mynet0; \
|
|
|
|
|
ip link del kube-bridge; \
|
|
|
|
|
ip link del dummy0; \
|
|
|
|
|
ip link del kube-ipvs0; \
|
|
|
|
|
ip link del cilium_net; \
|
2019-02-28 18:27:57 +08:00
|
|
|
|
ip link del cilium_vxlan"
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: cleanup networks2
|
|
|
|
|
shell: "ip link del docker0; \
|
2019-02-19 16:38:10 +08:00
|
|
|
|
systemctl restart networking; \
|
2019-02-28 18:27:57 +08:00
|
|
|
|
systemctl restart network"
|
2019-02-19 16:38:10 +08:00
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
2019-02-28 18:27:57 +08:00
|
|
|
|
- name: cleanup 'calico' routes
|
2019-02-19 16:38:10 +08:00
|
|
|
|
shell: "for rt in `ip route|grep bird|sed 's/blackhole//'|awk '{print $1}'`;do ip route del $rt;done;"
|
|
|
|
|
when: "CLUSTER_NETWORK == 'calico'"
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
# 清理etcd 集群相关
|
|
|
|
|
- name: stop and disable etcd service
|
|
|
|
|
service:
|
|
|
|
|
name: etcd
|
|
|
|
|
state: stopped
|
|
|
|
|
enabled: no
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: 清理目录和文件
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/var/lib/etcd"
|
|
|
|
|
- "/etc/etcd/"
|
|
|
|
|
- "/backup/k8s"
|
|
|
|
|
- "/etc/systemd/system/etcd.service"
|
|
|
|
|
|
|
|
|
|
# 清理负载均衡相关
|
|
|
|
|
- name: stop keepalived service
|
|
|
|
|
shell: systemctl disable keepalived && systemctl stop keepalived
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: stop haproxy service
|
|
|
|
|
shell: systemctl disable haproxy && systemctl stop haproxy
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
|
- name: 清理LB 配置文件目录
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/etc/haproxy"
|
|
|
|
|
- "/etc/keepalived"
|
|
|
|
|
|
|
|
|
|
# 清理其他
|
|
|
|
|
- name: stop and disable chrony in Ubuntu
|
|
|
|
|
service: name=chrony state=stopped enabled=no
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
tags: rm_ntp
|
|
|
|
|
when: ansible_distribution == "Ubuntu" or ansible_distribution == "Debian"
|
|
|
|
|
|
|
|
|
|
- name: stop and disable chronyd in CentOS/RedHat
|
|
|
|
|
service: name=chronyd state=stopped enabled=no
|
|
|
|
|
ignore_errors: true
|
|
|
|
|
tags: rm_ntp
|
|
|
|
|
when: ansible_distribution == "CentOS" or ansible_distribution == "RedHat"
|
|
|
|
|
|
|
|
|
|
- name: 清理证书目录和文件
|
|
|
|
|
file: name={{ item }} state=absent
|
|
|
|
|
with_items:
|
|
|
|
|
- "/etc/kubernetes/"
|
|
|
|
|
- "{{ ca_dir }}"
|
|
|
|
|
- "/root/.kube/"
|
|
|
|
|
- "/etc/docker/"
|
|
|
|
|
|
|
|
|
|
- name: 清理自动生成的PATH
|
|
|
|
|
lineinfile:
|
|
|
|
|
dest: ~/.bashrc
|
|
|
|
|
state: absent
|
|
|
|
|
regexp: '{{ item }}'
|
|
|
|
|
with_items:
|
|
|
|
|
- 'kubeasz'
|
2019-05-19 08:51:01 +08:00
|
|
|
|
- 'helm completion'
|
|
|
|
|
- 'crictl completion'
|
2019-02-19 16:38:10 +08:00
|
|
|
|
- 'kubectl completion'
|
2019-05-19 08:51:01 +08:00
|
|
|
|
- 'HELM_TLS_ENABLE'
|
2019-02-19 16:38:10 +08:00
|
|
|
|
delegate_to: "{{ NODE_TO_DEL }}"
|
|
|
|
|
run_once: true
|
|
|
|
|
|
|
|
|
|
# 执行kubectl delete(节点可能是kube-node节点)
|
|
|
|
|
- name: 执行kubectl delete(节点可能是kube-node节点)
|
|
|
|
|
shell: "{{ bin_dir }}/kubectl delete node {{ NODE_TO_DEL }}"
|
2018-08-28 21:53:43 +08:00
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
2019-02-19 16:38:10 +08:00
|
|
|
|
# 删除 ansible hosts 中节点配置
|
|
|
|
|
- name: rm {{ NODE_TO_DEL }} in ansible hosts
|
2018-06-20 11:48:29 +08:00
|
|
|
|
lineinfile:
|
2019-02-19 16:38:10 +08:00
|
|
|
|
dest: "{{ base_dir }}/hosts"
|
2018-06-20 11:48:29 +08:00
|
|
|
|
state: absent
|
2019-05-06 22:10:58 +08:00
|
|
|
|
regexp: '^{{ NODE_TO_DEL }}$|^{{ NODE_TO_DEL }}[^0-9]'
|
2019-02-19 16:38:10 +08:00
|
|
|
|
connection: local
|