How to delete Kubernetes namespace stuck in terminating state


How to delete Kubernetes namespace stuck in terminating state:

Port forwarding to k8s:

ssh -oTCPKeepAlive=no -oServerAliveInterval=20 -J<name of vm>:2222 -NL 4443:<IP Address of k8s>:4443 <user_id>@bastion.local

Open another terminal and run below command:

kubectl api-versions
kubectl get namespaces


NAME              STATUS AGE
admin             Active 213d
buglabs           Terminating 6d20h
default           Active 265d
kube-node-lease   Active 265d
kube-public       Active 265d
kube-system       Active 265d
tslogs            Terminating 186d

kubectl get namespace <namespace> -o json > <namespace>.json
kubectl get namespace buglabs -o json > buglabs.json

Edit buglabs.json file and remove “kubernetes” from finalizers section:

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "creationTimestamp": "2020-01-27T09:59:04Z",
        "deletionTimestamp": "2020-01-27T10:04:30Z",
        "name": "buglabs",
        "resourceVersion": "<some number>",
        "selfLink": "/api/v1/namespaces/buglabs",
        "uid": "<some_id>"
    },
    "spec": {
        "finalizers": [
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}
~   



Execute below command:


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

For deleting each namespace, please repeat the above steps by editing relating name.

Below are the namespaces deleted from k8s

  1. buglabs

Run below commands to see if namespaces are deleted:

kubectl get namespaces

NAME              STATUS AGE
admin             Active 213d
default           Active 265d
kube-node-lease   Active 265d
kube-public       Active 265d
kube-system       Active 265d

And again created using kubectl create namespace <namespace_to_be_created>

<End of Doc>

Comments