I am developing a shell script which will forcefully delete pod stuck in Terminating status(due to any reason).
kubectl get pods --all-namespaces | grep Terminating | while read line; do
pod_name=$(echo $line | awk '{print $2}' ) name_space=$(echo $line | awk
'{print $1}' ); kubectl delete pods $pod_name -n $name_space --grace-period=0 --force;
done
But to be on safe side I want to delete pod only if its in Terminating status since last 10 minute.
How can I get this time as kubectl get pods --all-namespaces | grep Terminating
give AGE of pod not time of current status.
As per documentation pod-lifecycle status.phase can have only 5 different values and none of them is Terminating. This means that termination state in not reflected in this field and therefore filtering by the phase field will not help.
Update1: After suggestions from @Vitalii following is output, These pods (statefulset) are in terminating status due to node shutdown
kubectl -n my_ns get pod mypod-0 -o json| jq '.status.containerStatuses[]'
{
"containerID": "docker://bc4fdf965b19dffd5cb54dddb1931d9f522aad885d1576d03a41cd45d6d60c0d",
"image": "mypod:1.11.0",
"imageID": "docker://sha256:128b5cb1a95cfa001620d9e54b8cf9d0ef96b2a261cab4850accab4cfa0fe6cb",
"lastState": {},
"name": "mypod",
"ready": true,
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2021-04-21T16:42:23Z"
}
}
}