Deleting a kubernetes namespace can be done easily with
kubectl delete command. However, sometime you would see the deleting process stuck at “Terminating” status and never finish. There is an open issue on Kubernetes Github repository for this issue. It happened to my Kubernetes cluster v1.13 as well.
For example following command shows the
monitoring namespace is being Terminating.
$ kubectl get namespaces
To troubleshoot this issue, let’s take a look at the namespace’s information.
kubectl get namespace monitoring -o json
You should see a similar output
finalizers array has one value which is
kubernetes. This is the reason that causes the terminating process keep running forever.
The Finalizer are arbitrary string values, that when present ensure that a hard delete of a resource is not possible while they exist. Kubernetes only finally deletes the object if the list of finalizers is empty, meaning all finalizers have been executed.
This issue can be solved quickly by removing the values of
finalizers array. However, to be safe, we should double check the list of our namespace’s related resources. Following command shows the resources belong to
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n monitoring
If you see any resource from above command output, try to delete it with
kubectl delete command. If not, let’s try to modify the Finalizer then. You will not be able to remove it by using
kubectl edit. To delete it, we have to update the its JSON data via API.
First of all, let’s expose the API using kube-proxy
kubectl proxy &
curl command to push the modified version of above JSON data to the Kubernetes API. Make sure you emptied the
finalizers array and save it into a file,
data.json for example.
curl -s -k -H "Content-Type: application/json" -X PUT -o /dev/null --data-binary @data.json http://localhost:8001/api/v1/namespaces/monitoring/finalize
Now let’s check the namespace list again, you would see the
monitoring namespace has been removed successfully.
kubectl get namespaces
If you wish to automate the deleting process, check out following script from ctron