「KUBERNETES」- Namespace | 常见问题处理

[Sol.] 卡在 Terminating 状态

A namespace is stuck in the Terminating state – IBM Documentation
rook-ceph stuck in Terminating state : unable to delete · Issue #2668 · rook/rook · GitHub

分析

原因众多,可能原因有:
1)在集群中,存在未删除的资源

注意,我们并未深究该问题成因,仅是搜索解决方案,并复制执行。

方案 | kubectl replace | 09/09/2024

kubernetes – Namespace “stuck” as Terminating – Stack Overflow

for Kubernetes 1.28

for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}')
do
  kubectl get ns $ns -ojson | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f -
done

for ns in $(kubectl get ns --field-selector status.phase=Terminating -o jsonpath='{.items[*].metadata.name}')
do
  kubectl get ns $ns -ojson | jq '.metadata.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/$ns/finalize" -f -
done

执行记录:

  • Kubernetes 1.30 自建集群有效 06/07/2025

方案 | 针对命名空间存在未删除资源

kubernetes – Namespace “stucked” as Terminating, How I removed it – Stack Overflow

在 Namespace 中,检查是否存在未删除的资源:

# kubectl api-resources --verbs=list --namespaced -o name  \
    | xargs -n 1 kubectl get --no-headers --show-kind --ignore-not-found -n rook-ceph
configmap/rook-ceph-mon-endpoints   5     635d
secret/rook-ceph-mon   kubernetes.io/rook   4     635d
cephblockpool.ceph.rook.io/ceph-blockpool   Failure
cephfilesystem.ceph.rook.io/ceph-filesystem   1     635d   Ready
cephobjectstore.ceph.rook.io/ceph-objectstore   Deleting
Warning: metallb.io v1beta1 AddressPool is deprecated, consider using IPAddressPool

在 Namespace 中,删除其他资源:

# kubectl -n rook-ceph patch "cephclusters.ceph.rook.io" "rook-ceph" \
    -p '{"metadata":{"finalizers": []}}' --type=merge

方案 | 直接处理命名空间

kubernetes – Namespace “stuck” as Terminating, How I removed it – Stack Overflow

# kubectl get namespace <terminating-namespace> -o json >tmp.json

  {
      "apiVersion": "v1",
      "kind": "Namespace",
      "metadata": {
          "creationTimestamp": "2018-11-19T18:48:30Z",
          "deletionTimestamp": "2018-11-19T18:59:36Z",
          "name": "<terminating-namespace>",
          "resourceVersion": "1385077",
          "selfLink": "/api/v1/namespaces/<terminating-namespace>",
          "uid": "b50c9ea4-ec2b-11e8-a0be-fa163eeb47a5"
      },
      "spec": {
         "finalizers": []
      },
      "status": {
          "phase": "Terminating"
      }
  }

kubectl proxy

curl -k -H "Content-Type: application/json" -X PUT         \
    --data-binary @tmp.json                                \
    http://127.0.0.1:8001/api/v1/namespaces/<terminating-namespace>/finalize

快捷命令:

(
    set -e -x
    kubectl proxy &
    sleep 3
    NAMESPACE=kube-flannel
    kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' > /tmp/temp.json
    curl -k -H "Content-Type: application/json" -X PUT \
        --data-binary @/tmp/temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize

    killall -TERM kubectl
    set +e +x
)

kubectl get namespace "cattle-system" -o json \
  | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \
  | kubectl replace --raw /api/v1/namespaces/cattle-system/finalize -f -