List newly created kubernetes pods in last 24 hours

2/1/2021

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.

-- Vivek Subramani
devops
kubernetes

2 Answers

2/1/2021

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
Source: StackOverflow

2/11/2021

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
Source: StackOverflow