一、前言
删除某个应用或模块的时候,可能会出现命名空间的状态一直处于Terminating状态,无论是重启k8s还是重启所有服务器都没卵用。
k8s遇到有无法删除的ns(namespace)命名空间的时候,可以尝试几种删除方式
二、删除命名空间namespace
1、先查看k8s
[root@k8s-master1 ~]# kubectl get ns
NAME STATUS AGE
cattle-impersonation-system Terminating 2d14h
cattle-system Terminating 2d14h
default Active 13d
ingress-nginx Terminating 4d13h
kube-node-lease Active 13d
kube-public Active 13d
kube-system Active 13d
monitoring Active 12d
2、可以先逐步尝试以下几种删除kubernetes命名空间namespace的方法
kubectl delete ns <要删除的命名空间>
kubectl delete ns <要删除的命名空间> --froce
kubectl delete ns <要删除的命名空间> --force --grace-period=0
3、如果上面的3中方法依然无法删除kubernetes命名空间,则可以再尝试使用接口去删除
三、使用接口的方式删除kubernetes命名空间
1、获取命名空间的json文件,下面以删除ingress-nginx这个命名空间为例。
kubectl get ns ingress-nginx -o json > delete-ns-ingress-nginx.json
如得到的源文件:
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"cattle.io/status": "{\"Conditions\":[{\"Type\":\"ResourceQuotaInit\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2023-06-28T22:44:35Z\"},{\"Type\":\"InitialRolesPopulated\",\"Status\":\"True\",\"Message\":\"\",\"LastUpdateTime\":\"2023-06-28T22:44:41Z\"}]}",
"field.cattle.io/projectId": "c-m-x64sl2w6:p-4845x",
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"labels\":{\"app.kubernetes.io/instance\":\"ingress-nginx\",\"app.kubernetes.io/name\":\"ingress-nginx\"},\"name\":\"ingress-nginx\"}}\n",
"lifecycle.cattle.io/create.namespace-auth": "true",
"management.cattle.io/no-default-sa-token": "true"
},
"creationTimestamp": "2023-06-26T15:26:03Z",
"deletionGracePeriodSeconds": 0,
"deletionTimestamp": "2023-06-29T15:06:06Z",
"finalizers": [
"controller.cattle.io/namespace-auth" #删除这一行,并且保持finalizers数组为空,即[]
],
"labels": {
"app.kubernetes.io/instance": "ingress-nginx",
"app.kubernetes.io/name": "ingress-nginx",
"kubernetes.io/metadata.name": "ingress-nginx"
},
"name": "ingress-nginx",
"resourceVersion": "147165",
"uid": "7dc28b36-5960-4237-b1b5-41b8d6e20f7c"
},
"spec": {},
"status": {
"conditions": [
{
"lastTransitionTime": "2023-06-29T15:06:14Z",
"message": "All resources successfully discovered",
"reason": "ResourcesDiscovered",
"status": "False",
"type": "NamespaceDeletionDiscoveryFailure"
},
{
"lastTransitionTime": "2023-06-29T15:06:14Z",
"message": "All legacy kube types successfully parsed",
"reason": "ParsedGroupVersions",
"status": "False",
"type": "NamespaceDeletionGroupVersionParsingFailure"
},
{
"lastTransitionTime": "2023-06-29T15:06:14Z",
"message": "All content successfully deleted, may be waiting on finalization",
"reason": "ContentDeleted",
"status": "False",
"type": "NamespaceDeletionContentFailure"
},
{
"lastTransitionTime": "2023-06-29T15:08:52Z",
"message": "All content successfully removed",
"reason": "ContentRemoved",
"status": "False",
"type": "NamespaceContentRemaining"
},
{
"lastTransitionTime": "2023-06-29T15:06:14Z",
"message": "All content-preserving finalizers finished",
"reason": "ContentHasNoFinalizers",
"status": "False",
"type": "NamespaceFinalizersRemaining"
}
],
"phase": "Terminating"
}
}
2、搜索json文件中finalizers,将其数组置空。如本次没删之前
3、在k8s的master节点上开启proxy
kubectl proxy
注意看端口是8001
4、再开一个k8s-master节点的终端以便我们执行命令
curl -k -H "Content-Type: application/json" -X PUT --data-binary @delete-ns-ingress-nginx.json http://127.0.0.1:8001/api/v1/namespaces/ingress-nginx/finalize
注:delete-ns-ingress-nginx.json 是刚才你导出并修改后的json文件,注意执行命令的时候前面有@字符。
http://127.0.0.1:8001/api/v1/namespaces/ingress-nginx/finalize 中的 ingress-nginx 换成你要删除的命名空间字符串
执行完命令后,又返回一堆的字符串,不用管它。
5、再次查看所有Kubernetes的命名空间看目标ns是否被删除了
可以看到本次要删除的命名空间ingress-nginx已经被彻底干掉了。
------ok--------kahn------2023年7月1日13:29:26-----------