我有一个“卡住”的名称空间,我删除显示在这个永恒的“终止”状态。


当前回答

我喜欢这个答案 它只有两个命令。

在一个终端:

kubectl proxy

在另一个终端:

kubectl get ns delete-me -o json | \
  jq '.spec.finalizers=[]' | \
  curl -X PUT http://localhost:8001/api/v1/namespaces/delete-me/finalize -H "Content-Type: application/json" --data @-

其他回答

解决方案:

使用下面的命令,不做任何更改。这招很管用。

NS=`kubectl get ns |grep Terminating | awk 'NR==1 {print $1}'` && kubectl get namespace "$NS" -o json   | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/"   | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -

享受

在我的案例中,问题是由自定义指标引起的。

要了解导致问题的原因,只需运行以下命令:

kubectl api-resources | grep -i false

这将告诉您是哪些api资源导致了问题。一旦识别出来,就删除它:

kubectl delete apiservice v1beta1.custom.metrics.k8s.io

一旦删除,命名空间就会消失。

调试类似的问题。

有两件重要的事情需要考虑:

1)在从命名空间中删除终结器之前要三思,因为可能有一些资源您不想自动删除,或者至少要了解为了排除故障而删除了哪些资源。

2)像kubectl api-resources——verbs=list这样的命令可能不会提供由外部crds创建的资源。


在我的例子中:

我用kubectl编辑ns <ns-name>,在状态->条件下,我看到我安装的一些外部crds未能被删除,因为它们添加了定义的终结器:

 - lastTransitionTime: "2021-06-14T11:14:47Z"
    message: 'Some content in the namespace has finalizers remaining: finalizer.stackinstall.crossplane.io
      in 1 resource instances, finalizer.stacks.crossplane.io in 1 resource instances'
    reason: SomeFinalizersRemain
    status: "True"
    type: NamespaceFinalizersRemaining

最简单的方法就是复制这个bash脚本

#!/bin/bash

###############################################################################
# Copyright (c) 2018 Red Hat Inc
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
###############################################################################

set -eo pipefail

die() { echo "$*" 1>&2 ; exit 1; }

need() {
    which "$1" &>/dev/null || die "Binary '$1' is missing but required"
}

# checking pre-reqs

need "jq"
need "curl"
need "kubectl"

PROJECT="$1"
shift

test -n "$PROJECT" || die "Missing arguments: kill-ns <namespace>"

kubectl proxy &>/dev/null &
PROXY_PID=$!
killproxy () {
    kill $PROXY_PID
}
trap killproxy EXIT

sleep 1 # give the proxy a second

kubectl get namespace "$PROJECT" -o json | jq 'del(.spec.finalizers[] | select("kubernetes"))' | curl -s -k -H "Content-Type: application/json" -X PUT -o /dev/null --data-binary @- http://localhost:8001/api/v1/namespaces/$PROJECT/finalize && echo "Killed namespace: $PROJECT"

# proxy will get killed by the trap

在deletenamepsace.sh文件中添加上述代码。

然后通过提供命名空间作为参数执行它(linkerd是我想在这里删除的命名空间)

➜ kubectl get namespaces
linkerd           Terminating   11d

➜ sh deletenamepsace.sh linkerd
Killed namespace: linkerd

➜ kubectl get namespaces

上面的建议对我很有效。

老实说,我认为 删除命名空间mynamespace——grace-period=0——force 根本不值得一试。

特别感谢Jens Reimann!我认为这个脚本应该被合并到kubectl命令中。

这里有一个(又一个)解决方案。它使用jq从json中移除finalisers块,并且不需要kubectl代理:

namespaceToDelete=blah

kubectl get namespace "$namespaceToDelete" -o json \
  | jq 'del(.spec.finalizers)' \
  | kubectl replace --raw /api/v1/namespaces/$namespaceToDelete/finalize -f -