1. Preface

I was using Kubernetes and had a problem with a Kubernetes NameSpace not being deleted properly.

The NameSpace status had been in Terminating for quite a while, and I decided to force the NameSpace to be deleted.

2. Steps

1. View the existing NameSpace

1
2
3
4
5
6
7
8
[root@master ~]# kubectl get ns
NAME                   STATUS        AGE
default                Active        5h46m
istio-system           Terminating   11m
kube-node-lease        Active        5h46m
kube-public            Active        5h46m
kube-system            Active        5h46m
kubernetes-dashboard   Active        5h46m

2. try to force delete NameSpace

1
2
[root@master ~]# kubectl delete ns istio-system --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.

After executing the command stuck for a long time but no effect. It seems that it can not be deleted, you have to force the deletion

3. Get the NameSpace information that needs to be forcibly deleted

1
[root@master ~]# kubectl get namespace istio-system -o json > istio-system.json

finalizers related content

5. running kube-proxy

1
2
[root@master ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001

6. Forced deletion of NameSpace via API

Run a new terminal and call the API to delete the NameSpace.

1
[root@master ~]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @istio-system.json http://127.0.0.1:8001/api/v1/namespaces/istio-system/finalize

7. Shut down kube-proxy and make sure the NameSpace is deleted

Press CTRL-C to close kube-proxy, and then confirm the current NameSpace.

1
2
3
4
5
6
7
[root@master ~]# kubectl get ns
NAME                   STATUS        AGE
default                Active        5h48m
kube-node-lease        Active        5h48m
kube-public            Active        5h48m
kube-system            Active        5h48m
kubernetes-dashboard   Active        5h48m

You can see that NameSpace has been forcibly deleted.