我总共有 2030 个 pvc,我想从中删除 2000 个 pvc,并保留 30 个。
那 30 个 pvc 是最新的,并且只存在不到 2 天.. 所以这就是我不想删除它们的原因。其他所有 2000 个 pvc 都超过 2 天了。
我想创建一个自动运行的脚本来删除超过 2 天的 pvc。
我的 pvc 的一些例子:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-14353-postgresql-0 Bound pvc-1a6 8Gi RWO gp2 2d15h
data-14354-postgresql-0 Bound pvc-2d6 8Gi RWO gp2 16d
data-14358-postgresql-0 Bound pvc-9dc 8Gi RWO gp2 127m
data-14367-postgresql-0 Bound pvc-2eb 8Gi RWO gp2 65h
data-14370-postgresql-0 Bound pvc-90d 8Gi RWO gp2 56d
现在你可以看到我有一个混合的 AGE 标签。
可以使用以下方法删除它们:
kubectl delete pvc
但这会删除所有..我不想删除所有!
- 我可以添加什么命令或 -label for age 来运行删除所有 pvc 的命令,只有不到 2 天的 pvc。
最佳答案
TL:DR;
kubectl get pvc --sort-by=.metadata.creationTimestamp --no-headers | tac | cut -d ' ' -f 1 | sed -n 31,2000p | xargs kubectl delete pvc
让我们分段解释:
kubectl get pvc --sort-by=.metadata.creationTimestamp --no-headers
:这部分按降序列出 PVC,不带标题行
tac
反转输出并产生升序
cut -d ' ' -f 1
获取具有 PVC 名称的第一列
sed -n 31,2000p
打印第 30 行之后的所有 PVC,其中包含 30 个不到 2 天的 PVC
p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.
xargs kubectl delete pvc
删除您的 PVC。
关于amazon-web-services - 按标签 AGE 删除 kubernetes pvc 的自动化脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66000236/