K8S删除卡在Terminating状态的namespace

1 背景

由于一些未知原因,一些namespace在删除时会一直处于Terminating状态,无法彻底删除

[root@master ~]# kubectl get ns crt-test
NAME       STATUS        AGE
crt-test   Terminating   71d

2 处理方式

2.1 删除对应资源的finalizers字段

finalizers字段属于K8S的GC垃圾收集器,是一种删除拦截机制,能够让控制器实现异步的删除前回调,从而在删除目标资源前清理相关资源或基础设施。当资源被删除时,API server将为该对象设置metadata.deletionTimestamp,然后控制器会试图满足资源的finalizers条件。 每当一个finalizer条件被满足时,控制器就会从资源的finalizers字段中删除该键。 直到finalizers字段为空时,对象才会被自动删除。

由于某些异常原因,finalizers条件无法满足,该资源就会一直处于Terminating状态,这个时候我们就可以手动edit这个资源,删除对应的finalizer条件,

kubectl edit ns crt-test

2.2 强制删除

在实际操作时,有时候edit资源删除finalizer无法生效,这时候可以试试调用api直接删除etcd里的数据,但是这种方式会出现无主资源,也就是尽管namespace本身被删除,但是底下的资源依然存在,

NAMESPACE=your-namespace
kubectl proxy &
kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize

实际也是去删除finalize,但是采用更直接的方式,

2.3 删除namespace下所有资源

有时候如果namespace里还有其他资源,那么该namespace也是无法被删除的,那我们就可以通过手动删除该namespace下的资源。

kubectl delete all --all -n crt-test

但是该方式只会删除部分资源,这里all对应的并不是所有资源,只包含了pod,service,daemonset,deployment,replicaset,statefulset,job,cronjobs,因此对于一些CRD是无法删除的。

这时候我们可以使用kubectl api-resources来查看该集群下的所有资源,然后在该namespace下查一遍,

kubectl api-resources --verbs=list --namespaced -o name   | xargs -n 1 kubectl get --show-kind --ignore-not-found -n crt-test

能查,自然能删,

kubectl api-resources --verbs=delete --namespaced -o name   | xargs -n 1 kubectl delete --all -n crt-test

当然,删除资源的时候也有可能会卡主,可以继续尝试上面提到的删除finalizer方式。


参考文档:

  1. https://kubernetes.io/docs/concepts/overview/working-with-objects/finalizers/
  2. https://www.studytonight.com/post/how-to-list-all-resources-in-a-kubernetes-namespace
  3. https://stackoverflow.com/questions/52369247/namespace-stuck-as-terminating-how-i-removed-it
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
从 Windows 3.0 SDK 发掘的资源,英文原版 + HTML版本 Microsoft Codeview and Utilities User's Guide Microsoft(R) CodeView(R) and Utilities User's Guide Version 2.3 for MS(R) OS/2 and MS-DOS(R) Operating Systems MICROSOFT CORPORATION Information in this document is subject to change without notice and does not represent a commitment on the part of Microsoft Corporation. The software described in this document is furnished under a license agreement or nondisclosure agreement. The software may be used or copied only in accordance with the terms of the agreement. It is against the law to copy the software on any medium except as specifically allowed in the license or nondisclosure agreement. No part of this manual may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying and recording, for any purpose without the express written permission of Microsoft. (c)Copyright Microsoft Corporation, 1987, 1989. All rights reserved. Simultaneously published in the U.S. and Canada. Printed and bound in the United States of America. Microsoft, MS, MS-DOS, XENIX, and CodeView are registered trademarks of Microsoft Corporation. AT&T is a registered trademark of American Telephone and Telegraph Company. Eagle is a registered trademark of Eagle Computer, Inc. IBM is a registered trademark of International Business Machines Corporation. Intel is a registered trademark of Intel Corporation. Lotus is a registered trademark of Lotus Development Corporation. Tandy is a registered trademark of Tandy Corporation. Document No. LN0801A-500-R00-0889 Part No. 07824 10 9 8 7 6 5 4 3 2 1 %@CR:MCVTOC00@% Table of Contents Introduction New Features of the CodeView(R) Debugger About this Manual Document Conventions Part 1 The CodeView Debugger Chapter 1 Getting Started 1.1 Restrictions 1.2 The CodeView Environment 1.3 Preparing Programs for the CodeView Debugger 1.3.1 Programming Considerations 1.3.2 CodeView Compile Options 1.3.3 CodeView Link
### 回答1: 在 Kubernetes 集群中删除所有处于 Terminating 状态的 Pod,可以使用以下命令: ``` kubectl delete pod --field-selector=status.phase=Terminating ``` 请注意,这将删除所有处于 Terminating 状态的 Pod,并且无法撤消。因此,应谨慎使用此命令。 ### 回答2: Kubernetes(K8s)是一种用于自动化部署、扩展和管理容器化应用程序的开源平台。要删除所有处于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、付费专栏及课程。

余额充值