目录

Kubeadm系列-05-reset

概述

查看一下 reset 的 help 信息。

1
2
3
4
5
6
7
8
9
# kubeadm reset --help
Performs a best effort revert of changes made to this host by 'kubeadm init' or 'kubeadm join'

The "reset" command executes the following phases:

preflight              Run reset pre-flight checks
update-cluster-status  Remove this node from the ClusterStatus object.
remove-etcd-member     Remove a local etcd member.
cleanup-node           Run cleanup node.

会清理哪些目录的数据

其实在反复安装卸载主机的 k8s 相关的文件的时候,我是比较关系,到底能够清理到什么程度的,根据源码可以深入了解一下。

1
2
3
func (r *resetData) AddDirsToClean(dirs ...string) {
	r.dirsToClean = append(r.dirsToClean, dirs...)
}

看看哪些地方调用了这个函数。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    // 首先是kubelet的目录,默认就是/var/lib/kubelet
	kubeletRunDir, err := absoluteKubeletRunDirectory()
	if err == nil {
		// Only clean absoluteKubeletRunDirectory if umountDirsCmd passed without error
		r.AddDirsToClean(kubeletRunDir)
	}

	// 然后是下面这些目录,如果没有用docker的话,dockershim就可以忽略
	r.AddDirsToClean("/var/lib/dockershim", "/var/run/kubernetes", "/var/lib/cni")

	// 最后是etcd的数据
	etcdDataDir, err := getEtcdDataDir(etcdManifestPath, cfg)
	if err == nil {
		r.AddDirsToClean(etcdDataDir)
		...
	} else {
       ...
	}

删的干净吗

从应用的角度来看,数据文件该清理都清理了,假设在不重装机器的前提下,想还给运维一个相对干净的环境,如果我希望把 k8s/cri/runc/cni 这些软件也收拾干净,包括 iptables/ipvs,创建出来的网络接口、网桥,那么单纯用 kubeadm reset 就删不干净了。

浅尝一下

如果想知道 kubeadm reset 有什么作用,或者想知道具体帮我们 reset 了什么,不妨执行这样的命令 kubeadm reset --dry-run,然后具体阅读一下输出。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[reset] Reading configuration from the cluster...
[reset] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
W0621 03:38:00.141515  151679 preflight.go:55] [reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
[reset] Are you sure you want to proceed? [y/N]: y
[preflight] Running pre-flight checks
[reset] Would remove the etcd member on this node from the etcd cluster
[reset] Would stop the kubelet service
[reset] Would unmount mounted directories in "/var/lib/kubelet"
[reset] Would remove Kubernetes-managed containers
[reset] Would delete contents of directories: [/etc/kubernetes/manifests /etc/kubernetes/pki]
[reset] Would delete files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf]
[reset] Would delete contents of stateful directories: [/var/lib/etcd /var/lib/dockershim /var/run/kubernetes /var/lib/cni]

The reset process does not clean CNI configuration. To do so, you must remove /etc/cni/net.d

The reset process does not reset or clean up iptables rules or IPVS tables.
If you wish to reset iptables, you must do so manually by using the "iptables" command.

If your cluster was setup to utilize IPVS, run ipvsadm --clear (or similar)
to reset your system's IPVS tables.

The reset process does not clean your kubeconfig files and you must remove them manually.
Please, check the contents of the $HOME/.kube/config file.

从上面的结果,可以知道,像 iptables/ipvs 的规则以及 kubeconfig 是不会自动帮忙清理的,iptables/ipvs 这些可以理解,因为规则太复杂了,有时候还包括了用户或者运维自定义的一些规则,如果贸然由 kubeadm reset 给清理掉,可能会引发一些不好排查的网络问题

从上面的输出结果也可以看到,kubeadm reset 不会清理掉 kubeadm/kubelet 这些软件包,只会删除他们产生的一些数据和文件,基本上可以说是可以清理干净的

kubeadm reset afterhook

这里的清理逻辑很难统一,但是大家可以按照自己的想法来配置一下脚本。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
kubeadm reset
ipvsadm --clear
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
rm -rf /var/lib/cni/*
rm -rf /var/lib/kubelet/*
rm -rf /etc/cni/*
rm -rf $HOME/.kube

ifconfig cni0 down
ifconfig flannel.1 down
ifconfig kube-ipvs0 down
ifconfig vxlan.calico down
ifconfig docker0 down
ifconfig dummy0 down
ifconfig tunl0 down
ifconfig eth1 down

ip link delete cni0
ip link delete flannel.1
ip link delete kube-ipvs0
ip link delete vxlan.calico
ip link delete docker0
ip link delete dummy0
ip link delete tunl0

清理docker

虽然个人不推荐继续在 k8s 内使用 docker,但是应该还有很多集群有用 docker 的,所以也讲一下清理的逻辑。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# ubuntu
docker system prune -a
systemctl stop docker
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo yum remove -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

# centos
docker system prune -a
systemctl stop docker
yum autoremove -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

删除遇到问题

如果遇到删除目录卡住的话,可以尝试下面的这些方法。

1
2
3
4
5
6
# rm -rf /var/lib/docker
rm: cannot remove /var/lib/docker: Device or resource busy

# 解决方法
cat /proc/mounts | grep "docker"
umount /var/lib/docker
警告
本文最后更新于 2022年3月20日,文中内容可能已过时,请谨慎参考。