Namespace “stuck” as Terminating

1/11/2021

I am getting issue while terminating the namesapce in the cluster, It's showing many parameters inside the namespace JSON. I followed this link https://medium.com/@craignewtondev/how-to-fix-kubernetes-namespace-deleting-stuck-in-terminating-state-5ed75792647e

 "spec": {},
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2021-01-11T08:41:48Z",
                "message": "All resources successfully discovered",
                "reason": "ResourcesDiscovered",
                "status": "False",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2021-01-11T08:41:48Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2021-01-11T08:41:48Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2021-01-11T08:42:09Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2021-01-11T08:41:48Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}```
-- rajendra sharma
amazon-eks
kubernetes
namespaces
rancher

3 Answers

1/11/2022

Firstly export your namespace name in env which got struck in Terminating state

export NAMESPACE="monitoring"

Then run below command to delete the Terminating namespace

kubectl get namespace $NAMESPACE -o json   | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/"   | kubectl replace --raw /api/v1/namespaces/$NAMESPACE/finalize -f -
-- Pratik Raj
Source: StackOverflow

5/26/2021

I have found the answer to terminate the stuck namespace.

for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}')
do
  kubectl get ns $ns -ojson | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f -
done

for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}')
do
  kubectl get ns $ns -ojson | jq '.metadata.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f -
done
-- rajendra sharma
Source: StackOverflow

1/11/2021

The tutorial you've used is not proper because deleting the namespace by removing finalizers is not good way to go since it could leave resources registered to a non existing namespace. Please take a look at this post: finalizer-kubernetes-ns.

You can try to find out which resources in the namespace are pending deletion by:

  • Finding all resources that still exist using command kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -n $yotur-ns-to-delete
  • Checking if any apiservice is unavailable and hence doesn't serve its resources by executing command kubectl get apiservice|grep False

Take a look also at this problem: ns-kubernetes-stuck-terminating.

-- Malgorzata
Source: StackOverflow