Friday, November 22, 2024
Google search engine
HomeData Modelling & AIHow to force delete a Kubernetes Namespace

How to force delete a Kubernetes Namespace

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

Namespaces are a way to divide Kubernetes cluster resources between multiple users and teams. They are intended for use in environments with many users spread across multiple teams, or projects. For Kubernetes clusters with just a few users, there may be no need to create or think about namespaces.

You may have already created a Kubernetes cluster using kubeadm as discussed in our Kubernetes installation guide. As you are doing your deployments, you deleted a namespace before all its resources have been removed and want to reuse the same name to recreate your resources. However, your namespace is stuck at a terminating state.

[mkemei@kubernetesmaster01 ~]$ kubectl delete ns developer
namespace "developer" deleted

[mkemei@kubernetesmaster01 ~]$ kubectl get ns developer
NAME STATUS AGE
developer Terminating 1h

There are various ways in which we can approach this issue. In this article, I will guide you through resolving this problem using kubectl proxy.

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

STEP 1: EDIT THE NAMESPACE

First, we need to edit the terminating namespace so as to remove the kubernetes finalizer in it’s spec. Finalizers are values set in kubernetes resources, that when present ensure that a hard delete of a resource is not possible while they exist. This explains why the namespace remains at a terminating state if not gracefully deleted.

We can do this using JSON format. Run the following commands:

[mkemei@kubernetesmaster01 ~]$ kubectl get ns developer -o json  > tmp.json
[mkemei@kubernetesmaster01 ~]$ vim  tmp.json
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "developer",
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"conditions": [
{
"lastTransitionTime": "2023-07-19T12:29:58Z",
"message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request",
"reason": "DiscoveryFailed",
"status": "True",
"type": "NamespaceDeletionDiscoveryFailure"
},
{
"lastTransitionTime": "2023-07-19T12:30:02Z",
"message": "Failed to delete all resource types, 1 remaining: unexpected items still remain in namespace: developer for gvr: /v1, Resource=pods",
"reason": "ContentDeletionFailed",
"status": "True",
"type": "NamespaceDeletionContentFailure"
}
],
"phase": "Terminating"
}
}

So, the spec.finalizer value should be as shown below:

[mkemei@kubernetesmaster01 ~]$ cat tmp.json
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "developer"
},
"spec": {
"finalizers": []
}
}

STEP 2: OPEN A NEW TERMINAL

Secondly, we will use an HTTP Proxy to access the Kubernetes API. The proxy server allows us to explore the kubernetes API using curlwget, or a browser. We can do this by running the following command on a new terminal:

[mkemei@kubernetesmaster01 ~]$ kubectl proxy
Starting to serve on 127.0.0.1:8001

STEP 3: APPLY THE EDITED JSON FILE

Finally, we will use curl  to apply the namespace without the problematic finalizer.

$ curl -k -H "Content-Type: application/json" -X PUT --data-binary \
  @tmp.json http://127.0.0.1:8001/api/v1/namespaces/developer/finalize

{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "developer",
"spec": {},
"status": {
"phase": "Terminating",
"conditions": [
{
"type": "NamespaceDeletionDiscoveryFailure",
"status": "True",
"lastTransitionTime": "2020-07-19T12:53:50Z",
"reason": "DiscoveryFailed",
"message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request"
},
{
"type": "NamespaceDeletionGroupVersionParsingFailure",
"status": "False",
"lastTransitionTime": "2020-07-19T12:53:54Z",
"reason": "ParsedGroupVersions",
"message": "All legacy kube types successfully parsed"
},
{
"type": "NamespaceDeletionContentFailure",
"status": "False",
"lastTransitionTime": "2020-07-19T12:53:54Z",
"reason": "ContentDeleted",
"message": "All content successfully deleted"
}
]
}

We have successfully deleted the namespace.

[mkemei@kubernetesmaster01 ~]$ kubectl get ns developer
Error from server (NotFound): namespaces "developer" not found

Check below Courses that can help you master Kubernetes Internals.

More articles on Kubernetes:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments