简体   繁体   English

如何删除 Kubernetes 部署中的所有磁盘?

[英]How do I delete all the disks in a deployment in Kubernetes?

I want to delete all the Persistent Volume Claims attached to a customized deployment.我想删除附加到自定义部署的所有持久卷声明。 At first, I delete the deployment itself that deletes all the pods but the disks aren't deleted.首先,我删除了删除所有 Pod 但磁盘没有删除的部署本身。

I know the command to delete pvcs but that involves fetching all of them using a command[1] and then manually copying-pasting[2] the ones that belong to the deployment set.我知道删除 pvc 的命令,但这涉及使用命令 [1] 获取所有 pvc,然后手动复制粘贴 [2] 属于部署集的那些。

If I want to do this programmatically, which API of Kubernetes Python client or the cli command do I use?如果我想以编程方式执行此操作,我应该使用 Kubernetes Python 客户端的哪个 API 或 cli 命令?

I am using GKE.我正在使用 GKE。

I tried reading the Python client documentation but could not deduce how to do this task.我尝试阅读Python 客户端文档,但无法推断出如何执行此任务。

[1] kubectl --namespace=<my_namespace> --context=<my_cluster> get pvc [1] kubectl --namespace=<my_namespace> --context=<my_cluster> get pvc

[2] kubectl --namespace=<my_namespace> --context=<cluster_name> delete pvc data-avirals-july19-1-devel-0 data-avirals-july19-1-devel-1 [2] kubectl --namespace=<my_namespace> --context=<cluster_name> delete pvc data-avirals-july19-1-devel-0 data-avirals-july19-1-devel-1

There might be some shorter way, however this one would filter all the pvc associated with the deployment name MY_DEPLOYMENT_NAME .可能有一些更短的方法,但是这个方法会过滤与部署名称MY_DEPLOYMENT_NAME关联的所有 pvc。 Other pvc (not associated with the same deployment) will not be touched.其他pvc (不与同一部署关联)不会被触及。

for volume in $(kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}');
    do
      kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}' ;
      # kubectl delete pvc "$(kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}')"
done

Note:笔记:

  1. uncomment the command kubectl delete to really delete the pvc.取消注释kubectl delete命令以真正删除 pvc。 Its commented to protect users from accidently deleting.它的评论是为了保护用户不被意外删除。
  2. bash is required to make this loop work.需要bash才能使此循环工作。

Explanation:解释:
Following command would list down the volumes used by a deployment called MY_DEPLOYMENT_NAME以下命令将列出名为MY_DEPLOYMENT_NAME的部署所使用的卷

kubectl get deployments MY_DEPLOYMENT_NAME -o jsonpath='{.spec.template.spec.volumes[*].name}'

Now,we will loop over these volumes, to find what is the claim used by these volumes.现在,我们将遍历这些卷,以找出这些卷使用的claim是什么。 This is done my checking claimRef section.这是我检查claimRef部分完成的。 This is done by:这是通过以下方式完成的:

kubectl get pv $volume -o jsonpath='{.spec.claimRef.name}{"\n"}' 

You can delete all pvc at once with the following command:您可以使用以下命令一次删除所有 pvc:

kubectl --namespace=<my_namespace> delete pvc --all 

The PVs should be deleted after that but it might take some time.之后应该删除 PV,但这可能需要一些时间。 Also, be very careful with this command and make sure you are deleting what you intend to.此外,使用此命令要非常小心,并确保您正在删除您打算删除的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM