Leo Zhang
我是一块砖,哪里需要哪里搬!

 

我的问题:删除k8s命名空间时一直处于卡主状态,无法删除。

[root@k8s147 ]# kubectl delete ns kubernetes-dashboard
namespace "kubernetes-dashboard" deleted
^C
[root@k8s147 ]# kubectl delete ns kubernetes-dashboard --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.
namespace "kubernetes-dashboard" force deleted
^C
[root@k8s147 ]# kubectl get ns
NAME                   STATUS        AGE
default                Active        4h58m
kube-node-lease        Active        4h58m
kube-public            Active        4h58m
kube-system            Active        4h58m
kubernetes-dashboard   Terminating   4h49m
monitoring             Active        3h28m

出现这种情况一般都是因为删除资源时没有按照资源绑定关系,无序删除导致。

比如这里命名空间无法删除的情况,这是因为删除命名空间时,该命名空间下还存在其他资源导致。

但是我们往往一个yaml文件会定义很多资源,删除时也会同样删除这些资源,这时候就有可能会发生此问题,这个很不友好,但也只能等待k8s官方优化了。

又比如删除PV时,如果你的PV已经绑定一个PVC,这时你强制删除PV也是会发生此问题,一直处于卡主的状态,正常删除顺序应该是:先删除POD ->再删除其绑定的PVC ->然后再删除该PV。

 

解决办法:本地开启proxy入口,然后通过PUT请求删除指定资源。

# 终端1
[root@k8s147 ]# kubectl proxy Starting to serve on 127.0.0.1:8001
# 终端2
[root@k8s147 ~]# kubectl get ns kubernetes-dashboard -o json | jq '.spec.finalizers=[]' | curl -X PUT http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/finalize -H "Content-Type: application/json" --data @- { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "kubernetes-dashboard", "selfLink": "/api/v1/namespaces/kubernetes-dashboard/finalize", "uid": "34c3b499-6327-4a7a-8d8a-7648127027b9", "resourceVersion": "55380", "creationTimestamp": "2020-04-26T01:51:19Z", "deletionTimestamp": "2020-04-26T06:29:30Z" }, "spec": { }, "status": { "phase": "Terminating", "conditions": [ { "type": "NamespaceDeletionDiscoveryFailure", "status": "True", "lastTransitionTime": "2020-04-26T06:29:40Z", "reason": "DiscoveryFailed", "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request" }, { "type": "NamespaceDeletionGroupVersionParsingFailure", "status": "False", "lastTransitionTime": "2020-04-26T06:29:40Z", "reason": "ParsedGroupVersions", "message": "All legacy kube types successfully parsed" }, { "type": "NamespaceDeletionContentFailure", "status": "False", "lastTransitionTime": "2020-04-26T06:29:40Z", "reason": "ContentDeleted", "message": "All content successfully deleted, may be waiting on finalization" }, { "type": "NamespaceContentRemaining", "status": "False", "lastTransitionTime": "2020-04-26T06:29:51Z", "reason": "ContentRemoved", "message": "All content successfully removed" }, { "type": "NamespaceFinalizersRemaining", "status": "False", "lastTransitionTime": "2020-04-26T06:29:40Z", "reason": "ContentHasNoFinalizers", "message": "All content-preserving finalizers finished" } ] } }

查看命名空间,发现之前处于Terminating状态命名空间已被删除。

[root@k8s147 ~]#kubectl get ns
NAME              STATUS   AGE
default           Active   6h14m
kube-node-lease   Active   6h14m
kube-public       Active   6h14m
kube-system       Active   6h14m
monitoring        Active   4h44m

 

此解决方法参考自官方问题库(https://github.com/kubernetes/kubernetes/issues/77086):

 

作者:Leozhanggg

出处:https://www.cnblogs.com/leozhanggg/p/12780653.html

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted on 2020-04-26 16:43  LeoZhanggg  阅读(746)  评论(0编辑  收藏  举报