How Do I Update a Namespace in Terminating State?
In Kubernetes, a namespace has two common states: Active and Terminating. The Terminating state is rare. When a namespace has running resources but the namespace is deleted, the namespace becomes Terminating. In this case, the namespace will be automatically deleted by the system after the Kubernetes reclaims the resources in the namespace.
However, in some cases, even if no resource is running in the namespace, the namespace in the Terminating state still cannot be deleted.
To solve this problem, perform the following steps:
- View the namespace details.
$ kubectl get ns | grep rdb rdbms Terminating 6d21h $ kubectl get ns rdbms -o yaml apiVersion: v1 kind: Namespace metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"rdbms"}} creationTimestamp: "2020-05-07T15:19:43Z" deletionTimestamp: "2020-05-07T15:33:23Z" name: rdbms resourceVersion: "84553454" selfLink: /api/v1/namespaces/rdbms uid: 457788ddf-53d7-4hde-afa3-1fertg21ewe1 spec: finalizers: - kubernetes status: phase: Terminating - View resources in the namespace.
# View resources that can be isolated using namespaces in the cluster. $ kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n rdbms
After running the command above, no resource is occupied in the namespace rdbms.
- Delete the namespace.
Directly delete the namespace rdbms.
$ kubectl delete ns rdbms Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system.
The system displays a message indicating that the deletion operation will be complete until the system deletes all useless resources.
- Forcibly delete the namespace.
$ kubectl delete ns rdbms --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. Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system.
After running the command above, the namespace still cannot be deleted.
- Use the native API to delete these resources.
Obtain the namespace details.
$ kubectl get ns rdbms -o json > rdbms.json
Check the JSON configuration defined by the namespace, edit the JSON file, and delete the spec part.
$ cat rdbms.json { "apiVersion": "v1", "kind": "Namespace", "metadata": { "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n" }, "creationTimestamp": "2019-10-14T12:17:44Z", "deletionTimestamp": "2019-10-14T12:30:27Z", "name": "rdbms", "resourceVersion": "8844754", "selfLink": "/api/v1/namespaces/rdbms", "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1" }, "spec": { "finalizers": [ "kubernetes" ] }, "status": { "phase": "Terminating" } }After the PUT request is executed, the namespace is automatically deleted.
$ curl --cacert /root/ca.crt --cert /root/client.crt --key /root/client.key -k -H "Content-Type:application/json" -X PUT --data-binary @rdbms.json https://x.x.x.x:5443/api/v1/namespaces/rdbms/finalize { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "rdbms", "selfLink": "/api/v1/namespaces/rdbms/finalize", "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1", "resourceVersion": "8844754", "creationTimestamp": "2019-10-14T12:17:44Z", "deletionTimestamp": "2019-10-14T12:30:27Z", "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n" } }, "spec": { }, "status": { "phase": "Terminating" }If the namespace still cannot be deleted, check whether the finalizers field exists in the metadata. If the field exists, run the following command to access the namespace and delete the field:
kubectl edit ns rdbms
- For details about how to obtain the cluster certificate, see Obtaining a Cluster Certificate.
- https://x.x.x.x:5443 indicates the address for connecting to the cluster. To obtain the address, perform the following steps:
Log in to the CCE console. In the navigation pane, choose Resource Management > Clusters. Click the name of the cluster to be connected and obtain the IP address and port number next to Internal API Server Address in the Basic Information pane.
Figure 1 Obtaining the access address
- Check whether the namespace has been deleted.
$ kubectl get ns | grep rdb
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.