k8s删除Terminating状态的命名空间

背景: 我们都知道在k8s中namespace有两种常见的状态,即Active和Terminating状态,其中后者一般会比较少见,只有当对应的命名空间下还存在运行的资源,但是该命名空间被删除时才会出现所谓的terminating状态,这种情况下只要等待k8s本身将命名空间下的资源回收后,该命名空间将会被系统自动删除。但是今天遇到命名空间下已没相关资源,但依然无法删除terminating状态的命名空间的情况,特此记录一下.

查看命名空间详情

$ kubectl  get ns  | grep nginx
nginx                  Terminating   1d1h

$ kubectl  get ns nginx -o yaml
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"nginx"}}
  creationTimestamp: "2020-02-19T09:25:26Z"
  deletionTimestamp: "2020-02-20T10:10:43Z"
  name: nginx
  resourceVersion: "7733643"
  selfLink: /api/v1/namespaces/nginx
  uid: 39067ddf-56d7-4cce-afa3-1fbdbd223ab2
spec:
  finalizers:
  - kubernetes
status:
  phase: Terminating

查看该命名空间下的资源

# 查看k8s集群中可以使用命名空间隔离的资源
$ kubectl get all -n nginx
# 发现nginx命名空间下并无资源占用

尝试对命名空间进行删除

# 直接删除命名空间nginx
## 提示删除操作未能完成,说系统会在确定没用没用资源后将会被自动删除
$ kubectl  delete ns nginx
Error from server (Conflict): Operation cannot be fulfilled on namespaces "nginx": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

# 使用强制删除(依然无法删除该命名空间)
$ kubectl  delete ns nginx --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 "nginx": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.

使用原生接口删除

# 获取namespace的详情信息
$ kubectl  get ns nginx  -o json > nginx.json

# 查看napespace定义的json配置
## 删除掉spec部分即可
$ cat nginx.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"nginx\"}}\n"
        },
        "creationTimestamp": "2020-02-19T09:25:26Z",
        "deletionTimestamp": "2020-02-20T10:10:43Z",
        "name": "nginx",
        "resourceVersion": "7733643",
        "selfLink": "/api/v1/namespaces/nginx",
        "uid": "39067ddf-56d7-4cce-afa3-1fbdbd223ab2"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}

# 使用http接口进行删除
$ curl -k -H "Content-Type:application/json" -X PUT --data-binary @nginx.json https://x.x.x.x:6443/api/v1/namespaces/nginx/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "nginx",
    "selfLink": "/api/v1/namespaces/nginx/finalize",
    "uid": "39067ddf-56d7-4cce-afa3-1fbdbd223ab2",
    "resourceVersion": "7733643",
    "creationTimestamp": "2020-02-19T09:25:26Z",
    "deletionTimestamp": "2020-02-20T10:10:43Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"nginx\"}}\n"
    }
  },
  "spec": {

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

# 再次查看namespace发现已经被删除了
$ kubectl  get ns  | grep nginx
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 什么是Pod Pod是Kubernetes中可以创建和部署的最小单位。 Pod为一个或多个container的组合...
    AlienPaul阅读 5,319评论 0 3
  • 在实践之前,必须先学习k8s的几个重要概念,它们是组成k8s集群的基石。 1. Cluster Cluster是计...
    wangfs阅读 937评论 0 0
  • 排错指南 - Pod 本文档介绍 Pod 的异常状态,可能原因和解决办法。 排查 Pod 异常的常用命令如下: 查...
    小孩子的童话2014阅读 7,194评论 0 2
  • ​ 太阳到达黄经150°时是农历二十四节气的处暑。处暑是反映气温变化的一个节气。“处”含有躲藏、终止意思,“处暑”...
    O2TEA阅读 624评论 0 0
  • 冬天适合贮藏。 吃不完的白菜,晒不完的萝卜干,腌不完的咸鱼. 萝卜干全国各地都有,但是每个人的做法也不尽相同。 娘...
    简儿的十年阅读 702评论 19 30

友情链接更多精彩内容