首页 > 其他分享 >手把手带你,在K8S集群中删除处于 "terminating" 状态命名空间。

手把手带你,在K8S集群中删除处于 "terminating" 状态命名空间。

时间:2024-04-08 17:23:50浏览次数:30  
标签:rook resource 手把手 ceph instances terminating io K8S type

1. 背景

背景:在Kubernetes集群中,有时侯想要删掉某个长时间不用命名空间以释放资源却发现删不掉,经过反复删除发现想要删除命名空间处在Terminating状态,强制删除也无济于事。作者也遇到这样事情,我又是如何删除掉呢?

#kubectl get ns
NAME                   STATUS        AGE
default                Active        44d
kube-node-lease        Active        44d
kube-public            Active        44d
kube-system            Active        44d
kubernetes-dashboard   Active        44d
rook-ceph              Terminating   15d
2. 使用 "kubectl delete ns rook-ceph" 删除

发现根本删除不掉,一直处于被夯住状态......, 最终不得使用 Ctrl +c 结束。

#kubectl delete ns rook-ceph 
namespace "rook-ceph" deleted
......
3. 使用 "kubectl delete ns rook-ceph --grace-period=0 --force" 强制删除
#kubectl delete ns rook-ceph --grace-period=0 --force
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.
namespace "rook-ceph" force deleted
...............

同样,发现根本删除不掉,一直处于被夯住状态......, 最终不得使用 Ctrl +c 结束。

4. 使用调用api-server接口进行删除 删除成功

4.1 将要删除命名空间使用 “kubectl get ns rook-ceph -o json > rook-ceph.json” 该命令将其转化为json格式并保存在当前目录。

#kubectl get ns
NAME                   STATUS        AGE
default                Active        44d
kube-node-lease        Active        44d
kube-public            Active        44d
kube-system            Active        44d
kubernetes-dashboard   Active        44d
rook-ceph              Terminating   15d
#kubectl get ns rook-ceph -o json > rook-ceph.json
#cat rook-ceph.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rook-ceph\"}}\n"
        },
        "creationTimestamp": "2024-03-23T10:01:56Z",
        "deletionTimestamp": "2024-04-07T13:15:10Z",
        "labels": {
            "kubernetes.io/metadata.name": "rook-ceph"
        },
        "name": "rook-ceph",
        "resourceVersion": "658949",
        "uid": "66c4eb8a-33ac-4cd3-a7b6-89398b739d6b"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2024-04-08T03:30:14Z",
                "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1",
                "reason": "DiscoveryFailed",
                "status": "True",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2024-04-08T02:31:11Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "Some resources are remaining: cephblockpools.ceph.rook.io has 1 resource instances, cephclusters.ceph.rook.io has 1 resource instances, cephfilesystems.ceph.rook.io has 1 resource instances, cephfilesystemsubvolumegroups.ceph.rook.io has 1 resource instances, configmaps. has 1 resource instances, secrets. has 1 resource instances",
                "reason": "SomeResourcesRemain",
                "status": "True",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2024-04-07T13:15:41Z",
                "message": "Some content in the namespace has finalizers remaining: ceph.rook.io/disaster-protection in 2 resource instances, cephblockpool.ceph.rook.io in 1 resource instances, cephcluster.ceph.rook.io in 1 resource instances, cephfilesystem.ceph.rook.io in 1 resource instances, cephfilesystemsubvolumegroup.ceph.rook.io in 1 resource instances",
                "reason": "SomeFinalizersRemain",
                "status": "True",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}
[root@k8s-master01 ~]#

4.2 编辑rook-ceph.json文件,将spec.finalizers设为空数组。

#将rook-ceph.json文件"spec"中"kubernetes"删除后保存。

"spec": {
    "finalizers": [
        "kubernetes"
    ]
#更新后的样子:

"spec": {
        "finalizers": [
        ]
    },

4.3 执行kubectl proxy,启动一个kube api server本地代理,待执行完删除命令后再结束掉。

#kubectl proxy 
Starting to serve on 127.0.0.1:8001

4.4 另开一个窗口执行 "curl -k -H Content-Type: application/json -X PUT --data-binary @rook-ceph.json http://127.0.0.1:8001/api/v1/namespaces/rook-ceph/finalize" 进行接口调用方式删除处于Terminating状态命名空间。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

注意:rook-ceph.json替换为4.1步骤当中你自己的json文件,127.0.0.1.8001替换为4.3步骤你自己kubectl proxy本地代理,rook-ceph替换为你将要删除命名空间名字。

[root@k8s-master01 ~]#curl -k -H Content-Type: application/json -X PUT --data-binary @rook-ceph.json http://127.0.0.1:8001/api/v1/namespaces/rook-ceph/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "rook-ceph",
    "uid": "66c4eb8a-33ac-4cd3-a7b6-89398b739d6b",
    "resourceVersion": "658949",
    "creationTimestamp": "2024-03-23T10:01:56Z",
    "deletionTimestamp": "2024-04-07T13:15:10Z",
    "labels": {
      "kubernetes.io/metadata.name": "rook-ceph"
    },
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rook-ceph\"}}\n"
    },
    "managedFields": [
      {
        "manager": "kubectl-create",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-03-23T10:01:56Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:labels": {
              ".": {},
              "f:kubernetes.io/metadata.name": {}
            }
          }
        }
      },
      {
        "manager": "kubectl-client-side-apply",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-03-23T11:01:37Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:metadata": {
            "f:annotations": {
              ".": {},
              "f:kubectl.kubernetes.io/last-applied-configuration": {}
            }
          }
        }
      },
      {
        "manager": "kube-controller-manager",
        "operation": "Update",
        "apiVersion": "v1",
        "time": "2024-04-08T03:30:14Z",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:status": {
            "f:conditions": {
              ".": {},
              "k:{\"type\":\"NamespaceContentRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionContentFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionDiscoveryFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceDeletionGroupVersionParsingFailure\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              },
              "k:{\"type\":\"NamespaceFinalizersRemaining\"}": {
                ".": {},
                "f:lastTransitionTime": {},
                "f:message": {},
                "f:reason": {},
                "f:status": {},
                "f:type": {}
              }
            }
          }
        },
        "subresource": "status"
      }
    ]
  },
  "spec": {},
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2024-04-08T03:30:14Z",
        "reason": "DiscoveryFailed",
        "message": "Discovery failed for some groups, 1 failing: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1"
      },
      {
        "type": "NamespaceDeletionGroupVersionParsingFailure",
        "status": "False",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2024-04-08T02:31:11Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted, may be waiting on finalization"
      },
      {
        "type": "NamespaceContentRemaining",
        "status": "True",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "SomeResourcesRemain",
        "message": "Some resources are remaining: cephblockpools.ceph.rook.io has 1 resource instances, cephclusters.ceph.rook.io has 1 resource instances, cephfilesystems.ceph.rook.io has 1 resource instances, cephfilesystemsubvolumegroups.ceph.rook.io has 1 resource instances, configmaps. has 1 resource instances, secrets. has 1 resource instances"
      },
      {
        "type": "NamespaceFinalizersRemaining",
        "status": "True",
        "lastTransitionTime": "2024-04-07T13:15:41Z",
        "reason": "SomeFinalizersRemain",
        "message": "Some content in the namespace has finalizers remaining: ceph.rook.io/disaster-protection in 2 resource instances, cephblockpool.ceph.rook.io in 1 resource instances, cephcluster.ceph.rook.io in 1 resource instances, cephfilesystem.ceph.rook.io in 1 resource instances, cephfilesystemsubvolumegroup.ceph.rook.io in 1 resource instances"
      }
    ]
  }
}

4.5 执行完后,你会惊奇发现,处于Terminating状态命名空间就这样被删除了。

#kubectl get ns
NAME                   STATUS   AGE
default                Active   44d
kube-node-lease        Active   44d
kube-public            Active   44d
kube-system            Active   44d
kubernetes-dashboard   Active   44d

5.5 祝大家能够轻松删除处于Terminating状态命名空间。

标签:rook,resource,手把手,ceph,instances,terminating,io,K8S,type
From: https://www.cnblogs.com/huangjiabobk/p/18121787

相关文章

  • Visual Studio 2022插件的安装及使用 - 编程手把手系列文章
          这次开始写手把手编程系列文章,刚写到C#的Dll程序集类库的博文,就发现需要先介绍VisualStudio2022的插件的安装及使用,因为在后面编码的时候会用到这些个插件,所以有必要先对这个内容进行介绍。      其实笔者使用的VisualStudio2022的插件不多,因为有些插件......
  • 计算机毕业设计项目:springboot 智能答疑系统 96852(开题答辩+程序定制+全套文案 )上万套
    毕业论文(设计) 题   目springboot智能答疑系统学   院       XXXXX     专业班级   XXXXX学生姓名       XXXX    指导教师            XXXX          撰写日期:202 年 月 日目 录摘要......
  • 计算机毕业设计项目:新生儿疫苗接种管理系统 87023(开题答辩+程序定制+全套文案 )上万套
    PHP新生儿疫苗接种管理系统系   院XXXX学科门类XXX专   业 XXX班级XXX学   号XXX姓   名XXX指导教师XXX教师职称XXX摘 要新生儿计划免疫是根据危害儿童健康的一些传染病,利用安全有效的疫苗,按照规定的免疫程序进行预防接种,......
  • 数据结构之二叉树 - 超详细的教程,手把手教你认识并运用二叉树
    目录1.树形结构(了解)1.1树形结构的概念(重要)1.2 树的表示形式(了解)1.3 树的应用2.二叉树(重点)2.1概念2.2两种特殊的二叉树2.3二叉树的性质2.4二叉树的存储2.5二叉树的基本操作2.5.1二叉树的遍历1.前序遍历2.中序遍历3.后序遍历4.层序遍历2.5.2......
  • 手把手教你做阅读理解题-初中中考阅读理解解题技巧013-dearMars Project
    PDF格式公众号回复关键字:ZKYD013阅读理解技巧,在帮助读者有效获取和理解文本信息方面发挥着重要作用,熟练掌握如下6个技巧,可快速突破阅读理解1预览文章结构在开始深入阅读之前,快速浏览文章的标题、段落开头和结尾,可以迅速把握文章的主题、大致内容和结构标题通常能概括文章......
  • 单节点部署K8S
    K使用Kubeadm搭建单节点安装前注意进行快照,方便多次安装练习。虚拟机基础信息系统:Ubuntu20.04.6LTS内核:5.15.0-67-generic硬盘:60G内存:12GCPU:4C4U本次安装参考博客地址:https://glory.blog.csdn.net/article/details/120606787安装前准备1.关闭防火墙systemctlstat......
  • K8s技术全景:架构、应用与优化
    本文深入探讨了Kubernetes(K8s)的关键方面,包括其架构、容器编排、网络与存储管理、安全与合规、高可用性、灾难恢复以及监控与日志系统。关注【TechLeadCloud】,分享互联网架构、云服务技术的全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕......
  • 【Java业务需求解决方案】分布式锁应用详情,多种方案选择,轻松解决,手把手操作(非全数
    目录背景:解决方案:分布式锁方案一(不建议,但原理得懂):Redis锁setnx与业务代码处理雏形代码产生问题一:锁释放问题代码改造:锁添加过期时间产生问题二:锁被别的线程误删代码改造:添加setnx锁请求标识防勿删产生问题三:递归容易造成内存溢出代码改造:递归改造while循环产生......
  • k8s.gcr.io、registry.k8s.io镜像下载失败解决方案
    k8s.gcr.io、registry.k8s.io镜像下载失败解决方案问题解决方案使用方法匹配规则问题初始化Kubernetes集群时,很多人都可能遇到以下问题,部分镜像无法访问:Errorresponsefromdaemon:Gethttps://k8s.gcr.io/v2/:net/http:requestcanceledwhilewaitingforcon......
  • 手把手教你构建嵌入式Linux根文件系统
    /bin:此目录下存放着系统需要的可执行文件,一般都是一些命令,比如ls、mv等命令/dev:device的缩写,此目录下的文件都是和设备有关的。在Linux下一切皆文件,即使是硬件设备,也是以文件的形式存在的,比如/dev/ttymxc0就表示串口0/etc:此目录下存放着各种配置文件/lib:library的简称,也就是......