强制删除命名空间(K8S 强制删除卡在Terminating状态的namespaces 记录)

参考:https://blog.csdn.net/lxy___/article/details/106644302

调用API接口删除

bash 复制代码
[root@master ~]# netstat -ntlp | grep kube-apiserve
tcp6       0      0 :::6443                 :::*                    LISTEN      7737/kube-apiserver 
#API接口查询 
[root@master ~]# kubectl get ns monitoring -ojson > tmp.json
#导出信息

修改如下内容

javascript 复制代码
[root@master ~]# cat tmp.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
        },
        "creationTimestamp": "2020-04-24T01:39:40Z",
        "deletionTimestamp": "2020-06-09T07:22:46Z",
        "name": "monitoring",
        "resourceVersion": "30261",
        "selfLink": "/api/v1/namespaces/monitoring",
        "uid": "3fdd71e6-9693-4d44-bd8a-f1e54799cde4"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}

删除以下代码段

javascript 复制代码
"spec": {
    "finalizers": [
        "kubernetes"
    ]
},

权限问题

javascript 复制代码
修改完成后,直接调用API接口会有报错如下


curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json https://127.0.0.1:6443/api/v1/namespaces/monitoring(替换成自己要删除的ns)/finalize
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "namespaces \"monitoring\" is forbidden: User \"system:anonymous\" cannot update resource \"namespaces/finalize\" in API group \"\" in the namespace \"monitoring\"",
  "reason": "Forbidden",
  "details": {
    "name": "monitoring",
    "kind": "namespaces"
  },
  "code": 403

此时需要创建匿名用户的权限,命令如下

bash 复制代码
kubectl create clusterrolebinding test:anonymous --clusterrole=cluster-admin --user=system:anonymous

再次调用

javascript 复制代码
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json https://127.0.0.1:6443/api/v1/namespaces/monitoring(替换成自己要删除的ns)/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "monitoring",
    "selfLink": "/api/v1/namespaces/monitoring/finalize",
    "uid": "3fdd71e6-9693-4d44-bd8a-f1e54799cde4",
    "resourceVersion": "30261",
    "creationTimestamp": "2020-04-24T01:39:40Z",
    "deletionTimestamp": "2020-06-09T07:22:46Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
    }
  },
  "spec": {
    
  },
  "status": {
    "phase": "Terminating"
  }

删除成功

bash 复制代码
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   46d
kube-node-lease   Active   46d
kube-public       Active   46d
kube-system       Active   46d
相关推荐
tonngw3 小时前
【Mac 从 0 到 1 保姆级配置教程 16】- Docker 快速安装配置、常用命令以及实际项目演示
macos·docker·容器·开源·github·docker desktop·orbstack
Chan165 小时前
【 SpringCloud | 微服务 MQ基础 】
java·spring·spring cloud·微服务·云原生·rabbitmq
2201_761199047 小时前
k8s4部署
云原生·容器·kubernetes
慌ZHANG7 小时前
云原生技术驱动 IT 架构现代化转型:企业实践与落地策略全解
云原生
小柏ぁ7 小时前
calico/node is not ready: BIRD is not ready: BGP not established with xxx
运维·docker·kubernetes
三劫散仙8 小时前
kubernetes jenkins pipeline优化拉取大仓库性能指定分支+深度
容器·kubernetes·jenkins
西京刀客8 小时前
k8s热更新-subPath 不支持热更新
云原生·容器·kubernetes·configmap·subpath
weixin_434936289 小时前
k8S 命令
linux·容器·kubernetes
nuczzz11 小时前
GPU虚拟化
docker·kubernetes·k8s·gpu·nvidia
Johny_Zhao12 小时前
2025年6月Docker镜像加速失效终极解决方案
linux·网络·网络安全·docker·信息安全·kubernetes·云计算·containerd·yum源·系统运维