在用longhorn工具做k8s存储卷动态预配的时候,需要修改longhorn.yaml的一个默认参数,修改完成需要重新加载longhorn.yaml,结果重新加载出错了,修改的参数没有生效,于是执行kubectl delete -f longhorn.yaml想将部署的资源全部删除重新启动;但是发现创建的namespace无法删除,状态一直是Terminating;
[root@k8smaster longhorn]# kubectl get ns NAME STATUS AGE default Active 49d dev Active 29d kube-node-lease Active 49d kube-public Active 49d kube-system Active 49d longhorn-system Terminating 11h stage Active 29d [root@k8smaster longhorn]# kubectl delete ns longhorn-system Error from server (Conflict): Operation cannot be fulfilled on namespaces "longhorn-system": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system. 从报错来看是因为在该名称空间中还有没有删除的内容,但是查找了该名称空间,该名称空间中已经没有运行的pod了;
解决方式:通过api来删除
先查找到该namespace的api接口地址 [root@k8smaster longhorn]# kubectl get ns/longhorn-system -o yaml apiVersion: v1 kind: Namespace metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"longhorn-system"}} creationTimestamp: "2021-06-29T15:43:23Z" deletionTimestamp: "2021-06-30T02:48:32Z" name: longhorn-system resourceVersion: "6197365" selfLink: /api/v1/namespaces/longhorn-system uid: bcc59118-d8f0-11eb-b1e9-000c29087c24 spec: finalizers: - kubernetes status: phase: Terminating
导出该namespace json格式的详细信息 [root@k8smaster longhorn]# kubectl get ns longhorn-system -o json > longhorn-system.json [root@k8smaster longhorn]# cat longhorn-system.json { "apiVersion": "v1", "kind": "Namespace", "metadata": { "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"longhorn-system\"}}\n" }, "creationTimestamp": "2021-06-29T15:43:23Z", "deletionTimestamp": "2021-06-30T02:48:32Z", "name": "longhorn-system", "resourceVersion": "6197365", "selfLink": "/api/v1/namespaces/longhorn-system", "uid": "bcc59118-d8f0-11eb-b1e9-000c29087c24" }, "spec": { "finalizers": [ "kubernetes" ] }, "status": { "phase": "Terminating" } } 删除finalizers的认证方式; 删除后的json文件; [root@k8smaster longhorn]# cat longhorn-system.json { "apiVersion": "v1", "kind": "Namespace", "metadata": { "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"longhorn-system\"}}\n" }, "creationTimestamp": "2021-06-29T15:43:23Z", "deletionTimestamp": "2021-06-30T02:48:32Z", "name": "longhorn-system", "resourceVersion": "6197365", "selfLink": "/api/v1/namespaces/longhorn-system", "uid": "bcc59118-d8f0-11eb-b1e9-000c29087c24" }, "spec": { "finalizers": [ ] }, "status": { "phase": "Terminating" } }
调用接口删除该namespace 1、因为k8s接口默认使用https访问的,所以需要临时开一个HTTP代理端口 [root@k8smaster ~]# kubectl proxy Starting to serve on 127.0.0.1:8001 2、新开一个终端,执行调用接口命令,注意接口最后加上finalize [root@k8smaster longhorn]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @longhorn-system.json http://127.0.0.1:8001/api/v1/namespaces/longhorn-system/finalize { "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "longhorn-system", "selfLink": "/api/v1/namespaces/longhorn-system/finalize", "uid": "bcc59118-d8f0-11eb-b1e9-000c29087c24", "resourceVersion": "6202444", "creationTimestamp": "2021-06-29T15:43:23Z", "deletionTimestamp": "2021-06-30T02:48:32Z", "annotations": { "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"longhorn-system\"}}\n" } }, "spec": { }, "status": { "phase": "Terminating" } } 3、查看k8s的ns,发现longhorn-system被删除了 [root@k8smaster longhorn]#kubectl get ns NAME STATUS AGE default Active 49d dev Active 29d kube-node-lease Active 49d kube-public Active 49d kube-system Active 49d stage Active 29d