Kubernetes delete ns(namespace)

I. Introduction

When deleting an application or module, the state of the namespace may always be in the Terminating state. It is useless to restart k8s or restart all servers.

When k8s encounters an ns (namespace) namespace that cannot be deleted, you can try several deletion methods

2. Delete the namespace namespace

1. Check k8s first

[root@k8s-master1 ~]# kubectl get ns
NAME                          STATUS        AGE
cattle-impersonation-system   Terminating   2d14h
cattle-system                 Terminating   2d14h
default                       Active        13d
ingress-nginx                 Terminating   4d13h
kube-node-lease               Active        13d
kube-public                   Active        13d
kube-system                   Active        13d
monitoring                    Active        12d

2. You can try the following methods to delete the kubernetes namespace namespace step by step

kubectl delete ns <要删除的命名空间>
kubectl delete ns <要删除的命名空间> --froce
kubectl delete ns <要删除的命名空间> --force --grace-period=0

3. If the above 3 methods still cannot delete the kubernetes namespace, you can try to use the interface to delete

3. Delete the kubernetes namespace by using the interface

1. Obtain the json file of the namespace. Let’s take deleting the namespace ingress-nginx as an example.

kubectl get ns ingress-nginx -o json > delete-ns-ingress-nginx.json

If the source file is obtained:

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "cattle.io/status": "{\"Conditions\":[{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2023-06-28T22:44:35Z\"},{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2023-06-28T22:44:41Z\"}]}",
            "field.cattle.io/projectId": "c-m-x64sl2w6:p-4845x",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"labels\":{\"app.kubernetes.io/instance\":\"ingress-nginx\",\"app.kubernetes.io/name\":\"ingress-nginx\"},\"name\":\"ingress-nginx\"}}\n",
            "lifecycle.cattle.io/create.namespace-auth": "true",
            "management.cattle.io/no-default-sa-token": "true"
        },
        "creationTimestamp": "2023-06-26T15:26:03Z",
        "deletionGracePeriodSeconds": 0,
        "deletionTimestamp": "2023-06-29T15:06:06Z",
        "finalizers": [
            "controller.cattle.io/namespace-auth"            #删除这一行,并且保持finalizers数组为空,即[]
        ],
        "labels": {
            "app.kubernetes.io/instance": "ingress-nginx",
            "app.kubernetes.io/name": "ingress-nginx",
            "kubernetes.io/metadata.name": "ingress-nginx"
        },
        "name": "ingress-nginx",
        "resourceVersion": "147165",
        "uid": "7dc28b36-5960-4237-b1b5-41b8d6e20f7c"
    },
    "spec": {},
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2023-06-29T15:06:14Z",
                "message": "All resources successfully discovered",
                "reason": "ResourcesDiscovered",
                "status": "False",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2023-06-29T15:06:14Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2023-06-29T15:06:14Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2023-06-29T15:08:52Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2023-06-29T15:06:14Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}

2. Search for finalizers in the json file, and empty the array. If it is not deleted this time

 

 3. Open the proxy on the master node of k8s

kubectl proxy

 Note that the port is 8001

4. Open another k8s-master node terminal so that we can execute commands

curl -k -H "Content-Type: application/json" -X PUT --data-binary @delete-ns-ingress-nginx.json http://127.0.0.1:8001/api/v1/namespaces/ingress-nginx/finalize

Note: delete-ns-ingress-nginx.json is the json file you exported and modified just now, pay attention to the @ character in front of the command.

Replace ingress-nginx in http://127.0.0.1:8001/api/v1/namespaces/ingress-nginx/finalize with the namespace string you want to delete

After the command is executed, a bunch of strings are returned, so don't worry about it.

5. Check all Kubernetes namespaces again to see if the target ns has been deleted

 

It can be seen that the namespace ingress-nginx to be deleted this time has been completely killed.

------ok--------kahn------July 1, 2023 13:29:26-----------

Guess you like

Origin blog.csdn.net/xoofly/article/details/131492019