kubernetes 删除 namespace 但是一直处于 Terminating 状态
# 查看namespace
- 这里可以看到之前部署 kubesphere 生成的 namespace 没删掉
[root@master ]# kubectl get ns
NAME STATUS AGE
cattle-logging Active 2d20h
cattle-prometheus Active 2d19h
cattle-system Active 2d20h
default Active 11d
fleet-system Active 2d20h
kspark Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubeapps Active 11d
kubesphere-controls-system Terminating 4d23h
kubesphere-monitoring-system Terminating 4d23h
monitoring Active 4d3h
# 解决方法一
- 首先导出json格式到xxx.json
[root@master ]# kubectl get namespace kubesphere-controls-system -o json > xxx.json
[root@master ]# kubectl get namespace kubesphere-monitoring-system -o json > yyy.json
[root@master ]# ll
-rw-r--r-- 1 root root 4751 2月 7 14:33 xxx.json
-rw-r--r-- 1 root root 4693 2月 7 14:36 yyy.json
[root@master ]#
- 编辑xxx.josn,yyy.josn删除finalizers 字段的值
"finalizers": [
"finalizers.kubesphere.io/namespaces"
],
#改为
"finalizers": [ ],
- 开启 proxy
[root@master ]# kubectl proxy
Starting to serve on 127.0.0.1:8001
- 新开窗口执行
[root@master ]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @xxx.json http://127.0.0.1:8001/api/v1/namespaces/kubesphere-controls-system/finalize
[root@master ]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @yyy.json http://127.0.0.1:8001/api/v1/namespaces/kubesphere-monitoring-system/finalize
[root@master ]#kubectl get ns
NAME STATUS AGE
cattle-logging Active 2d20h
cattle-prometheus Active 2d20h
cattle-system Active 2d21h
default Active 11d
fleet-system Active 2d20h
kspark Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubeapps Active 11d
monitoring Active 4d4h
原理其实就是将现有状态导出为json文件,再执行覆盖;
# 解决方法二
- 直接 edit 该资源,将 finalizers 字段的值删除(vim dd)
[root@master ]# kubectl get ns
NAME STATUS AGE
cattle-logging Active 2d21h
cattle-prometheus Active 2d20h
cattle-system Active 2d21h
default Active 11d
fleet-system Active 2d21h
kspark Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubeapps Active 11d
monitoring Active 4d4h
kharbor Terminating 5d5h
[root@master ]# kubectl edit ns kharbor
namespace/kharbor edited
[root@master ]# kubectl get ns
NAME STATUS AGE
cattle-logging Active 2d21h
cattle-prometheus Active 2d20h
cattle-system Active 2d21h
default Active 11d
fleet-system Active 2d21h
kspark Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubeapps Active 11d
monitoring Active 4d4h
[root@master ]#
总结:每当删除 namespace 或 pod 等一些 Kubernetes 资源时,有时资源状态会卡在 Terminating,很长时间无法删除,甚至有时增加 --force flag 之后还是无法正常删除。这时就需要 edit 该资源,将 finalizers 字段设置为 [],之后 Kubernetes 资源就正常删除了。