作者信息:https://home.cnblogs.com/u/huangjiabobk

手把手带你,在K8S集群中删除处于 "terminating" 状态命名空间。

1. 背景

背景:在Kubernetes集群中,有时侯想要删掉某个长时间不用命名空间以释放资源却发现删不掉,经过反复删除发现想要删除命名空间处在Terminating状态,强制删除也无济于事。作者也遇到这样事情,我又是如何删除掉呢?

#kubectl get ns
NAME                   STATUS        AGE
default                Active        44d
kube-node-lease        Active        44d
kube-public            Active        44d
kube-system            Active        44d
kubernetes-dashboard   Active        44d
rook-ceph              Terminating   15d
2. 使用 "kubectl delete ns rook-ceph" 删除

发现根本删除不掉,一直处于被夯住状态......, 最终不得使用 Ctrl +c 结束。

#kubectl delete ns rook-ceph 
namespace "rook-ceph" deleted
......
3. 使用 "kubectl delete ns rook-ceph --grace-period=0 --force" 强制删除
#kubectl delete ns rook-ceph --grace-period=0 --force
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.
namespace "rook-ceph" force deleted
...............

同样,发现根本删除不掉,一直处于被夯住状态......, 最终不得使用 Ctrl +c 结束。

4. 使用调用api-server接口进行删除 删除成功

4.1 将要删除命名空间使用 “kubectl get ns rook-ceph -o json > rook-ceph.json” 该命令将其转化为json格式并保存在当前目录。

#kubectl get ns
NAME                   STATUS        AGE
default                Active        44d
kube-node-lease        Active        44d
kube-public            Active        44d
kube-system            Active        44d
kubernetes-dashboard   Active        44d
rook-ceph              Terminating   15d
#kubectl get ns rook-ceph -o json > rook-ceph.json
#cat rook-ceph.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rook-ceph\"}}\n"
        },
        "creationTimestamp": "2024-03-23T10:01:56Z",
        "deletionTimestamp": "2024-04-07T13:15:10Z",
        "labels": {
            "kubernetes.io/metadata.name": "rook-ceph"
        },
        "name": "rook-ceph",
        "resourceVersion": "658949",
        "uid": "66c4eb8a-33ac-4cd3-a7b6-89398b739d6b"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2024-04-08T03:30:14Z",
                "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1",
                "reason": "DiscoveryFailed",
                "status": "True",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2024-04-08T02:31:11Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "Some resources are remaining: cephblockpools.ceph.rook.io has 1 resource instances, cephclusters.ceph.rook.io has 1 resource instances, cephfilesystems.ceph.rook.io has 1 resource instances, cephfilesystemsubvolumegroups.ceph.rook.io has 1 resource instances, configmaps. has 1 resource instances, secrets. has 1 resource instances",
                "reason": "SomeResourcesRemain",
                "status": "True",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "Some content in the namespace has finalizers remaining: ceph.rook.io/disaster-protection in 2 resource instances, cephblockpool.ceph.rook.io in 1 resource instances, cephcluster.ceph.rook.io in 1 resource instances, cephfilesystem.ceph.rook.io in 1 resource instances, cephfilesystemsubvolumegroup.ceph.rook.io in 1 resource instances",
                "reason": "SomeFinalizersRemain",
                "status": "True",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}
[root@k8s-master01 ~]#

4.2 编辑rook-ceph.json文件,将spec.finalizers设为空数组。

#将rook-ceph.json文件"spec"中"kubernetes"删除后保存。

"spec": {
    "finalizers": [
        "kubernetes"
    ]
#更新后的样子:

"spec": {
        "finalizers": [
        ]
    },

4.3 执行kubectl proxy,启动一个kube api server本地代理,待执行完删除命令后再结束掉。

#kubectl proxy 
Starting to serve on 127.0.0.1:8001

4.4 另开一个窗口执行 "curl -k -H Content-Type: application/json -X PUT --data-binary @rook-ceph.json http://127.0.0.1:8001/api/v1/namespaces/rook-ceph/finalize" 进行接口调用方式删除处于Terminating状态命名空间。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

[root@k8s-master01 ~]#curl -k -H Content-Type: application/json -X PUT --data-binary @rook-ceph.json http://127.0.0.1:8001/api/v1/namespaces/rook-ceph/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "rook-ceph",
    "uid": "66c4eb8a-33ac-4cd3-a7b6-89398b739d6b",
    "resourceVersion": "658949",
    "creationTimestamp": "2024-03-23T10:01:56Z",
    "deletionTimestamp": "2024-04-07T13:15:10Z",
    "labels": {
      "kubernetes.io/metadata.name": "rook-ceph"
    },
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rook-ceph\"}}\n"
    },
    "managedFields": [
      {
        "manager": "kubectl-create",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-03-23T10:01:56Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:labels": {
              ".": {},
              "f:kubernetes.io/metadata.name": {}
            }
          }
        }
      },
      {
        "manager": "kubectl-client-side-apply",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-03-23T11:01:37Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:annotations": {
              ".": {},
              "f:kubectl.kubernetes.io/last-applied-configuration": {}
            }
          }
        }
      },
      {
        "manager": "kube-controller-manager",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-04-08T03:30:14Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:status": {
            "f:conditions": {
              ".": {},
              "k:{\"type\":\"NamespaceContentRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionContentFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceFinalizersRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              }
            }
          }
        },
        "subresource": "status"
      }
    ]
  },
  "spec": {},
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2024-04-08T03:30:14Z",
        "reason": "DiscoveryFailed",
        "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1"
      },
      {
        "type": "NamespaceDeletionGroupVersionParsingFailure",
        "status": "False",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2024-04-08T02:31:11Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted, may be waiting on finalization"
      },
      {
        "type": "NamespaceContentRemaining",
        "status": "True",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "SomeResourcesRemain",
        "message": "Some resources are remaining: cephblockpools.ceph.rook.io has 1 resource instances, cephclusters.ceph.rook.io has 1 resource instances, cephfilesystems.ceph.rook.io has 1 resource instances, cephfilesystemsubvolumegroups.ceph.rook.io has 1 resource instances, configmaps. has 1 resource instances, secrets. has 1 resource instances"
      },
      {
        "type": "NamespaceFinalizersRemaining",
        "status": "True",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "SomeFinalizersRemain",
        "message": "Some content in the namespace has finalizers remaining: ceph.rook.io/disaster-protection in 2 resource instances, cephblockpool.ceph.rook.io in 1 resource instances, cephcluster.ceph.rook.io in 1 resource instances, cephfilesystem.ceph.rook.io in 1 resource instances, cephfilesystemsubvolumegroup.ceph.rook.io in 1 resource instances"
      }
    ]
  }
}

4.5 执行完后,你会惊奇发现,处于Terminating状态命名空间就这样被删除了。

#kubectl get ns
NAME                   STATUS   AGE
default                Active   44d
kube-node-lease        Active   44d
kube-public            Active   44d
kube-system            Active   44d
kubernetes-dashboard   Active   44d

5.5 祝大家能够轻松删除处于Terminating状态命名空间。

posted @ 2024-04-08 17:12  黄嘉波  阅读(1768)  评论(0编辑  收藏  举报
版权声明:原创作品,谢绝转载!否则将追究法律责任。--作者 黄嘉波