K8S命名空间删除

TOC

删除命名空间命令

可以通过以下命令删除命名空间

kubectl delete ns [namespace]
#或者
kubectl delete ns [namespace] --force --grace-period=0

通过kubectl get ns查看命名空间资源

[root@K8S ~]# kubectl get ns
NAME                              STATUS        AGE
monitoring                        Terminating   30d

删除Terminating状态的命名空间

这种方式一般都能删除,但也有可能会出现清理不掉的情况,ns的状态一直为Terminating就可以使用下面的方法删除
1.先将要删除的namespace的描述信息导出到文件

kubectl get ns monitoring -o json > monitoring.json

2.删除该文件中spec和finalizers这两个字段包含的内容,并保存
删除之后的文件如下

cat monitoring.json
-------------------------------
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-07-31T02:55:55Z\"},{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2021-07-31T02:55:55Z\"}]}",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2021-07-27T09:19:29Z",
        "deletionGracePeriodSeconds": 0,
        "deletionTimestamp": "2022-08-31T03:13:18Z",
        "finalizers": [
        ],
        "labels": {
            "kubernetes.io/metadata.name": "monitoring",
            "kubesphere.io/namespace": "monitoring"
        },
        "name": "monitoring",
        "resourceVersion": "132110265",
        "uid": "b9d064ab-1169-48cd-a83e-1702746f07cb"
    },
    "spec": {},
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2022-08-31T03:13:23Z",
                "message": "All resources successfully discovered",
                "reason": "ResourcesDiscovered",
                "status": "False",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2022-08-31T03:13:23Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2022-08-31T03:13:23Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2022-08-31T03:13:29Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2022-08-31T03:13:29Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}

这里我们可以看到spec和finalizers这两个字段内容被删除了为空

"finalizers": [
        ],
"spec": {},

3.然后开通代理,使用curl命令调用接口

[root@K8S ~]# kubectl proxy --address='0.0.0.0' --port=8081
Starting to serve on [::]:8081

调用接口

curl -k -H "Content-Type: application/json" -X PUT --data-binary @monitoring.json http://127.0.0.1:8081/api/v1/namespaces/monitoring/finalize

这里的格式为

curl -k -H "Content-Type: application/json" -X PUT --data-binary @[导出命名空间资源信息的文件名] http://[接口地址]:[端口号]/api/v1/namespaces/[命名空间]/finalize