kubernetes删除ns异常状态为:Terminating

发布时间 2023-05-31 10:47:48作者: xiao智

在部署kuboard控制平台的时候,不规范删除,导致ns状态为Terminating

[root@master01 ~]# kubectl delete namespace kuboard 

^C
root@master01 ~]# kubectl get ns
NAME              STATUS        AGE
default           Active        25h
kube-flannel      Active        24h
kube-node-lease   Active        25h
kube-public       Active        25h
kube-system       Active        25h
kuboard           Terminating   21h
[root@master01 ~]# 

尝试1,还是不行

使用 -grace-period=0 --force 参数强行删除,还是不行

[root@master01 ~]# kubectl delete namespace kuboard  --grace-period=0 --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
namespace "kuboard" force deleted


^C
[root@master01 ~]# 
[root@master01 ~]# kubectl  get ns
NAME              STATUS        AGE
default           Active        39h
kube-flannel      Active        39h
kube-node-lease   Active        39h
kube-public       Active        39h
kube-system       Active        39h
kuboard           Terminating   36h

尝试2,调用kubernetes-API删除

感谢大哥的文章: http://www.manongjc.com/detail/40-xxgstfjidoveeot.html

1、首先,获取要删除 NameSpace 的 JSON 文件:

替换其中的 <terminating-namespace> 为实际 namespace
kubectl get namespace <terminating-namespace> -o json >tmp.json

[root@master01 ~]# kubectl get namespace kuboard -o json > tmp.json

2、编辑上一步导出的 json 文件,去除 spec.finalizers 配置

[root@master01 ~]# vim tmp.json
····
    "spec": {
        "finalizers": []
····

image

3、打开另一个ssh窗口运行kubectl proxy开启代理

[root@master01 ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001

4、删除 namespace

其中 tmp.json 为第一步中导出的 json 文件, 需替换为实际的 namespace
$ curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces//finalize

[root@master01 ~]# kubectl  get ns
NAME              STATUS        AGE
default           Active        39h
kube-flannel      Active        39h
kube-node-lease   Active        39h
kube-public       Active        39h
kube-system       Active        39h
kuboard           Terminating   36h
[root@master01 ~]# 
[root@master01 ~]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/kuboard/finalize


}[root@master01 ~]# kubectl  get ns
NAME              STATUS   AGE
default           Active   39h
kube-flannel      Active   39h
kube-node-lease   Active   39h
kube-public       Active   39h
kube-system       Active   39h
[root@master01 ~]#

image

完成对namespace状态Terminating的删除