7 - 清理节点


将节点添加到集群时后,会创建容器、虚拟网络接口等资源和证书、配置文件。从集群中正常删除节点时(如果处于Active状态),将自动清除这些资源,并且只需重新启动节点即可。当节点无法访问且无法使用自动清理,或者异常导致节点脱离集群后,如果需要再次将节点加入集群,那么需要手动进行节点初始化操作。

手动清理节点

警告: 以下操作将删除节点中的数据,在执行命令之前,请确保已进行数据备份。

  1. # 停止服务
  2. systemctl disable kubelet.service
  3. systemctl disable kube-scheduler.service
  4. systemctl disable kube-proxy.service
  5. systemctl disable kube-controller-manager.service
  6. systemctl disable kube-apiserver.service
  7. systemctl stop kubelet.service
  8. systemctl stop kube-scheduler.service
  9. systemctl stop kube-proxy.service
  10. systemctl stop kube-controller-manager.service
  11. systemctl stop kube-apiserver.service
  12. # 删除所有容器
  13. docker rm -f $(docker ps -qa)
  14. # 删除所有容器卷
  15. docker volume rm $(docker volume ls -q)
  16. # 卸载mount目录
  17. for mount in $(mount | grep tmpfs | grep '/var/lib/kubelet' | awk '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done
  18. # 备份目录
  19. mv /etc/kubernetes /etc/kubernetes-bak-$(date +"%Y%m%d%H%M")
  20. mv /var/lib/etcd /var/lib/etcd-bak-$(date +"%Y%m%d%H%M")
  21. mv /var/lib/rancher /var/lib/rancher-bak-$(date +"%Y%m%d%H%M")
  22. mv /opt/rke /opt/rke-bak-$(date +"%Y%m%d%H%M")
  23. # 删除残留路径
  24. rm -rf /etc/ceph \
  25. /etc/cni \
  26. /opt/cni \
  27. /run/secrets/kubernetes.io \
  28. /run/calico \
  29. /run/flannel \
  30. /var/lib/calico \
  31. /var/lib/cni \
  32. /var/lib/kubelet \
  33. /var/log/containers \
  34. /var/log/pods \
  35. /var/run/calico
  36. # 清理网络接口
  37. network_interface=`ls /sys/class/net`
  38. for net_inter in $network_interface;
  39. do
  40. if ! echo $net_inter | grep -qiE 'lo|docker0|eth*|ens*';then
  41. ip link delete $net_inter
  42. fi
  43. done
  44. # 清理残留进程
  45. port_list=`80 443 6443 2376 2379 2380 8472 9099 10250 10254`
  46. for port in $port_list
  47. do
  48. pid=`netstat -atlnup|grep $port |awk '{print $7}'|awk -F '/' '{print $1}'|grep -v -|sort -rnk2|uniq`
  49. if [[ -n $pid ]];then
  50. kill -9 $pid
  51. fi
  52. done
  53. pro_pid=`ps -ef |grep -v grep |grep kube|awk '{print $2}'`
  54. if [[ -n $pro_pid ]];then
  55. kill -9 $pro_pid
  56. fi
  57. # 清理Iptables表
  58. ## 注意:如果节点Iptables有特殊配置,以下命令请谨慎操作
  59. sudo iptables --flush
  60. sudo iptables --flush --table nat
  61. sudo iptables --flush --table filter
  62. sudo iptables --table nat --delete-chain
  63. sudo iptables --table filter --delete-chain
  64. systemctl restart docker