#kubernetes 如何移除一个k8s集群

k8s集群安装失败了,准备重新安装,在这之前需要做一些清理工作:

sudo kubeadm reset

然后重新初始化即可。

注意
Control Plane重置以后,所有的worker节点也要用kubeadm reset来重置。

## 1. 重置配置

kubectl config unset clusters
kubectl config unset contexts
kubectl config unset users

## 2. 停止并移除相关docker容器

docker container prune

## 3. 移除配置文件

sudo mv /etc/kubernetes /etc/kubernetes.old

否则会报错

[init] Using Kubernetes version: v1.21.0
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
    [ERROR FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
    [ERROR FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
    [ERROR FileAvailable--etc-kubernetes-manifests-etcd.yaml]: /etc/kubernetes/manifests/etcd.yaml already exists
    [ERROR Swap]: running with swap on is not supported. Please disable swap
    [ERROR DirAvailable--var-lib-etcd]: /var/lib/etcd is not empty
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher

## 4. 停止kubelet服务

sudo systemctl stop kubelet

否则会提示端口(10250)被占用

[init] Using Kubernetes version: v1.21.0
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR Port-10250]: Port 10250 is in use

## 5. 移除etcd配置

sudo mv /var/lib/etcd /var/lib/etcd.old

否则提示etcd目录已经存在

error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR DirAvailable--var-lib-etcd]: /var/lib/etcd is not empty

## 6. 移除kubelet

sudo apt-get purge kubelet

记得重新安装

# 测试

sudo kubeadm init --dry-run
```~~