在 K8s 中删除资源时,偶尔会遇到资源一直卡在 Terminating 状态无法清除的情况,本文记录了两种移除 finalizer 的解决方法。

事出有因

在部署 kserve 时出现了一些问题,由于时间紧任务急没时间定位排查,于是采取暴力卸载重装,在执行 kubectl delete -f kserve.yaml,在删除 kserve 的 namespace 时一直卡在 Terminating。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(base) [root@node77 kserve]# kubectl get ns
NAME STATUS AGE
bluewhale-platform Active 7d16h
calico-apiserver Active 11d
calico-system Active 11d
default Active 11d
kserve Terminating 17h
kube-federation-system Active 7d17h
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubesphere-controls-system Active 7d18h
kubesphere-monitoring-federated Active 7d18h
kubesphere-monitoring-system Active 7d18h
kubesphere-system Active 7d18h
longhorn-system Active 10d
seldon-mesh Active 10d
tigera-operator Active 11d
user1 Active 67m

迎刃而解

方案一(简单)

使用 kubectl patch 移除指定 CRD 的 finalizer:

1
2
3
kubectl patch crd/此处写CRD的名字 -p '{"metadata":{"finalizers":[]}}' --type=merge

kubectl patch crd/inferenceservices.serving.kserve.io -p '{"metadata":{"finalizers":[]}}' --type=merge

当前 namespace 状态,kserve 一直卡在 Terminating:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(base) [root@node77 test-kserve-deploy]# kubectl get ns
NAME STATUS AGE
bluewhale-platform Active 7d21h
calico-apiserver Active 11d
calico-system Active 11d
cert-manager Active 4h25m
default Active 11d
ingress-nginx Active 4h26m
kserve Terminating 4h36m
kserve-test Active 5m46s
kube-federation-system Active 7d23h
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubesphere-controls-system Active 7d23h
kubesphere-monitoring-federated Active 7d23h
kubesphere-monitoring-system Active 7d23h
kubesphere-system Active 7d23h
longhorn-system Active 10d
seldon-mesh Active 10d
tigera-operator Active 11d
user1 Active 6h21m

查看 kserve 相关的 CRD:

1
2
3
4
5
6
7
(base) [root@node77 test-kserve-deploy]# kubectl get crd | grep kserve
clusterservingruntimes.serving.kserve.io 2024-08-30T03:47:15Z
clusterstoragecontainers.serving.kserve.io 2024-08-30T03:47:15Z
inferencegraphs.serving.kserve.io 2024-08-30T03:47:15Z
inferenceservices.serving.kserve.io 2024-08-29T09:02:22Z
servingruntimes.serving.kserve.io 2024-08-30T03:47:16Z
trainedmodels.serving.kserve.io 2024-08-30T03:47:16Z

对相应 CRD 移除 finalizer:

1
2
(base) [root@node77 test-kserve-deploy]# kubectl patch crd/inferenceservices.serving.kserve.io -p '{"metadata":{"finalizers":[]}}' --type=merge
customresourcedefinition.apiextensions.k8s.io/inferenceservices.serving.kserve.io patched

再次查看 namespace 状态,kserve 已被删除:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(base) [root@node77 test-kserve-deploy]# kubectl get ns
NAME STATUS AGE
bluewhale-platform Active 7d21h
calico-apiserver Active 11d
calico-system Active 11d
cert-manager Active 4h25m
default Active 11d
ingress-nginx Active 4h26m
kserve-test Active 5m49s
kube-federation-system Active 7d23h
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubesphere-controls-system Active 7d23h
kubesphere-monitoring-federated Active 7d23h
kubesphere-monitoring-system Active 7d23h
kubesphere-system Active 7d23h
longhorn-system Active 10d
seldon-mesh Active 10d
tigera-operator Active 11d
user1 Active 6h21m

方案二(繁琐)

需求:删除名为 kserve 的 namespace

  1. 生成一个 tmp.json 文件:
1
2
3
# kubectl get namespace <terminating-namespace> -o json >tmp.json

kubectl get namespace kserve -o json > tmp.json
  1. 编辑 tmp.json 文件,找到下列内容,删除 finalizers 的 kubernetes 即可:
1
2
3
4
5
6
7
8
9
10
11
...
"spec": {
"finalizers": "kubernetes"
}

# 改为:
"spec": {
"finalizers": ""
}

# 或者直接删除finalizers这个key和value
  1. 在终端中启动一个代理:
1
kubectl proxy

输出如下:

1
2
(base) [root@node77 kserve]# kubectl proxy
Starting to serve on 127.0.0.1:8001
  1. 新开一个终端,执行:
1
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://127.0.0.1:8001/api/v1/namespaces/kserve/finalize
  1. 再次查看 namespace 状态,已删除:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(base) [root@node77 kserve]# kubectl get ns
NAME STATUS AGE
bluewhale-platform Active 7d16h
calico-apiserver Active 11d
calico-system Active 11d
default Active 11d
kube-federation-system Active 7d17h
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubesphere-controls-system Active 7d18h
kubesphere-monitoring-federated Active 7d18h
kubesphere-monitoring-system Active 7d18h
kubesphere-system Active 7d18h
longhorn-system Active 10d
seldon-mesh Active 10d
tigera-operator Active 11d
user1 Active 71m

说明

上述两种方案原理是一样的,不仅仅解决 namespace 删除时 Terminating 的问题,理论上,常见的资源(pod、deploy、namespaces、crd 等)出现 Terminating 都能使用此方法。


本站由 sswfive 使用 Stellar 1.44.0 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站总访问量