k8s 删除Terminating状态的namespace

发布时间 2023-11-14 16:38:20作者: yuhaohao

查看ns状态

root@test-10-5-2-15:~# kubectl get ns
NAME              STATUS        AGE
cert-manager      Terminating   19h

查看该命名空间下的资源

kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n cert-manager

查看命名空间下并无资源占用

获取namespace的详情信息

root@test-10-5-2-15:~# kubectl get ns cert-manager -o json > ns.json

查看namespace定义的json配置,编辑json文件并删除掉spec部分:

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"cert-manager\"}}\n"
        },
        "creationTimestamp": "2023-11-13T12:55:45Z",
        "deletionTimestamp": "2023-11-14T08:18:44Z",
        "labels": {
            "kubernetes.io/metadata.name": "cert-manager"
        },
        "name": "cert-manager",
        "resourceVersion": "8393161",
        "uid": "ec907f95-068f-4f82-8adb-401e4fbbef39"
    },
  // 删除下面的部分
 //   "spec": {
 //       "finalizers": [
 //           "kubernetes"
 //       ]
 //   },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2023-11-14T08:18:50Z",
                "message": "All resources successfully discovered",
                "reason": "ResourcesDiscovered",
                "status": "False",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2023-11-14T08:18:50Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2023-11-14T08:18:50Z",
                "message": "Failed to delete all resource types, 1 remaining: Internal error occurred: error resolving resource",
                "reason": "ContentDeletionFailed",
                "status": "True",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2023-11-14T08:18:50Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2023-11-14T08:18:50Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}

启动proxy接口

root@test-10-5-2-15:~# kubectl proxy
Starting to serve on 127.0.0.1:8001

调用k8s原生接口删除

# 注意配置finalize前面的namespace
root@test-10-5-2-15:~# curl -k -H "Content-Type: application/json" -X PUT --data-binary @ns.json http://127.0.0.1:8001/api/v1/namespaces/cert-manager/finalize