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


当前回答

这是由于名称空间控制器无法删除名称空间中仍然存在的资源。

这个命令(使用kubectl 1.11+)将显示名称空间中保留的资源:

kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace>

一旦找到并解析并删除这些名称空间,该名称空间就会被清理

其他回答

请尝试以下命令:

kubectl patch ns <your_namespace> -p '{"metadata":{"finalizers":null}}'

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

1. 使用Curl命令

问题提及:https://amalgjose.com/2021/07/28/how-to-manually-delete-a-kubernetes-namespace-stuck-in-terminating-state/

export NAMESPACE=<specifice-namespace>
kubectl get namespace $NAMESPACE -o json > tempfile.json

编辑JSON文件并从spec.finalizers中删除所有值

保存它,然后在单独的选项卡上应用此命令 (必须在单独的标签打开)

kubectl proxy

并在同一选项卡上运行此命令:

curl -k -H "Content-Type: application/json" -X PUT --data-binary @tempfile.json http://127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize

检查命名空间是否删除了终止命名空间

kubectl get namespaces

2. 使用Kubectl命令

提到的问题:https://aws.amazon.com/premiumsupport/knowledge-center/eks- terminated-namespaces/

以如下格式保存JSON文件:

export NAMESPACE=<specifice-namespace>
kubectl get namespace $NAMESPACE -o json > tempfile.json

编辑JSON文件并从spec.finalizers中删除所有值 要应用更改,运行如下命令:

kubectl replace --raw "/api/v1/namespaces/$NAMESPACE/finalize" -f ./tempfile.json

验证终止命名空间已被删除:

kubectl get namespaces

对于任何想为Kubernetes的新版本寻找一些命令的人来说,这对我很有帮助。

NAMESPACE=mynamespace
kubectl get namespace $NAMESPACE -o json | sed 's/"kubernetes"//' | kubectl replace --raw "/api/v1/namespaces/$NAMESPACE/finalize" -f -

在Kubernetes v1.24.1中测试

我尝试了3-5个选项来删除ns,但只有这一个对我有用。

这个sh文件将删除所有处于终止状态的名称空间

$ 我们 force-delete-namespaces.sh

$chmod +x force-delete-namespaces.sh

美元。/ force-delete-namespaces.sh

#!/usr/bin/env bash

set -e
set -o pipefail

kubectl proxy &
proxy_pid="$!"
trap 'kill "$proxy_pid"' EXIT

for ns in $(kubectl get namespace --field-selector=status.phase=Terminating --output=jsonpath="{.items[*].metadata.name}"); do
    echo "Removing finalizers from namespace '$ns'..."
    curl -H "Content-Type: application/json" -X PUT "127.0.0.1:8001/api/v1/namespaces/$ns/finalize" -d @- \
        < <(kubectl get namespace "$ns" --output=json | jq '.spec = { "finalizers": [] }')

    echo
    echo "Force-deleting namespace '$ns'..."
    kubectl delete namespace "$ns" --force --grace-period=0 --ignore-not-found=true
done