問題描述
在k8s中,有些namespace創建之後,覺得不需要了,想刪除掉,但怎麼都刪不掉。類似於下圖,即使用delete --force命令也刪不掉,這些ns最終還是現實爲terminating狀態。
解決辦法
-
將該namespace導出爲json格式。
kubectl get ns redis -o json > tmp.json
-
編輯該json,去除掉spec部分。
vim tmp.json
# 原始數據
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"cattle.io/status": "{\"Conditions\":[{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:31Z\"},{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:30Z\"}]}",
"field.cattle.io/creatorId": "user-gpdkw",
"field.cattle.io/projectId": "c-bz2b7:p-w5xc9",
"lifecycle.cattle.io/create.namespace-auth": "true"
},
"creationTimestamp": "2020-05-15T03:05:12Z",
"deletionTimestamp": "2020-05-15T03:13:31Z",
"labels": {
"cattle.io/creator": "norman",
"field.cattle.io/projectId": "p-w5xc9"
},
"name": "redis",
"resourceVersion": "29284653",
"selfLink": "/api/v1/namespaces/redis",
"uid": "e4763eae-9658-11ea-b716-000c291a5adf"
},
#刪除掉spec包裹的部分
"spec": {
"finalizers": [
"kubernetes"
]
},
"status": {
"phase": "Terminating"
}
}
# 修改後的數據
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"cattle.io/status": "{\"Conditions\":[{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:31Z\"},{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-05-15T03:14:30Z\"}]}",
"field.cattle.io/creatorId": "user-gpdkw",
"field.cattle.io/projectId": "c-bz2b7:p-w5xc9",
"lifecycle.cattle.io/create.namespace-auth": "true"
},
"creationTimestamp": "2020-05-15T03:05:12Z",
"deletionTimestamp": "2020-05-15T03:13:31Z",
"labels": {
"cattle.io/creator": "norman",
"field.cattle.io/projectId": "p-w5xc9"
},
"name": "redis",
"resourceVersion": "29284653",
"selfLink": "/api/v1/namespaces/redis",
"uid": "e4763eae-9658-11ea-b716-000c291a5adf"
},
"spec": {
},
"status": {
"phase": "Terminating"
}
}
- 將該修改應用到apiserver中。
# 打開一個終端,開啓proxy。不然要經過API server的驗證。
kubectl proxy
# 向開放的端口提交修改後的json。
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json 127.0.0.1:8001/api/v1/namespaces/redis/finalize
# 查看是否應用成功
kubectl get ns