关于程序员:k8s中删除处于Terminating状态的namespace

3次阅读

共计 4026 个字符,预计需要花费 11 分钟才能阅读完成。

每当删除 namespace 或 pod 等一些 Kubernetes 资源时,有时资源状态会卡在 Terminating,很长时间无奈删除,甚至有时减少 –force grace-period=0 之后还是无奈失常删除。这时就须要 edit 该资源,或者将该资源导出为 json(通过调用原生接口进行删除), 将 finalizers 字段设置为 [],之后 Kubernetes 资源就失常删除了。

查看 ns 状态

# kubectl get ns
NAME              STATUS        AGE
default           Active        48d
kube-node-lease   Active        48d
kube-public       Active        48d
kube-system       Active        48d
monitoring        Terminating   61m

能够看到 monitoring 这个 namespace 始终处于 Terminating 状态,个别状况下强删是删不掉的,强删的办法如下:

kubectl delete ns monitoring --force --grace-period=0

如果删不掉,就参考上面的办法

获取 namespace 的 json 文件

kubectl get ns monitoring -o json > /tmp/monitoring.json

查看 monitoring.json 的内容

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
        },
        "creationTimestamp": "2020-05-26T06:29:13Z",
        "deletionTimestamp": "2020-05-26T07:16:09Z",
        "name": "monitoring",
        "resourceVersion": "6710357",
        "selfLink": "/api/v1/namespaces/monitoring",
        "uid": "db09b70a-6198-443b-8ad7-5287b2483a08"
    },
    "spec": {
        "finalizers": ["kubernetes"]
    },
    "status": {"phase": "Terminating"}
}

批改此 monitoring.json 文件内容为:

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
        },
        "creationTimestamp": "2020-05-26T06:29:13Z",
        "deletionTimestamp": "2020-05-26T07:16:09Z",
        "name": "monitoring",
        "resourceVersion": "6710357",
        "selfLink": "/api/v1/namespaces/monitoring",
        "uid": "db09b70a-6198-443b-8ad7-5287b2483a08"
    },
    "spec": { },
    "status": {"phase": "Terminating"}
}

调用 api-server 接口进行删除

关上一个新的终端,或者把上面的命令放到后盾执行

kubectl proxy

调用接口删除

# curl -k -H "Content-Type: application/json" -X PUT --data-binary @monitoring.json http://127.0.0.1:8001/api/v1/namespaces/monitoring/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "monitoring",
    "selfLink": "/api/v1/namespaces/monitoring/finalize",
    "uid": "db09b70a-6198-443b-8ad7-5287b2483a08",
    "resourceVersion": "6710357",
    "creationTimestamp": "2020-05-26T06:29:13Z",
    "deletionTimestamp": "2020-05-26T07:16:09Z",
    "annotations": {"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
    }
  },
  "spec": { },
  "status": {"phase": "Terminating"}
}

输入以上内容示意删除胜利。

: 如果kubectl get ns monitoring -o json 的后果中 "spec": {} 中为空,则须要看下 metadata 局部是否有 finalizers 字段,如下以 cattle-system 所示:

{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {"cattle.io/status": "{\"Conditions\":[{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-10-22T11:22:02Z\"},{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2020-10-22T11:22:07Z\"}]}",
            "field.cattle.io/projectId": "local:p-wfknh",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"cattle-system\"}}\n",
            "lifecycle.cattle.io/create.namespace-auth": "true"
        },
        "creationTimestamp": "2020-10-22T11:20:30Z",
        "deletionGracePeriodSeconds": 0,
        "deletionTimestamp": "2020-10-22T11:37:20Z",
        "finalizers": ["controller.cattle.io/namespace-auth"],
        "labels": {"field.cattle.io/projectId": "p-wfknh"},
        "name": "cattle-system",
        "resourceVersion": "165368",
        "selfLink": "/api/v1/namespaces/cattle-system",
        "uid": "223ad163-507c-4efe-b3a3-d3bc4b7a5211"
    },
    "spec": {},
    "status": {"phase": "Terminating"}
}

这里 "spec": {}, 后果为空,无论咱们怎么执行,此时此 ns 都不会被删除,此时想弄清楚这个问题,须要先理解下 finalizers 这个的含意 Finalizers 字段属于 Kubernetes GC 垃圾收集器,是一种删除拦挡机制,可能让控制器实现异步的删除前(Pre-delete)回调。其存在于任何一个资源对象的 Meta 中,在 k8s 源码中申明为 []string,该 Slice 的内容为须要执行的拦截器名称。
对带有 Finalizer 的对象的第一个删除申请会为其 metadata.deletionTimestamp 设置一个值,但不会真的删除对象。一旦此值被设置,finalizers 列表中的值就只能被移除。
当 metadata.deletionTimestamp 字段被设置时,负责监测该对象的各个控制器会通过轮询对该对象的更新申请来执行它们所要解决的所有 Finalizer。当所有 Finalizer 都被执行过,资源被删除。
metadata.deletionGracePeriodSeconds 的取值管制对更新的轮询周期。
每个控制器要负责将其 Finalizer 从列表中去除。
每执行完一个就从 finalizers 中移除一个,直到 finalizers 为空,之后其宿主资源才会被真正的删除。
看到这里有 finalizers 时,须要把上面的两行一并删除,办法如下:

kubectl edit ns cattle-system
进去后,间接删除即可,保留退出后,处于 Terminating 状态的 ns 便没有了。

喜爱的敌人能够关注下我的集体公众号哦

正文完
 0