I need to get error pods above five days. The below commands is working well for the pods which is below five days. Could anyone please let me know on how to get pods which is above 5 days only. It should not show the error pods which is below 5 days.
kubectl get pods --all-namespaces --sort-by=.metadata.creationTimestamp | awk 'match($6,/^[1-4]d|^[1-900]+h|^[1-900]+m|^[1-900]+s/) {print $0}' | grep "Error"
I got two options for you:
kubectl get pods --sort-by=.metadata.creationTimestamp | awk 'match($5,/[6-9]d|[0-9][0-9]d|[0-9][0-9][0-9]d/) {print $0}' | grep -i error
or
kubectl get pods --field-selector=status.phase=Pending --sort-by=.metadata.creationTimestamp | awk 'match($5,/[6-9]d|[0-9][0-9]d|[0-9][0-9][0-9]d/) {print $0}'
Both will only show pods that exists for 6 days or longer. First option will also look for those with errors and the second one will show only those with the Status=Pending.