k8s删除Terminating状态的命名空间

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

在项目的k8s中安装了kube-prometheus,但是根据用处不是太大,而且阿里云资源不多,就准备卸载了,结果卡住了

image-20210726201843298

查看napespace定义的json配置

删除掉spec部分即可

[root@k8s-master ~]# kubectl get ns monitoring  -o json > monitoring.json
[root@k8s-master ~]# cat monitoring.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
        },
        "creationTimestamp": "2021-07-19T03:13:20Z",
        "deletionTimestamp": "2021-07-26T10:23:58Z",
        "name": "monitoring",
        "resourceVersion": "2912094",
        "selfLink": "/api/v1/namespaces/monitoring",
        "uid": "88cfc047-3736-42bb-ab51-db07fec7c6ef"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2021-07-26T10:24:09Z",
                "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",
                "reason": "DiscoveryFailed",
                "status": "True",
                "type": "NamespaceDeletionDiscoveryFailure"
            },
            {
                "lastTransitionTime": "2021-07-26T10:24:04Z",
                "message": "All legacy kube types successfully parsed",
                "reason": "ParsedGroupVersions",
                "status": "False",
                "type": "NamespaceDeletionGroupVersionParsingFailure"
            },
            {
                "lastTransitionTime": "2021-07-26T10:24:04Z",
                "message": "All content successfully deleted, may be waiting on finalization",
                "reason": "ContentDeleted",
                "status": "False",
                "type": "NamespaceDeletionContentFailure"
            },
            {
                "lastTransitionTime": "2021-07-26T10:24:20Z",
                "message": "All content successfully removed",
                "reason": "ContentRemoved",
                "status": "False",
                "type": "NamespaceContentRemaining"
            },
            {
                "lastTransitionTime": "2021-07-26T10:24:04Z",
                "message": "All content-preserving finalizers finished",
                "reason": "ContentHasNoFinalizers",
                "status": "False",
                "type": "NamespaceFinalizersRemaining"
            }
        ],
        "phase": "Terminating"
    }
}

删除下面的部分

image-20210726202233999

导出k8s的密钥

导出K8s访问密钥
echo $(kubectl config view --raw -oyaml | grep client-cert  |cut -d ' ' -f 6) |base64 -d > /tmp/client.pem
echo $(kubectl config view --raw -oyaml | grep client-key-data  |cut -d ' ' -f 6 ) |base64 -d > /tmp/client-key.pem
echo $(kubectl config view --raw -oyaml | grep certificate-authority-data  |cut -d ' ' -f 6  ) |base64 -d > /tmp/ca.pem

使用http接口进行删除

[root@k8s-master ~]# curl --cert /tmp/client.pem --key /tmp/client-key.pem --cacert /tmp/ca.pem -H "Content-Type: application/json" -X PUT --data-binary @/root/monitoring.json https://172.16.8.43:6443/api/v1/namespaces/monitoring/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "monitoring",
    "selfLink": "/api/v1/namespaces/monitoring/finalize",
    "uid": "88cfc047-3736-42bb-ab51-db07fec7c6ef",
    "resourceVersion": "2912094",
    "creationTimestamp": "2021-07-19T03:13:20Z",
    "deletionTimestamp": "2021-07-26T10:23:58Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"monitoring\"}}\n"
    }
  },
  "spec": {
 
  },
  "status": {
    "phase": "Terminating",
    "conditions": [
      {
        "type": "NamespaceDeletionDiscoveryFailure",
        "status": "True",
        "lastTransitionTime": "2021-07-26T10:24:09Z",
        "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": "2021-07-26T10:24:04Z",
        "reason": "ParsedGroupVersions",
        "message": "All legacy kube types successfully parsed"
      },
      {
        "type": "NamespaceDeletionContentFailure",
        "status": "False",
        "lastTransitionTime": "2021-07-26T10:24:04Z",
        "reason": "ContentDeleted",
        "message": "All content successfully deleted, may be waiting on finalization"
      },
      {
        "type": "NamespaceContentRemaining",
        "status": "False",
        "lastTransitionTime": "2021-07-26T10:24:20Z",
        "reason": "ContentRemoved",
        "message": "All content successfully removed"
      },
      {
        "type": "NamespaceFinalizersRemaining",
        "status": "False",
        "lastTransitionTime": "2021-07-26T10:24:04Z",
        "reason": "ContentHasNoFinalizers",
        "message": "All content-preserving finalizers finished"
      }
    ]
  }

再次查看namespace发现已经被删除了

[root@k8s-master ~]#kubectl get ns
NAME              STATUS   AGE
default           Active   13d
dev               Active   13d
framework         Active   11d
ingress-nginx     Active   7d21h
kube-node-lease   Active   13d
kube-public       Active   13d
kube-system       Active   13d
kuboard           Active   13d
[root@k8s-master ~]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Kubernetes 集群中删除所有处于 Terminating 状态的 Pod,可以使用以下命令: ``` kubectl delete pod --field-selector=status.phase=Terminating ``` 请注意,这将删除所有处于 Terminating 状态的 Pod,并且无法撤消。因此,应谨慎使用此命令。 ### 回答2: KubernetesK8s)是一种用于自动化部署、扩展和管理容器化应用程序的开源平台。要删除所有处于Terminating(终止)状态的Pod,可以使用以下语句: ``` kubectl get pods --field-selector=status.phase=Terminating -o json | kubectl delete -f - ``` 该语句使用了kubectl命令,并结合了一些参数和标志来执行特定的操作。 - `kubectl get pods`:获取当前集群中的所有Pod。 - `--field-selector=status.phase=Terminating`:使用字段过滤器,仅返回状态Terminating的Pod。 - `-o json`:输出格式为JSON,以便用于后续的操作。 - `|`:管道命令符,将前一个命令的输出作为后一个命令的输入。 - `kubectl delete -f -`:从标准输入中读取JSON文件,并根据其内容删除对应的资源。 这样,执行该语句后,Kubernetes将会删除所有处于Terminating状态的Pod。 需要注意的是,使用该语句删除Pod时,确保你有足够的权限执行操作,并且在执行前仔细确认一次,以免误删除正在使用或重要的Pod。同时,建议根据具体情况调整语句,以确保只删除目标Pod。 ### 回答3: 要删除所有Terminating状态的pod,可以使用以下命令: kubectl delete pod --all --grace-period=0 --force 这个命令中的参数含义如下: - "kubectl delete pod":删除pod的命令 - "--all":指定要删除所有的pod - "--grace-period=0":设置pod的终止期为0,即立即终止 - "--force":强制删除pod,即使pod处于Terminating状态 通过使用这个命令,所有处于Terminating状态的pod都会被立即删除,不会等待额外的终止期。 需要注意的是,执行此命令可能会导致数据丢失或应用程序中断,因此在执行之前请谨慎确认。另外,删除操作是不可逆的,无法恢复被删除的pod,所以请确保操作的可行性和必要性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值