Software Engineering

[Solved] Kubernetes Namespace caught in Terminating state

[Solved] Kubernetes Namespace caught in Terminating state
Written by admin


In case you have tried to delete a Kubernetes namespace, and it has been hanging in ‘deleting’ for hours on finish, it’s doubtless that you’ve got dangling assets that haven’t deleted but.

$ kubectl get ns
NAME              STATUS        AGE
apps              Energetic        2d19h
default           Energetic        3d8h
my-apps           Terminating   11h

It will forestall the namespace from being eliminated.

discover assets that should be deleted

You possibly can chain the api-resources verbs together with a kubectl get to seek out dangling assets:

kubectl api-resources --verbs=checklist --namespaced -o title | xargs -n 1 kubectl get --show-kind --show-labels --ignore-not-found -n <your_namespace>

It will run by all API Sources obtainable in your cluster and report if they’re present in that namespace.

Now you’ll be able to merely go and kubectl delete <useful resource> to every one as you discover them.

A 1-liner to delete the issue namespace

If you’re nonetheless not capable of delete the namespace, then you need to use the next 1-liner Python script.

Make sure that to switch “” along with your namespace itself:

python3 -c "namespace='<my-namespace>';import atexit,subprocess,json,requests,sys;proxy_process = subprocess.Popen(['kubectl', 'proxy']);atexit.register(proxy_process.kill);p = subprocess.Popen(['kubectl', 'get', 'namespace', namespace, '-o', 'json'], stdout=subprocess.PIPE);p.wait();knowledge = json.load(p.stdout);knowledge['spec']['finalizers'] = [];requests.put('http://127.0.0.1:8001/api/v1/namespaces/{}/finalize'.format(namespace), json=knowledge).raise_for_status()"

About the author

admin

Leave a Comment