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


当前回答

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

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

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

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

其他回答

我发现删除“终止”名称空间的唯一方法是删除“终结器”部分中的条目。我试过——强制删除它和——grace-period=0没有一个工作,但是,这个方法做到了:

在命令行中显示命名空间的信息:

$ kubectl get namespace your-rogue-namespace -o yaml

这将给你yaml输出,寻找类似于这样的一行:

deletionTimestamp: 2018-09-17T13:00:10Z
  finalizers:
  - Whatever content it might be here...
  labels:

然后只需编辑名称空间配置并删除终结器容器中的项。

$ kubectl edit namespace your-rogue-namespace

这将打开一个编辑器(在我的例子中是VI),浏览我想删除的行并删除它,我按D键两次删除整行。

保存它,退出编辑器,就像变魔术一样。rogue-namespace应该消失了。

为了证实这一点:

$ kubectl get namespace your-rogue-namespace -o yaml

最简单的方法就是复制这个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 1.11+)将显示名称空间中保留的资源:

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

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

我尝试了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

如果命名空间停留在终止状态,而该命名空间中的资源已经被删除,您可以在删除命名空间之前修补该命名空间的终结器:

kubectl patch ns ns_to_be_deleted -p '{"metadata":{"finalizers":null}}';

then

kubectl delete ns ns_to_be_deleted;

编辑:

请先查看@Antonio Gomez Alvarado的回答。根本原因可能是答案中提到的度量服务器。