You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

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.

[[email protected] ~]$ kubectl delete ns developer
namespace "developer" deleted

[[email protected] ~]$ 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.

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:

[[email protected] ~]$ kubectl get ns developer -o json  > tmp.json
[[email protected] ~]$ vim  tmp.json
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "developer",
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"conditions": [
{
"lastTransitionTime": "2020-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": "2020-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:

[[email protected] ~]$ 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:

[[email protected] ~]$ kubectl proxy
Starting to serve on 127.0.0.1:800

STEP 3: APPLY THE EDITED JSON FILE

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

[[email protected] ~]$ 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.

[[email protected] ~]$ kubectl get ns developer
Error from server (NotFound): namespaces "developer" not found

Check below Courses that can help you master Kubernetes Internals.

$15.40
$159.87
in stock
Udemy.com
$15.40
$159.87
in stock
Udemy.com
$17.76
$47.36
in stock
Udemy.com

More articles on Kubernetes:

Install Kubernetes Cluster on Ubuntu with kubeadm

 Migrate Docker Compose Application to Kubernetes With Kompose

 Install Kubernetes Dashboard with NodePort

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF