【运维杂谈】K8s遇到Terminating的名称空间删除不了怎么办?(解决方案亲测有效)

本篇几年前写的,非AI文,怀念那个没有AI自己下功夫研究的时代。

本篇文章给大家介绍名称空间删除不了怎么办,解决方案亲测有效!!!

如果你能看到这篇文章,想必你已经无数次尝试 kubectl delete ns xxx 了,不管是强制删除的 kubectl delete ns xxx --force 还是 kubectl delete ns xxx --force --grace-period=0,删除完查看名称空间还是没有删除,状态显示STATUS,详细信息根本看不懂!!!

此时请你跟着我做如下步骤,不要嫌麻烦,操作完后,你会有所收获的!

1、首先,我们查看名称空间,发现有几个Terminating的名称空间,我们以删除 koten-efk01 为例。

复制代码
[root@Master231 ~]# kubectl get ns
NAME              STATUS        AGE
default           Active        13d
koten-efk01       Terminating   3h52m
koten-efk02       Terminating   3h37m
koten-efk03       Active        146m
koten-efk04       Terminating   137m
kube-flannel      Active        13d
kube-node-lease   Active        13d
kube-public       Active        13d
kube-system       Active        13d

2、将要删除的名称空间的资源清单以json格式导出

复制代码
[root@Master231 ~]# kubectl get namespaces koten-efk01 -o json > koten-efk01.json

3、修改导出的json文件,删除spec下面的所有字段,但是spec本身不要删除。

删除前

删除后

4、打开一个新的shell会话,执行 kubectl proxy,启用代理

复制代码
[root@Master231 ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001

5、在原shell会话调用Api删除,注意修改下面命令的json文件名称和要删除的名称空间

复制代码
[root@Master231 ~]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @koten-efk01.json http://127.0.0.1:8001/api/v1/namespaces/koten-efk01/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "koten-efk01",
    "selfLink": "/api/v1/namespaces/koten-efk01/finalize",
    "uid": "a4b6bd8c-d1f3-4b00-a0d5-41a29fa39b90",
    "resourceVersion": "950079",
    "creationTimestamp": "2023-06-27T08:19:13Z",
    "deletionTimestamp": "2023-06-27T08:27:46Z",
    "labels": {
      "kubernetes.io/metadata.name": "koten-efk01"
    },
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"koten-efk01\"}}\n"
    },
    "managedFields": [
      {
        "manager": "kubectl-client-side-apply",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2023-06-27T08:19:13Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:annotations": {
              ".": {},
              "f:kubectl.kubernetes.io/last-applied-configuration": {}
            },
            "f:labels": {
              ".": {},
              "f:kubernetes.io/metadata.name": {}
            }
          }
        }
      },
      {
        "manager": "kube-controller-manager",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2023-06-27T08:27:52Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:status": {
            "f:conditions": {
              ".": {},
              "k:{\"type\":\"NamespaceContentRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionContentFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceFinalizersRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              }
            }
          }
        },
        "subresource": "status"
      }
    ]
  },
  "spec": {},
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2023-06-27T08:27:52Z",
        "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": "2023-06-27T08:27:52Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2023-06-27T08:27:52Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted, may be waiting on finalization"
      },
      {
        "type": "NamespaceContentRemaining",
        "status": "False",
        "lastTransitionTime": "2023-06-27T08:27:52Z",
        "reason": "ContentRemoved",
        "message": "All content successfully removed"
      },
      {
        "type": "NamespaceFinalizersRemaining",
        "status": "False",
        "lastTransitionTime": "2023-06-27T08:27:52Z",
        "reason": "ContentHasNoFinalizers",
        "message": "All content-preserving finalizers finished"
      }
    ]
  }
}[root@Master231 ~]# 

6、再次查看发现之前的名称空间成功被删除

复制代码
[root@Master231 ~]# kubectl get ns
NAME              STATUS        AGE
default           Active        13d
koten-efk02       Terminating   3h54m
koten-efk03       Active        163m
koten-efk04       Terminating   154m
kube-flannel      Active        13d
kube-node-lease   Active        13d
kube-public       Active        13d
kube-system       Active        13d

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

相关推荐
RuoyiOffice7 小时前
KubeSphere 容器云实践:从管理 K8s 到建设企业 PaaS,还需要哪些能力
spring boot·kubernetes·kubesphere·jenkins·paas·微服务部署·ruoyi office
风吹稻子8 小时前
etcd备份与恢复:删了 etcd.yaml,容器为什么还在跑?——一次 K8s 控制面死锁排查实录
kubernetes
2601_967987378 小时前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
feng_you_ying_li8 小时前
linux之数据链路层
linux·网络·智能路由器
修己xj8 小时前
服务器无法解析域名?一次 DNS 配置缺失的排查与修复
运维
GeW9 小时前
高端化走到深处,是数据库和Linux的事
linux
程序员初一10 小时前
Linux 远程桌面有四种做法
linux·ssh
爱吃奥利奥_wen11 小时前
SQL Server 基础语法详解:从建库建表到约束与增删改查
服务器·数据库·oracle
程序开发阿元11 小时前
1C1G 能跑什么:把留常驻服务和不留摆进一张内存表
linux·ssh