[SOLVED]: Kubernetes Namespace Stuck in Terminating

A Kubernetes namespace is a virtual cluster that allows multiple users to share the same physical cluster. It provides an additional layer of abstraction that isolates resources, such as pods, services, and deployments, and enables secure multi-tenancy within a single Kubernetes cluster.

However, sometimes when you try to delete a namespace, it might get stuck in the terminating state. This can occur due to various reasons, such as an unresponsive resource or a pending finalizer. When this happens, you might find it challenging to delete or recreate the namespace, which can be frustrating, especially if you need to use the resources within the namespace.

In this article, we will explore how to remove a stuck namespace in Kubernetes and get back to managing your resources as quickly as possible.

Step 1: Identify the Namespace that is Stuck in Terminating

The first step to removing a stuck namespace is to identify the namespace that is stuck in the terminating state. To do this, you can run the following command:

kubectl get namespace|grep -i termi

Step 2: Dump the contents of the namespace

kubectl get namespace ${NAMESPACE} -o json > tmp.json

Step 3: Remove kubernetes from the finalizer array

Use any text editor to remove kubernetes from the finalizer array, and save the file

 "spec": { 
"finalizers": [ 
"kubernetes"  ##Remove this
 ] 
},

Step4: Set a temporary proxy in the command line.

Set a temporary proxy in the command line using below command

#kubectl proxy
Starting to serve on 127.0.0.1:8001

Step4: Execute finalize command in a new terminal

Open a new terminal and execute the below curl command to finalize. Provide the name of the namespace  in the command

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

Step5: Verify  Kubernetes Namespace Stuck in Terminating is terminated

kubectl get namespaces

Single line command to delete the Namespace Stuck in Terminating

Execute below command to delete the namespace stuck in terminating. Replace "ns" with namespace

NAMESPACE=<ns>; kubectl proxy & kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json; curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize

Reference

https://stackoverflow.com/questions/52369247/namespace-stuck-as-terminating-how-i-removed-it

Search on LinuxDataHub

Leave a Comment