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


当前回答

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命令。 解决这个问题后,我使用脚本卸载集群以消除残留,并重新安装。

其他回答

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

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

then

kubectl delete ns ns_to_be_deleted;

编辑:

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

解决方案:

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

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 -

享受

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

使用实例查看处于“终止”状态的命名空间。 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获取命名空间

调试类似的问题。

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

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