I would like to list pods created within 24 hours. I didn't find any kubectl commands or anything to get those. Could anyone please help me with the kubectl command to get only the pods created in last 24 hours.
Asked
Active
Viewed 1,428 times
3 Answers
1
Not the most beautiful solution but this should work (or give you an idea if you want to further improve the command)
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 }'
List all pods names and filter rows with startTime > of one day.

AndD
- 2,281
- 1
- 8
- 22
-
Thanks for the quick response. I have a pod which is created like 7hours before, but its showing only the pods which is created 31 hours ago. Any idea please. – Vivek Subramani Feb 01 '21 at 08:36
-
Sorry, you are right, my idea could never work. I edited the solution, try with this new command? – AndD Feb 01 '21 at 08:46
-
I can get pods which are created in 24 hours. Could you please let me know how to grep for error pods in the same command. – Vivek Subramani Feb 01 '21 at 09:53
-
[Here](https://stackoverflow.com/questions/44379805/kubernetes-has-a-ton-of-pods-in-error-state-that-cant-seem-to-be-cleared) are some ways of finding the `Error` Pods. You can try to incorporate these together with the command above. – Wytrzymały Wiktor Feb 04 '21 at 13:50
1
In order to list all Pods created within the last 24h you can use the below command:
kubectl get pods --sort-by=.metadata.creationTimestamp | awk 'match($5,/^[0-9]h|^[0-9][0-9]h|^[0-9]m|^[0-9][0-9]m|^[0-9]s|^[0-9][0-9]s/) {print $0}'
If you also want to get Pods with errors only than you can use:
kubectl get pods --sort-by=.metadata.creationTimestamp | awk 'match($5,/^[0-9]h|^[0-9][0-9]h|^[0-9]m|^[0-9][0-9]m|^[0-9]s|^[0-9][0-9]s/) {print $0}' | grep -i Error
Or alternatively to only list Pods with the Pending
status:
kubectl get pods --field-selector=status.phase=Pending --sort-by=.metadata.creationTimestamp | awk 'match($5,/^[0-9]h|^[0-9][0-9]h|^[0-9]m|^[0-9][0-9]m|^[0-9]s|^[0-9][0-9]s/) {print $0}'

Wytrzymały Wiktor
- 11,492
- 5
- 29
- 37
1
Previous answers didn't work to me, the regex seems wrong and the field with the actual creationTimestamp is the sixth as per today, so :
kubectl get pods -A --sort-by=.metadata.creationTimestamp | awk 'match($6,/^[0-9]+[sm]{1}|^[0-5][0-9]m|^[1-9]h|^1[0-9]*h|^2[0-4]h/) {print $0}'
It may be also useful to append |grep -v Completed
to filter out Completed pods/jobs.
https://regex101.com/r/l5gLKu/1
Client Version: v1.26.1
Kustomize Version: v4.5.7
Server Version: v1.25.5
Fatality:
alias kpods1day="kubectl get pods -A --sort-by=.metadata.creationTimestamp | awk 'match(\$6,/^[0-9]+[sm]{1}|^[0-5][0-9]m|^[1-9]h|^1[0-9]*h|^2[0-4]h/) {print \$0}' |grep -v \"Completed\|kube-system\""
alias kpods2days="kubectl get pods -A --sort-by=.metadata.creationTimestamp | awk 'match(\$6,/^[0-9]+[sm]{1}|^[0-5][0-9]m|^[1-9]h|^1[0-9]*h|^[2-3][0-9]h|^4[0-8]h/) {print \$0}' |grep -v \"Completed\|kube-system\""

tuxErrante
- 1,274
- 12
- 19