强制删除命名空间(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
相关推荐
l1t6 小时前
用docker安装测试crate数据库
数据库·docker·容器·cratedb
平行云PVT6 小时前
数字孪生信创云渲染技术解析:从混合信创到全国产化架构
linux·unity·云原生·ue5·图形渲染·webgl·gpu算力
heimeiyingwang7 小时前
【架构实战】云原生架构设计原则
云原生·架构
枕书7 小时前
实战记录:如何使用 Docker 一键部署长亭 PandaWiki 智能知识库
运维·docker·容器
Cyber4K9 小时前
【妙招系列】Harbor 镜像私有仓库搭建手册
linux·云原生·容器
王码码203510 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
IT199511 小时前
Docker笔记-对docker-compose.yml基本认识
笔记·docker·容器
阿里云云原生11 小时前
同学,你好!阿里云云原生 2027 实习生招聘启动
云原生
阿里云云原生12 小时前
一行命令,给你的 OpenClaw 龙虾装上 X 光机——阿里云可观测,让养虾更经济更安全
云原生
阿里云云原生14 小时前
SLS 智能问答助手:秒解游戏运营客服难题
云原生