k8s 删除ns 卡住的解决办法

在某些情况下,在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:

  1. Enable docker admin client bundle

  2. 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"
    }
    }
    
  3. 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"
    }
    }
    
  4. 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

posted @ 2019-06-28 17:52  ElNinoT  阅读(5591)  评论(0编辑  收藏  举报