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


当前回答

我有类似的问题与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

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

其他回答

将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,并再次尝试上述步骤。

编辑: 不建议删除终结器。 正确的做法是:

删除命名空间下的所有资源。

Github问题链接

我通常的工作空间是一个小的k8s集群,我经常破坏并重新构建它,这就是为什么删除终结器方法适合我。

原来的答案:我经常遇到同样的问题。

这就是我的工作

kubectl get ns your-namespace -o json > ns-without-finalizers.json

编辑ns-without-finalizers.json。将所有终结器替换为空数组。

运行kubectl代理(通常在另一个终端上运行)

然后curl这个命令

curl -X PUT http://localhost:8001/api/v1/namespaces/your-namespace/finalize -H "Content-Type: application/json" --data @ns-without-finalizers.json

我写了一个简单的脚本,删除您卡住的名称空间基于@Shreyangi Saxena的解决方案。

cat > delete_stuck_ns.sh << "EOF"
#!/usr/bin/env bash

function delete_namespace () {
    echo "Deleting namespace $1"
    kubectl get namespace $1 -o json > tmp.json
    sed -i 's/"kubernetes"//g' tmp.json
    kubectl replace --raw "/api/v1/namespaces/$1/finalize" -f ./tmp.json
    rm ./tmp.json
}

TERMINATING_NS=$(kubectl get ns | awk '$2=="Terminating" {print $1}')

for ns in $TERMINATING_NS
do
    delete_namespace $ns
done
EOF

chmod +x delete_stuck_ns.sh

该脚本可以检测到所有处于终止状态的命名空间,并将其删除。


PS:

这可能在MacOS中不起作用,因为MacOS中的本机sed与GNU sed不兼容。 你可能需要在MacOS中安装GNU sed,请参考这个答案。 请确认您可以通过命令kubectl访问您的kubernetes集群。 已在kubernetes v1.15.3版本上测试


更新

我找到了一个更简单的解决办法:

kubectl patch RESOURCE NAME -p '{"metadata":{"finalizers":[]}}' --type=merge

使用实例查看处于“终止”状态的命名空间。 Kubectl获取命名空间 选择一个终止名称空间并查看该名称空间的内容以找出终止器。执行如下命令: Kubectl获取命名空间-o yaml 您的YAML内容可能类似于以下输出:

        apiVersion: v1
        kind: Namespace
        metadata:
           creationTimestamp: 2019-12-25T17:38:32Z
           deletionTimestamp: 2019-12-25T17:51:34Z
           name: <terminating-namespace>
           resourceVersion: "4779875"
           selfLink: /api/v1/namespaces/<terminating-namespace>
           uid: ******-****-****-****-fa1dfgerz5
         spec:
           finalizers:
           - kubernetes
         status:
           phase: Terminating

运行如下命令创建一个临时JSON文件: 获取命名空间-o json >tmp.json 编辑tmp。json文件。从finalizers字段中删除kubernetes值并保存文件。输出如下:

    {
        "apiVersion": "v1",
        "kind": "Namespace",
        "metadata": {
            "creationTimestamp": "2018-11-19T18:48:30Z",
            "deletionTimestamp": "2018-11-19T18:59:36Z",
            "name": "<terminating-namespace>",
            "resourceVersion": "1385077",
            "selfLink": "/api/v1/namespaces/<terminating-namespace>",
            "uid": "b50c9ea4-ec2b-11e8-a0be-fa163eeb47a5"
        },
        "spec": {
        },

        "status": {
            "phase": "Terminating"
        }
    }

使用实例设置临时代理IP和端口。请确保您的终端窗口一直打开,直到您删除卡住的命名空间: kubectl代理 您的代理IP和端口可能类似于以下输出: 开始在127.0.0.1:8001上播放 在一个新的终端窗口中,使用临时代理IP和端口进行API调用:

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

你的输出是这样的:

    {
       "kind": "Namespace",
       "apiVersion": "v1",
       "metadata": {
         "name": "<terminating-namespace>",
         "selfLink": "/api/v1/namespaces/<terminating-namespace>/finalize",
         "uid": "b50c9ea4-ec2b-11e8-a0be-fa163eeb47a5",
         "resourceVersion": "1602981",
         "creationTimestamp": "2018-11-19T18:48:30Z",
         "deletionTimestamp": "2018-11-19T18:59:36Z"
       },
       "spec": {

       },
       "status": {
         "phase": "Terminating"
       }
    }

终结器形参被移除。现在,验证终止的命名空间已被删除,运行以下命令: Kubectl获取命名空间

强制删除名称空间或删除终结器绝对不是正确的方法,因为这可能会使资源注册到一个不存在的名称空间。

这通常很好,但有一天您将无法创建资源,因为它仍然在某个地方徘徊。

即将发布的Kubernetes 1.16版本应该会对名称空间终结器有更多的了解,目前我将依赖于标识策略。 一个很酷的脚本试图自动化这些是:https://github.com/thyarles/knsk

然而,它可以跨所有名称空间工作,这可能是危险的。它的解决方案是:https://github.com/kubernetes/kubernetes/issues/60807#issuecomment-524772920

博士tl;

检查是否有apiservice不可用,因此没有服务它的资源:kubectl get apiservice|grep False 通过kubectl api-resources——verbs=list——namespaced -o name | xargs -n 1 kubectl get -n $your-ns-to-delete查找所有仍然存在的资源

(来源:https://github.com/kubernetes/kubernetes/issues/60807 # issuecomment - 524772920)