k8s删除Terminating状态的命名空间

kubernetes删除Terminating状态的命名空间

1.描述

使用kubectl delete -f xxx.yaml,再次执行 kubectl apply -f xxx.yaml,提示:

Error from server (Forbidden): error when creating "kubesphere-complete-setup.yaml": configmaps "ks-installer" is forbidden: unable to create new content in namespace kubesphere-system because it is being terminated
Error from server (Forbidden): error when creating "kubesphere-complete-setup.yaml": serviceaccounts "ks-installer" is forbidden: unable to create new content in namespace kubesphere-system because it is being terminated
Error from server (Forbidden): error when creating "kubesphere-complete-setup.yaml": deployments.apps "ks-installer" is forbidden: unable to create new content in namespace kubesphere-system because it is being terminated

查看命名空间

kubectl  get ns

# NAME                STATUS        AGE
# default             Active        15h
# kube-node-lease     Active        15h
# kube-public         Active        15h
# kube-system         Active        15h
# kubesphere-system   Terminating   28m

发现kubesphere-system一直处于Terminating 状态。无法删除命名空间!!

2.解决办法

查看kubesphere-system的namespace描述

kubectl get ns kubesphere-system  -o json > kubesphere-system.json
# 编辑json文件,删除spec字段的内存,因为k8s集群时需要认证的。
vi kubesphere-system.json
将
"spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
改成
"spec": {


  },

或者

for NS in $(kubectl get ns 2>/dev/null | grep Terminating | cut -f1 -d ' '); do
  kubectl get ns $NS -o json > /tmp/$NS.json
  sed -i '' "s/\"kubernetes\"//g" /tmp/$NS.json
  kubectl replace --raw "/api/v1/namespaces/$NS/finalize" -f /tmp/$NS.json
done

新开一个窗口运行kubectl proxy跑一个API代理在本地的8081端口

kubectl proxy --port=8081
# Starting to serve on 127.0.0.1:8081

最后运行curl命令进行删除

curl -k -H "Content-Type:application/json" -X PUT --data-binary @kubesphere-system.json http://127.0.0.1:8081/api/v1/namespaces/kubesphere-system/finalize

注意:命令中的kubesphere-system就是命名空间。

再次查看命名空间

kubectl get ns

# NAME              STATUS   AGE
# default           Active   15h
# kube-node-lease   Active   15h
# kube-public       Active   15h
# kube-system       Active   15h

发现kubesphere-system命名空间已经消失了

留下评论