在某些情況下,在k8s中會無法刪除某個namespace,它會一直卡在terminating狀態下。解決這個問題的步驟為:
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace>
這個指令找到阻礙這個namespace刪除的資源,然后手動刪除這些資源。
但是大部分時候,這些資源也殺不掉,解決辦法是執行
kubectl edit pod -o json
然后找到里面的"finalizers",把它的值設置成一個空數組。
解決這些資源后,參照以下文檔刪除那個ns:
Resolution
To delete a namespace stuck in the finalizing state:
-
Enable docker admin client bundle
-
GET the namespace object
$ NAMESPACE=skynet $ kubectl get ns $NAMESPACE -o json > /tmp/${NAMESPACE}.json $ cat /tmp/${NAMESPACE}.json { "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2019-02-04T16:15:06Z", "name": "skynet", "resourceVersion": "20879144", "selfLink": "/api/v1/namespaces/skynet", "uid": "0951bcf9-2898-11e9-8e38-0242ac11000b" }, "spec": { "finalizers": [ "kubernetes" ] }, "status": { "phase": "Active" } }
-
Use a text editor to delete the all elements in finalizer array, leaving only an empty array
[]
such as below example:{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2019-02-04T16:15:06Z", "name": "skynet", "resourceVersion": "20879144", "selfLink": "/api/v1/namespaces/skynet", "uid": "0951bcf9-2898-11e9-8e38-0242ac11000b" }, "spec": { "finalizers": [ ] }, "status": { "phase": "Active" } }
-
PUT the namespace object without the finalizer
a. Get a bearer token
``` UCP_URL=ucp.example.com USERNAME=admin PASSWORD=supersecretadminpassword curl -sk -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" https://${UCP_URL}/auth/login | jq -r .auth_token > auth-token ```
b. Issue an HTTP PUT
`curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json https://kubernetes-cluster-ip/api/v1/namespaces/skynet/finalize` Example: `curl -k -H "Content-Type: application/json" -H "authorization: Bearer $(cat ./auth-token)" -X PUT --data-binary @/tmp/skynet.json https://ucp.example.com/api/v1/namespaces/skynet/finalize`
After the finalizer array is empty and the status is in terminating, kubernetes will delete the namespace.
參考:https://success.docker.com/article/kubernetes-namespace-stuck-in-terminating