路漫漫其修远兮
吾将上下而求索

k8s学习:删除Namespace in Terminating State

In Kubernetes, a namespace has two common states: Active and Terminating. The Terminating state is rare. When a namespace has running resources but the namespace is deleted, the namespace becomes Terminating. In this case, the namespace will be automatically deleted by the system after the Kubernetes reclaims the resources in the namespace.

However, in some cases, even if no resource is running in the namespace, the namespace in the Terminating state still cannot be deleted.

To solve this problem, perform the following steps:

  1. View the namespace details.

    $ kubectl  get ns  | grep rdb
    rdbms                  Terminating   6d21h
    
    $ kubectl  get ns rdbms -o yaml
    apiVersion: v1
    kind: Namespace
    metadata:
      annotations:
        kubectl.kubernetes.io/last-applied-configuration: |
          {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"rdbms"}}
      creationTimestamp: "2020-05-07T15:19:43Z"
      deletionTimestamp: "2020-05-07T15:33:23Z"
      name: rdbms
      resourceVersion: "84553454"
      selfLink: /api/v1/namespaces/rdbms
      uid: 457788ddf-53d7-4hde-afa3-1fertg21ewe1
    spec:
      finalizers:
      - kubernetes
    status:
      phase: Terminating

  2. View resources in the namespace.

    # View resources that can be isolated using namespaces in the Kubernetes cluster.
    $ kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n rdbms

    After running the command above, no resource is occupied in the namespace rdbms.

  3. Delete the namespace.

    Directly delete the namespace rdbms.

    $ kubectl  delete ns rdbms
    Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

    The system displays a message indicating that the deletion operation will be complete until the system deletes all useless resources.

  4. Forcibly delete the namespace.

    $ kubectl  delete ns rdbms --force --grace-period=0
    warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
    Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

    After running the command above, the namespace still cannot be deleted.

  5. Use the native API to delete these resources.

    Obtain the namespace details.

    $ kubectl  get ns rdbms  -o json > rdbms.json

    Check the JSON configuration defined by the namespace, edit the JSON file, and delete the spec part.

    $ cat rdbms.json
    {
        "apiVersion": "v1",
        "kind": "Namespace",
        "metadata": {
            "annotations": {
                "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
            },
            "creationTimestamp": "2019-10-14T12:17:44Z",
            "deletionTimestamp": "2019-10-14T12:30:27Z",
            "name": "rdbms",
            "resourceVersion": "8844754",
            "selfLink": "/api/v1/namespaces/rdbms",
            "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1"
        },
        "spec": {
            "finalizers": [
                "kubernetes"
            ]
        },
        "status": {
            "phase": "Terminating"
        }
    }

    After the PUT request is executed, the namespace is automatically deleted.

    $ k replace --raw '/api/v1/namespaces/rdbms/finalize' -f rdbms.json
    {
      "kind": "Namespace",
      "apiVersion": "v1",
      "metadata": {
        "name": "rdbms",
        "selfLink": "/api/v1/namespaces/rdbms/finalize",
        "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1",
        "resourceVersion": "8844754",
        "creationTimestamp": "2019-10-14T12:17:44Z",
        "deletionTimestamp": "2019-10-14T12:30:27Z",
        "annotations": {
          "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
        }
      },
      "spec": {
    
      },
      "status": {
        "phase": "Terminating"
      }

未经允许不得转载:江哥架构师笔记 » k8s学习:删除Namespace in Terminating State

分享到:更多 ()

评论 抢沙发

评论前必须登录!