Menghapus namespace yang stuck pada status “Terminating” di Kubernetes bisa menjadi tantangan, karena namespace tersebut tidak dapat dihapus secara normal. Masalah ini sering terjadi karena resource atau objek di dalam namespace tidak dapat dihapus dengan benar. Berikut adalah cara singkat untuk mengatasi masalah ini:
Install jq dulu jika belum ada
sudo apt install jq
NAMESPACE=namespace-yang-nyangkut kubectl proxy & kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize
Contoh :
admin@master01:~$ sudo apt install jq [sudo] password for masadmin: Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages were automatically installed and are no longer required: libflashrom1 libftdi1-2 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: libjq1 libonig5 The following NEW packages will be installed: jq libjq1 libonig5 0 upgraded, 3 newly installed, 0 to remove and 10 not upgraded. Need to get 357 kB of archives. After this operation, 1,087 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://id.archive.ubuntu.com/ubuntu jammy/main amd64 libonig5 amd64 6.9.7.1-2build1 [172 kB] Get:2 http://id.archive.ubuntu.com/ubuntu jammy/main amd64 libjq1 amd64 1.6-2.1ubuntu3 [133 kB] Get:3 http://id.archive.ubuntu.com/ubuntu jammy/main amd64 jq amd64 1.6-2.1ubuntu3 [52.5 kB] Fetched 357 kB in 1s (617 kB/s) Selecting previously unselected package libonig5:amd64. (Reading database ... 111665 files and directories currently installed.) Preparing to unpack .../libonig5_6.9.7.1-2build1_amd64.deb ... Unpacking libonig5:amd64 (6.9.7.1-2build1) ... Selecting previously unselected package libjq1:amd64. Preparing to unpack .../libjq1_1.6-2.1ubuntu3_amd64.deb ... Unpacking libjq1:amd64 (1.6-2.1ubuntu3) ... Selecting previously unselected package jq. Preparing to unpack .../jq_1.6-2.1ubuntu3_amd64.deb ... Unpacking jq (1.6-2.1ubuntu3) ... Setting up libonig5:amd64 (6.9.7.1-2build1) ... Setting up libjq1:amd64 (1.6-2.1ubuntu3) ... Setting up jq (1.6-2.1ubuntu3) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.8) ... Scanning processes... Scanning candidates... Scanning linux images... Restarting services... systemctl restart ds_agent.service Service restarts being deferred: /etc/needrestart/restart.d/dbus.service systemctl restart docker.service systemctl restart networkd-dispatcher.service systemctl restart systemd-logind.service systemctl restart unattended-upgrades.service No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host. admin@master01:~$
Daftar namespace nya
admin@master01:~$ kubectl get namespace NAME STATUS AGE cattle-fleet-system Terminating 139m cattle-impersonation-system Terminating 140m cattle-system Terminating 8d cert-manager Active 13m default Active 68d ingress-nginx Active 68d kube-node-lease Active 68d kube-public Active 68d kube-system Active 68d local Active 140m portainer Active 6d simlala Active 63d admin@master01:~$
admin@master01:~$ NAMESPACE=cattle-system admin@master01:~$ kubectl proxy & kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize [1] 656739 Starting to serve on 127.0.0.1:8001 { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "cattle-system", "uid": "c81ddf66-0672-45f5-9aeb-544802daf94e", "resourceVersion": "16598150", "creationTimestamp": "2024-08-13T03:47:09Z", "deletionTimestamp": "2024-08-21T14:16:11Z", "labels": { "kubernetes.io/metadata.name": "cattle-system" }, "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"cattle-system\"}}\n", "management.cattle.io/no-default-sa-token": "true" }, "managedFields": [ { "manager": "kubectl-create", "operation": "Update", "apiVersion": "v1", "time": "2024-08-13T03:47:09Z", "fieldsType": "FieldsV1", "fieldsV1": { "f:status": { "f:phase": {} } } }, { "manager": "kubectl-client-side-apply", "operation": "Update", "apiVersion": "v1", "time": "2024-08-21T00:05:46Z", "fieldsType": "FieldsV1", "fieldsV1": { "f:metadata": { "f:annotations": { ".": {}, "f:kubectl.kubernetes.io/last-applied-configuration": {} } } } }, { "manager": "rancher", "operation": "Update", "apiVersion": "v1", "time": "2024-08-21T12:44:17Z", "fieldsType": "FieldsV1", "fieldsV1": { "f:metadata": { "f:annotations": { "f:management.cattle.io/no-default-sa-token": {} } } } }, { "manager": "kube-controller-manager", "operation": "Update", "apiVersion": "v1", "time": "2024-08-21T14:16:18Z", "fieldsType": "FieldsV1", "fieldsV1": { "f:status": { "f:conditions": { ".": {}, "k:{\"type\":\"NamespaceContentRemaining\"}": { ".": {}, "f:lastTransitionTime": {}, "f:message": {}, "f:reason": {}, "f:status": {}, "f:type": {} }, "k:{\"type\":\"NamespaceDeletionContentFailure\"}": { ".": {}, "f:lastTransitionTime": {}, "f:message": {}, "f:reason": {}, "f:status": {}, "f:type": {} }, "k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": { ".": {}, "f:lastTransitionTime": {}, "f:message": {}, "f:reason": {}, "f:status": {}, "f:type": {} }, "k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": { ".": {}, "f:lastTransitionTime": {}, "f:message": {}, "f:reason": {}, "f:status": {}, "f:type": {} }, "k:{\"type\":\"NamespaceFinalizersRemaining\"}": { ".": {}, "f:lastTransitionTime": {}, "f:message": {}, "f:reason": {}, "f:status": {}, "f:type": {} } } } }, "subresource": "status" } ] }, "spec": {}, "status": { "phase": "Terminating", "conditions": [ { "type": "NamespaceDeletionDiscoveryFailure", "status": "True", "lastTransitionTime": "2024-08-21T14:16:16Z", "reason": "DiscoveryFailed", "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1" }, { "type": "NamespaceDeletionGroupVersionParsingFailure", "status": "False", "lastTransitionTime": "2024-08-21T14:16:16Z", "reason": "ParsedGroupVersions", "message": "All legacy kube types successfully parsed" }, { "type": "NamespaceDeletionContentFailure", "status": "False", "lastTransitionTime": "2024-08-21T14:16:18Z", "reason": "ContentDeleted", "message": "All content successfully deleted, may be waiting on finalization" }, { "type": "NamespaceContentRemaining", "status": "False", "lastTransitionTime": "2024-08-21T14:16:18Z", "reason": "ContentRemoved", "message": "All content successfully removed" }, { "type": "NamespaceFinalizersRemaining", "status": "False", "lastTransitionTime": "2024-08-21T14:16:16Z", "reason": "ContentHasNoFinalizers", "message": "All content-preserving finalizers finished" } ] } } admin@master01:~$
Script lainnya, script ini akan menghapus semua yang masih statusnya terminating
for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}') do kubectl get ns $ns -ojson | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f - done for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}') do kubectl get ns $ns -ojson | jq '.metadata.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f - done