k8s删除Terminating状态的命名空间
一、概述
最近部署kubesphere时,使用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 状态。无法删除命名空间!!
二、解决方法
查看kubesphere-system的namespace描述
kubectl get ns kubesphere-system -o json > kubesphere-system.json
编辑json文件,删除spec字段的内存,因为k8s集群时需要认证的。
vi kubesphere-system.json
将
"spec": { "finalizers": [ "kubernetes" ] },
更改为:
"spec": { },
新开一个窗口运行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就是命名空间。
输出:

{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "kubesphere-system", "selfLink": "/api/v1/namespaces/kubesphere-system/finalize", "uid": "ba8b8bcd-adf0-4f4f-b6bf-ebab51c00252", "resourceVersion": "72676", "creationTimestamp": "2020-07-09T02:04:37Z", "deletionTimestamp": "2020-07-09T02:09:41Z", "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"kubesphere-system\"}}\n" } }, "spec": { }, "status": { "phase": "Terminating", "conditions": [ { "type": "NamespaceDeletionDiscoveryFailure", "status": "True", "lastTransitionTime": "2020-07-09T02:09:46Z", "reason": "DiscoveryFailed", "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request" }, { "type": "NamespaceDeletionGroupVersionParsingFailure", "status": "False", "lastTransitionTime": "2020-07-09T02:09:47Z", "reason": "ParsedGroupVersions", "message": "All legacy kube types successfully parsed" }, { "type": "NamespaceDeletionContentFailure", "status": "False", "lastTransitionTime": "2020-07-09T02:09:47Z", "reason": "ContentDeleted", "message": "All content successfully deleted" } ] } }
再次查看命名空间
# kubectl get ns NAME STATUS AGE default Active 15h kube-node-lease Active 15h kube-public Active 15h kube-system Active 15h
发现kubesphere-system命名空间已经消失了。
最后再次执行 kubectl apply -f xxx.yaml,就正常了。
本文参考链接:
分类:
python 运维开发
标签:
Kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2018-07-09 python 全栈开发,Day79(Django的用户认证组件,分页器)