Kubernetes:如何根据年龄/创建时间删除 POD

问题描述 投票:0回答:9

kubernetes 中是否可以根据创建时间或年龄删除 POD?

示例:我想删除所有超过 1 天的 POD。这些 POD 是孤立的,因此不会创建新的 POD。

minikube kubernetes
9个回答
55
投票

此命令将删除所有超过一天的 POD :

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d 'yesterday' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | xargs --no-run-if-empty kubectl delete pod

此命令将删除所有超过 4 小时的 POD :

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d'now-4 hours' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | xargs --no-run-if-empty kubectl delete pod

29
投票

我们可以使用

awk
直接在
[0-9]+d
($5,第 5 列)列上执行正则表达式
AGE
,然后打印相应的
NAME
($1,第一列)列

kubectl delete pod $(kubectl get pod | awk 'match($5,/[0-9]+d/) {print $1}')

首先测试看看有什么匹配:

kubectl get pod | awk 'match($5,/[0-9]+d/) {print $0}'

$0
表示所有列


3
投票

您可以添加一个 livenessprobe 来跟踪 pod 的存活时间,并在超过一定时间时将其杀死。或者您可以安排一个 CronJob


3
投票

已修改为可在 mac 上运行:

# try the gnu versions: gxargs
brew install findutils coreutils


kubectl get pods -o go-template -n gui2 --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(gdate -d '21 days ago' -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | gxargs --no-run-if-empty kubectl delete pod

3
投票

这是使用

kubectl
jq
xargs
的替代答案。

它可以在 Mac 上本地运行,但我也在运行 debian-slim 的 Kubernetes cronjob 中使用它,所以我认为它可以在 Mac 和 Linux 上运行。

时间以秒为单位设置(此处 86400 秒表示 1 天)。

kubectl get pod -o json | jq -r --argjson timestamp 86400 '.items[] | select (.metadata.creationTimestamp | sub("\\..*";"Z") | sub("\\s";"T") | fromdate < now - $timestamp).metadata.name' | xargs -r -L1 kubectl delete pod ;

1
投票

这是上面 dansl1982 的答案的一个变体,它将删除所有命名空间中具有特定标签的所有 pod。

kubectl get pods -A -l app=my-api -o go-template --template '{{range .items}}{{.metadata.namespace}} {{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$3 <= "'$(date -d'now-4 hours' -Ins --utc | sed 's/+0000/Z/')'" { print $1 " " $2 }' | xargs -L 1 --no-run-if-empty kubectl delete pod -n $1 $2

1
投票

这里的答案本身并不完整,无法满足我的需求,因此我采取了一些答案(并投票赞成),然后将它们混合在一起,从各个命名空间中删除超过一天的已完成 Pod。

这对我有用:

kubectl get pod --all-namespaces --field-selector=status.phase==Succeeded \
 | awk 'match($6,/[0-9]+d/) {print $1, $2}' \
 | xargs -L 1 --verbose --no-run-if-empty kubectl delete pod -n $1 $2
  • kubectl
    在所有命名空间中查找已完成的 Pod,
  • awk
    过滤一天或更长时间前完成的 Pod(任何以
    d
    结尾的年龄字符串),
  • xargs
    针对每个 pod 运行(和输出)
    kubectl delete
    ,指定 pod 的命名空间。

0
投票

这是另一个答案,通过在

get pods
时显示自定义字段并按
.metadata.creationTimestamp

进行过滤
kubectl delete pod -n foo $(kubectl get pods -n foo --field-selector=status.phase=Failed -o=custom-columns=NAME:.metadata.name,AGE:.metadata.creationTimestamp --no-headers=true --sort-by=.metadata.creationTimestamp | awk '$2 <= "'$(gdate -d '10 days ago' -Ins | sed 's/+0000/Z/')'" { print $1 }')

基于上述命令

-o=custom-columns=NAME:
已用于自定义列。


0
投票

对于 Powershell 用户,这会删除所有超过一天的 pod:

     kubectl get pods -o json | ConvertFrom-Json | ForEach-Object {
         $_.items | Where-Object {
             $timestamp = [System.DateTime]::ParseExact($_.metadata.creationTimestamp, "yyyy-MM-ddTHH:mm:ssZ", [System.Globalization.CultureInfo]::InvariantCulture)
             $timestamp -le [System.DateTime]::UtcNow.AddDays(-1)
         } | ForEach-Object {
             kubectl delete pod $_.metadata.name
         }
     }
© www.soinside.com 2019 - 2024. All rights reserved.