解决k8s中NameSpace无法删除的问题 | Eddie'Blog
解决k8s中NameSpace无法删除的问题

解决k8s中NameSpace无法删除的问题

eddie 672 2020-07-10

目录

在1.14.x之后的版本可能会出现删除命名空间状态Terminating失败的情况。

例子

现在需求删除 dev-java 的命名空间

[root@eddie /]# kubectl get ns
NAME              STATUS        AGE
arms-prom         Active        18h
default           Active        18h
dev-java          Terminating   17h
dev-tools         Active        17h
ingress-nginx     Terminating   12h
kube-node-lease   Active        18h
kube-public       Active        18h
kube-system       Active        18h

方法1

  1. k8s常规性删除命名空间
[root@eddie /]# kubectl delete ns dev-java
Error from server (Conflict): Operation cannot be fulfilled on namespaces "dev-java": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

[root@eddie /]# kubectl delete ns dev-java --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
Error from server (Conflict): Operation cannot be fulfilled on namespaces "dev-java": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

使用以上两种命令均无法成功删除命名空间,只会使命名空间的状态为Terminating状态

方法2 (可行)

  1. 获取 namespace 的json格式文件
kubectl get namespace dev-java -o json > devtesting.json 
  1. 使用CRT工具新开一个窗口,运行kubectl代理
kubectl proxy --port=8080
  1. 查看代理是否成功
[root@eddie /]# curl http://localhost:8080/api/
{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "172.18.110.114:6443"
    }
  ]
  1. 修改 devtesting.json 文件
    "spec": {
        "finalizers": [
	    "kubernetes"  # 删除这行,保存退出即可
        ]
    },
  1. 使用http接口方式进行删除操作,留意 @*.json的文件是你自己创建的.
[root@eddie /]# curl -k -H "Content-Type: application/json" -X PUT --data-binary @devtesting.json http://127.0.0.1:8080/api/v1/namespaces/dev-java/finalize

{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "dev-html",
    "selfLink": "/api/v1/namespaces/dev-html/finalize",
    "uid": "c4988fad-cdca-42fe-9600-0751c655388a",
    "resourceVersion": "10366",
    "creationTimestamp": "2020-07-09T07:29:25Z",
    "deletionTimestamp": "2020-07-09T07:29:42Z"
  },
  "spec": {
    
  },
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2020-07-09T07:29:47Z",
        "reason": "DiscoveryFailed",
        "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request"
      },
      {
        "type": "NamespaceDeletionGroupVersionParsingFailure",
        "status": "False",
        "lastTransitionTime": "2020-07-09T07:29:47Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2020-07-09T07:29:47Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted"
      }
    ]
  }

# Kubernetes