概述
有时候我们操作不规范,或者删除的先后顺序有问题,或者某项关键服务没有启动,导致 Kubernetes 经常会出现无法删除 NameSpace 的情况。这种情况下我们应该怎么办?
规范删除流程
其实,很多时候出现这种情况,主要是因为我们的删除操作不规范,典型的有下面几种情况:
•删除的先后顺序有问题,如:•先删除了 Traefik 的关键组件,再尝试删除包含 Traefik Ingress 或 EdgeIngress 的 CRD•某项关键服务没有启动,如:•对于安装了 Prometheus Operator custom adapter 的 Kubernetes 集群,在 Prometheus 的一些关键组件 scale down 的情况下,删除包含这些监控 CRD 或 HPA custom metric 的 NameSpace
...
综上,根源上,大部分情况下 NameSpace 无法删除,都是我们操作有错在先。
为了避免此类错误再犯,推荐搭建删除按照如下流程:
1.保证所有基础服务组件都是正常运行的状态(如前面提到的,ingress 组件,监控组件,servicemesh 组件。..)2.检查要删除的 NameSpace 下的所有资源,特别是 CRD, 这里推荐使用 Krew - Kubernetes 的 CLI 插件管理器[1] 安装 get-all
[2] 来真正地获取该 NameSpace 下的所有资源,如后面的代码块所示:3.针对其中的一些 CRD 或特殊资源,最好先明确指定删除并确保可以成功删除掉4.最后,再删除该 NameSpace
第 2 步的代码块:(有如此多的 CRD)
代码语言:javascript复制❯ kubectl get-all -n cert-manager
NAME NAMESPACE AGE
configmap/cert-manager-webhook cert-manager 277d
configmap/kube-root-ca.crt cert-manager 277d
endpoints/cert-manager cert-manager 277d
endpoints/cert-manager-webhook cert-manager 277d
endpoints/cert-manager-webhook-dnspod cert-manager 277d
pod/cert-manager-6d6bb4f487-hkwpn cert-manager 85d
pod/cert-manager-6d6bb4f487-wgtd8 cert-manager 85d
pod/cert-manager-cainjector-7d55bf8f78-5797c cert-manager 277d
pod/cert-manager-webhook-577f77586f-txlcx cert-manager 85d
pod/cert-manager-webhook-577f77586f-xh4st cert-manager 85d
pod/cert-manager-webhook-dnspod-5d5566c7bc-5cj4s cert-manager 211d
secret/cert-manager-cainjector-token-h8cqq cert-manager 277d
secret/cert-manager-token-28knj cert-manager 277d
secret/cert-manager-webhook-ca cert-manager 277d
secret/cert-manager-webhook-dnspod-ca cert-manager 277d
secret/cert-manager-webhook-dnspod-letsencrypt cert-manager 277d
secret/cert-manager-webhook-dnspod-secret cert-manager 277d
secret/cert-manager-webhook-dnspod-token-jsjrn cert-manager 277d
secret/cert-manager-webhook-dnspod-webhook-tls cert-manager 277d
secret/cert-manager-webhook-token-qxq44 cert-manager 277d
secret/default-token-mkpmt cert-manager 277d
secret/ewhisper-crt-secret cert-manager 277d
secret/sh.helm.release.v1.cert-manager-webhook-dnspod.v1 cert-manager 277d
secret/sh.helm.release.v1.cert-manager.v1 cert-manager 277d
serviceaccount/cert-manager cert-manager 277d
serviceaccount/cert-manager-cainjector cert-manager 277d
serviceaccount/cert-manager-webhook cert-manager 277d
serviceaccount/cert-manager-webhook-dnspod cert-manager 277d
serviceaccount/default cert-manager 277d
service/cert-manager cert-manager 277d
service/cert-manager-webhook cert-manager 277d
service/cert-manager-webhook-dnspod cert-manager 277d
order.acme.cert-manager.io/ewhisper-crt-6v6s4-2449993249 cert-manager 209d
order.acme.cert-manager.io/ewhisper-crt-89n7g-2449993249 cert-manager 23d
order.acme.cert-manager.io/ewhisper-crt-8g496-2449993249 cert-manager 277d
order.acme.cert-manager.io/ewhisper-crt-jj24l-2449993249 cert-manager 83d
order.acme.cert-manager.io/ewhisper-crt-q8pvw-2449993249 cert-manager 149d
deployment.apps/cert-manager cert-manager 277d
deployment.apps/cert-manager-cainjector cert-manager 277d
deployment.apps/cert-manager-webhook cert-manager 277d
deployment.apps/cert-manager-webhook-dnspod cert-manager 277d
replicaset.apps/cert-manager-6d6bb4f487 cert-manager 277d
replicaset.apps/cert-manager-cainjector-7d55bf8f78 cert-manager 277d
replicaset.apps/cert-manager-webhook-577f77586f cert-manager 277d
replicaset.apps/cert-manager-webhook-dnspod-5d5566c7bc cert-manager 211d
replicaset.apps/cert-manager-webhook-dnspod-5d78f9bfcb cert-manager 217d
replicaset.apps/cert-manager-webhook-dnspod-7c5cd575fc cert-manager 277d
app.catalog.cattle.io/cert-manager cert-manager 270d
app.catalog.cattle.io/cert-manager-webhook-dnspod cert-manager 270d
certificaterequest.cert-manager.io/cert-manager-webhook-dnspod-ca-l57hl cert-manager 277d
certificaterequest.cert-manager.io/cert-manager-webhook-dnspod-webhook-tls-7zwdh cert-manager 277d
certificaterequest.cert-manager.io/cert-manager-webhook-dnspod-webhook-tls-gs57f cert-manager 34d
certificaterequest.cert-manager.io/ewhisper-crt-6v6s4 cert-manager 209d
certificaterequest.cert-manager.io/ewhisper-crt-89n7g cert-manager 23d
certificaterequest.cert-manager.io/ewhisper-crt-8g496 cert-manager 277d
certificaterequest.cert-manager.io/ewhisper-crt-jj24l cert-manager 83d
certificaterequest.cert-manager.io/ewhisper-crt-q8pvw cert-manager 149d
certificate.cert-manager.io/cert-manager-webhook-dnspod-ca cert-manager 277d
certificate.cert-manager.io/cert-manager-webhook-dnspod-webhook-tls cert-manager 277d
certificate.cert-manager.io/ewhisper-crt cert-manager 277d
issuer.cert-manager.io/cert-manager-webhook-dnspod-ca cert-manager 277d
issuer.cert-manager.io/cert-manager-webhook-dnspod-selfsign cert-manager 277d
endpointslice.discovery.k8s.io/cert-manager-9lm6j cert-manager 277d
endpointslice.discovery.k8s.io/cert-manager-webhook-dnspod-q7f8n cert-manager 277d
endpointslice.discovery.k8s.io/cert-manager-webhook-z6qdd cert-manager 277d
rolebinding.rbac.authorization.k8s.io/cert-manager-webhook:dynamic-serving cert-manager 277d
role.rbac.authorization.k8s.io/cert-manager-webhook:dynamic-serving cert-manager 277d
ingressroute.traefik.containo.us/alertmanager cert-manager 244d
ingressroute.traefik.containo.us/grafana cert-manager 255d
ingressroute.traefik.containo.us/grafana-rancher cert-manager 238d
ingressroute.traefik.containo.us/prometheus cert-manager 244d
ingressroute.traefik.containo.us/rsshub cert-manager 268d
ingressroute.traefik.containo.us/ttrss cert-manager 257d
tlsstore.traefik.containo.us/default cert-manager 268d
试试强制删除
如果 NameSpace 已经处于 terminating
的状态,且久久无法删除,可以试试加上这 2 个参数强制删除:
•--force
•--grace-period=0
kubectl delete ns ${NAMESPACE} --force --grace-period=0
强制删除失败?再来试试这种办法
强制删除失败?再来试试这种办法:调用 Kubernetes API 删除
Hard Way 步骤
首先,获取要删除 NameSpace 的 JSON 文件:
代码语言:javascript复制NAMESPACE=cert-manager
kubectl get ns ${NAMESPACE} -o json > namespace.json
然后,编辑 namespace.json
, 从 finalizers
字段中删除 kubernetes
的值并保存,示例如下:
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
...: ...
},
"spec": {
"finalizers": []
},
"status": {
"phase": "Terminating"
}
}
之后,可以通过 kubectl proxy
设置 APIServer 的临时 IP 和端口
kubectl proxy --port=6880 &
最后,进行 API 调用来强制删除:
代码语言:javascript复制curl -k -H "Content-Type: application/json" -X PUT --data-binary @namespace.json http://127.0.0.1:6880/api/v1/namespaces/${NAMESPACE}/finalize
验证是否已经成功删除:
代码语言:javascript复制kubectl get ns ${NAMESPACE}