k8s刪除Terminating狀態的名稱空間
阿新 • • 發佈:2021-07-26
k8s中namespace有兩種常見的狀態,即Active和Terminating狀態,其中後者一般會比較少見,只有當對應的名稱空間下還存在執行的資源,但是該名稱空間被刪除時才會出現所謂的terminating狀態,這種情況下只要等待k8s本身將名稱空間下的資源回收後,該名稱空間將會被系統自動刪除。但是今天遇到名稱空間下已沒相關資源,但依然無法刪除terminating狀態的名稱空間的情況,特此記錄一下.
在專案的k8s中安裝了kube-prometheus,但是根據用處不是太大,而且阿里雲資源不多,就準備解除安裝了,結果卡住了
檢視napespace定義的json配置
刪除掉spec部分即可
[root@k8s-master ~]# kubectl get ns monitoring -o json > monitoring.json [root@k8s-master ~]# cat monitoring.json { "apiVersion": "v1", "kind": "Namespace", "metadata": { "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n" }, "creationTimestamp": "2021-07-19T03:13:20Z", "deletionTimestamp": "2021-07-26T10:23:58Z", "name": "monitoring", "resourceVersion": "2912094", "selfLink": "/api/v1/namespaces/monitoring", "uid": "88cfc047-3736-42bb-ab51-db07fec7c6ef" }, "spec": { "finalizers": [ "kubernetes" ] }, "status": { "conditions": [ { "lastTransitionTime": "2021-07-26T10:24:09Z", "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": "2021-07-26T10:24:04Z", "message": "All legacy kube types successfully parsed", "reason": "ParsedGroupVersions", "status": "False", "type": "NamespaceDeletionGroupVersionParsingFailure" }, { "lastTransitionTime": "2021-07-26T10:24:04Z", "message": "All content successfully deleted, may be waiting on finalization", "reason": "ContentDeleted", "status": "False", "type": "NamespaceDeletionContentFailure" }, { "lastTransitionTime": "2021-07-26T10:24:20Z", "message": "All content successfully removed", "reason": "ContentRemoved", "status": "False", "type": "NamespaceContentRemaining" }, { "lastTransitionTime": "2021-07-26T10:24:04Z", "message": "All content-preserving finalizers finished", "reason": "ContentHasNoFinalizers", "status": "False", "type": "NamespaceFinalizersRemaining" } ], "phase": "Terminating" } }
刪除下面的部分
匯出k8s的金鑰
匯出K8s訪問金鑰 echo $(kubectl config view --raw -oyaml | grep client-cert |cut -d ' ' -f 6) |base64 -d > /tmp/client.pem echo $(kubectl config view --raw -oyaml | grep client-key-data |cut -d ' ' -f 6 ) |base64 -d > /tmp/client-key.pem echo $(kubectl config view --raw -oyaml | grep certificate-authority-data |cut -d ' ' -f 6 ) |base64 -d > /tmp/ca.pem
使用http介面進行刪除
[root@k8s-master ~]# curl --cert /tmp/client.pem --key /tmp/client-key.pem --cacert /tmp/ca.pem -H "Content-Type: application/json" -X PUT --data-binary @/root/monitoring.json https://172.16.8.43:6443/api/v1/namespaces/monitoring/finalize
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "monitoring",
"selfLink": "/api/v1/namespaces/monitoring/finalize",
"uid": "88cfc047-3736-42bb-ab51-db07fec7c6ef",
"resourceVersion": "2912094",
"creationTimestamp": "2021-07-19T03:13:20Z",
"deletionTimestamp": "2021-07-26T10:23:58Z",
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
}
},
"spec": {
},
"status": {
"phase": "Terminating",
"conditions": [
{
"type": "NamespaceDeletionDiscoveryFailure",
"status": "True",
"lastTransitionTime": "2021-07-26T10:24:09Z",
"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": "2021-07-26T10:24:04Z",
"reason": "ParsedGroupVersions",
"message": "All legacy kube types successfully parsed"
},
{
"type": "NamespaceDeletionContentFailure",
"status": "False",
"lastTransitionTime": "2021-07-26T10:24:04Z",
"reason": "ContentDeleted",
"message": "All content successfully deleted, may be waiting on finalization"
},
{
"type": "NamespaceContentRemaining",
"status": "False",
"lastTransitionTime": "2021-07-26T10:24:20Z",
"reason": "ContentRemoved",
"message": "All content successfully removed"
},
{
"type": "NamespaceFinalizersRemaining",
"status": "False",
"lastTransitionTime": "2021-07-26T10:24:04Z",
"reason": "ContentHasNoFinalizers",
"message": "All content-preserving finalizers finished"
}
]
}
再次檢視namespace發現已經被刪除了
[root@k8s-master ~]#kubectl get ns
NAME STATUS AGE
default Active 13d
dev Active 13d
framework Active 11d
ingress-nginx Active 7d21h
kube-node-lease Active 13d
kube-public Active 13d
kube-system Active 13d
kuboard Active 13d
[root@k8s-master ~]#