【运维杂谈】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年运维经验,持续分享运维干货,感谢大家的阅读和关注!

相关推荐
yyzc111114 小时前
当中小微实体店面临经营困境时如何借助顺成汇创旗下荟生意品牌实现数字化智慧营销
大数据·运维·人工智能
CoderYanger6 小时前
Java EE 进阶:14.案例综合练习-博客系统(第二弹):博客详情+令牌技术
服务器·前端·spring boot·后端·spring·java-ee·github
Rsingstarzengjx7 小时前
第三十九章 Linux PWM 驱动实验
linux·运维·服务器
SeanQhl8 小时前
BoostKit 环境准备
linux·openeuler·鲲鹏·boostkit·软件源
笔记本note9 小时前
Linux 内存管理
linux·服务器·内存管理
郝学胜-神的一滴10 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
GeW11 小时前
入行 Linux 与数据库运维:一条路径、三项习惯、一个考证观
linux·数据库
南子北游13 小时前
Linux 进程状态全景(上)
linux·运维·服务器
wdfk_prog13 小时前
Wi-Fi Direct 真机实践:JOIN 建链耗时优化
服务器·asp.net·php
yyzc1111114 小时前
AI聚合支付搭配数字门店系统让实体店告别粗放经营
大数据·运维·人工智能