kubernetes 删除namespace 后一直处于Terminating状态


出现无法删除的情况,是因为 kubelet 阻塞,有其他的资源在使用该 namespace,比如 CRD 等,尝试重启 kubelet,再删除该 namespace 也不好使。

在尝试以下命令强制删除也不好使:

#!/usr/bin/env bash


if [ -z "$1" ];then
  echo "print input namespace"
  exit -1
fi

kubectl get ns $1 -ojson | jq 'del(.spec.finalizers[0])' > 0000.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @0000.json   http://127.0.0.1:8001/api/v1/namespaces/$1/finalize


echo kubectl delete ns $1
kubectl delete ns  ```

解决:

1. 运行以下命令以查看处于“Terminating”状态的namespace:

```null
 kubectl get namespaces```

2. 选择一个Terminating namespace,并查看namespace 中的finalizer。运行以下命令:

```null
 kubectl get namespace  -o yaml```

得到类似信息如下:

```null
creationTimestamp: "2019-11-20T15:18:06Z"deletionTimestamp: "2020-01-16T02:50:02Z"name: resourceVersion: "3249493"selfLink: /api/v1/namespaces/knative-eventinguid: f300ea38-c8c2-4653-b432-b66103e412db
  1. 导出 json 格式到 tmp.json:
 kubectl get namespace  -o json >tmp.json
  1. 编辑 tmp.josn,删除 finalizers 字段的值:
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "creationTimestamp": "2021-06-10T01:40:35Z",
        "deletionGracePeriodSeconds": 0,
        "deletionTimestamp": "2021-06-12T02:56:40Z",
        "finalizers": [
        ],
        "labels": {
            "kubesphere.io/namespace": "kubesphere-monitoring-system",
            "kubesphere.io/workspace": "system-workspace"
        },
        "managedFields": [
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:kubesphere.io/namespace": {},
                            "f:kubesphere.io/workspace": {}
                        }
                    }
                },
                "manager": "kubectl",
                "operation": "Update",
                "time": "2021-06-10T01:40:47Z"
            },
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:finalizers": {
                            ".": {},
                            "v:\"finalizers.kubesphere.io/namespaces\"": {}
                        },
                        "f:ownerReferences": {
                            ".": {},
                            "k:{\"uid\":\"a6a748bd-e55f-4139-b3ff-d50269c969c5\"}": {
                                ".": {},
                                "f:apiVersion": {},
                                "f:blockOwnerDeletion": {},
                                "f:controller": {},
                                "f:kind": {},
                                "f:name": {},
                                "f:uid": {}
                            }
                        }
                    }
                },
                "manager": "controller-manager",
                "operation": "Update",
                "time": "2021-06-10T01:46:14Z"
            },
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:status": {
                        "f:phase": {}
                    }
                },
                "manager": "kube-controller-manager",
                "operation": "Update",
                "time": "2021-06-12T03:01:42Z"
            }
        ],
        "name": "kubesphere-monitoring-system",
        "ownerReferences": [
            {
                "apiVersion": "tenant.kubesphere.io/v1alpha1",
                "blockOwnerDeletion": true,
                "controller": true,
                "kind": "Workspace",
                "name": "system-workspace",
                "uid": "a6a748bd-e55f-4139-b3ff-d50269c969c5"
            }
        ],
        "resourceVersion": "27410085",
        "selfLink": "/api/v1/namespaces/kubesphere-monitoring-system",
        "uid": "a148512f-54d3-4443-ae89-91cb97c6d6c2"
    },
    "spec": {},
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2021-06-12T02:57:16Z",
                "message": "All resources successfully discovered",
                "reason": "ResourcesDiscovered",
                "status": "False",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2021-06-12T02:57:16Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2021-06-12T03:01:42Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2021-06-12T03:01:42Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2021-06-12T02:57:16Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}
  1. 开启 proxy :
kubectl proxy```

6\. 打开新的terminal 窗口,运行以下命令:

```null
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces//finalize

输出以下信息:

"selfLink": "/api/v1/namespaces/istio-system/finalize","uid": "2e274537-727f-4a8f-ae8c-397473ed619a","resourceVersion": "3249492","creationTimestamp": "2019-11-20T15:18:06Z","deletionTimestamp": "2020-01-16T02:50:02Z"```

7\. 检查该namespace 是否被删除:

```null
 kubectl get namespaces

继续以上步骤删除其他 Terminating namespace。
https://blog.csdn.net/ANXIN997483092/article/details/104233494


文章作者:   Joey Wang
版权声明:   本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Joey Wang !
  目录