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


当前回答

最简单的方法就是复制这个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命令中。

其他回答

运行kubectl get apiservice

对于上面的命令,您将发现apiservice的Available Flag=Flase。

因此,只需使用kubectl删除apiservice,删除apiservice <apiservice名称>

执行此操作后,具有终止状态的名称空间将消失。

删除下面列出的所有资源:

kubectl delete -n YOURNAMESPACE —-all

使用kubectl delete -n YOURNAMESPACE <resource> <id>,或者(如果你从上面的输出复制粘贴)kubectl delete -n YOURNAMESPACE <resource>/<id>,对于你看到的列出的每个资源。

kubectl delete -n YOURNAMESPACE <resource>/<id1> <resource>/<id2> <resource2>/<id3> <resource2>/<id4> <resource3>/<id5>等。

可能您试图删除资源,但由于部署或复制集资源,它们正在被重新创建,从而阻止名称空间释放依赖的资源并被清理。

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

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

kubectl api-resources | grep -i false

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

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

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

curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json 127.0.0.1:8001/k8s/clusters/c-mzplp/api/v1/namespaces/rook-ceph/finalize

这对我来说很有效,命名空间消失了。

详细的解释可以在https://github.com/rook/rook/blob/master/Documentation/ceph-teardown.md链接中找到。

这发生在我中断kubernetes安装(Armory Minnaker)时。然后我继续删除名称空间并重新安装它。由于终结器的原因,我被困在豆荚的终止状态中。我把命名空间放到了tmp中。Json,从tmp删除终结器。Json文件,并执行curl命令。 解决这个问题后,我使用脚本卸载集群以消除残留,并重新安装。

调试类似的问题。

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

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