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


当前回答

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

其他回答

解决方案:

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

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 -

享受

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

将ambassador替换为您的名称空间

检查名称空间是否卡住

kubectl get ns ambassador

NAME         STATUS        AGE
ambassador   Terminating   110d

这个卡了很久了

打开管理终端/cmd提示符或powershell并运行

kubectl代理

这将启动本地web服务器

打开另一个终端并运行

kubectl get ns ambassador -o json >tmp.json

编辑tmp文件。Json使用vi或nano

从这个

{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
    "annotations": {
        "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"ambassador\"}}\n"
    },
    "creationTimestamp": "2021-01-07T18:23:28Z",
    "deletionTimestamp": "2021-04-28T06:43:41Z",
    "name": "ambassador",
    "resourceVersion": "14572382",
    "selfLink": "/api/v1/namespaces/ambassador",
    "uid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"spec": {
    "finalizers": [
        "kubernetes"
    ]
},
"status": {
    "conditions": [
        {
            "lastTransitionTime": "2021-04-28T06:43:46Z",
            "message": "Discovery failed for some groups, 3 failing: unable to retrieve the complete list of server APIs: compose.docker.com/v1alpha3: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1alpha3?timeout=32s\\\": Post https://0.0.0.1:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 0.0.0.0:53284-\u0026gt;0.0.0.0:443: write: broken pipe\") has prevented the request from succeeding, compose.docker.com/v1beta1: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1beta1?timeout=32s\\\": Post https://10.96.0.1:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 0.0.0.0:5284-\u0026gt;10.96.0.1:443: write: broken pipe\") has prevented the request from succeeding, compose.docker.com/v1beta2: an error on the server (\"Internal Server Error: \\\"/apis/compose.docker.com/v1beta2?timeout=32s\\\": Post https://0.0.0.0:443/apis/authorization.k8s.io/v1beta1/subjectaccessreviews: write tcp 1.1.1.1:2284-\u0026gt;0.0.0.0:443: write: broken pipe\") has prevented the request from succeeding",
            "reason": "DiscoveryFailed",
            "status": "True",
            "type": "NamespaceDeletionDiscoveryFailure"
        },
        {
            "lastTransitionTime": "2021-04-28T06:43:49Z",
            "message": "All legacy kube types successfully parsed",
            "reason": "ParsedGroupVersions",
            "status": "False",
            "type": "NamespaceDeletionGroupVersionParsingFailure"
        },
        {
            "lastTransitionTime": "2021-04-28T06:43:49Z",
            "message": "All content successfully deleted",
            "reason": "ContentDeleted",
            "status": "False",
            "type": "NamespaceDeletionContentFailure"
        }
    ],
    "phase": "Terminating"
}

}

to

    {
  "apiVersion": "v1",
  "kind": "Namespace",
  "metadata": {
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"ambassador\"}}\n"
    },
    "creationTimestamp": "2021-01-07T18:23:28Z",
    "deletionTimestamp": "2021-04-28T06:43:41Z",
    "name": "ambassador",
    "resourceVersion": "14572382",
    "selfLink": "/api/v1/namespaces/ambassador",
    "uid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  },
  "spec": {
    "finalizers": []
  }
}

通过删除终结器中的状态和kubernetes

现在使用该命令并用您的名称空间替换ambassador

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

运行之前,您将看到另一个json

然后运行命令

 kubectl get ns ambassador
Error from server (NotFound): namespaces "ambassador" not found

如果它仍然显示终止或任何其他错误,请确保以适当的方式格式化json,并再次尝试上述步骤。

我有类似的问题与metric -server,执行kubectl获得namespace < namespace - here > -o json后,我发现这条消息:

"某些组的发现失败,1失败:无法检索 服务器api的完整列表:Io /v1beta1:错误 服务器("内部服务器错误: \ " / api / metrics.k8s.io / v1beta1吗?timeout=32s\":未经授权")已经生效 阻止请求成功

我在这个集群上安装了旧版本的metrics-server。我只是删除了这些资源。因为它是安装在:

kubectl apply -f metrics-server-0.3.7/deploy/1.8+/ --dry-run -o yaml | kubectl apply -f -

这个命令删除了所有这些:

kubectl apply -f metrics-server-0.3.7/deploy/1.8+/ --dry-run -o yaml | kubectl delete -f -

最后,该名称空间在几秒钟后消失。

我假设,如果你有一个最新的版本,你可以删除:

1. YAML安装(此处):

kubectl delete -f https://github.com/kubernetes-sigs/metrics-server/releases/download/<VERSION-HERE>/components.yaml

2. 舵图安装(此处)

helm uninstall metrics-server

不要忘记用正确的版本重新安装它。

对于任何想为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中测试