kubernetes-client
kubernetes-client copied to clipboard
Get pod restart count?
I would like to get the number of times a certain pod has restarted. Is there any method in the k8 client that allows this?
When we do kubectl get pods , there is a restart column that I am interested in.
I see there is a restartCount for individual containers within a pod, but I am more interested in the pod level restart count.
thanks
I'm slightly confused about what you mean by pod level restart count. From what I know pod only seem to have a phase which is available in status field.
@deod use the dsl to get a Pod (client.pods()...), then use pod.getStatus().getContainerStatuses().get(index).getRestartCount() - where index is the appropriate container index.
@shawkins : I think @deod is not interested in individual containers but pod level:
I see there is a restartCount for individual containers within a pod, but I am more interested in the pod level restart count.
@rohanKanojia as far as I can tell the first container restartCount is typically reported as the pod restarts in kubectl. I'm not sure what it does when there are multiple containers.
@deod when we call the command
Kubectl get pods
It returns all the current pods with their restart count information as one of the columns. This restart count tells us the number of times the container inside the respective pod has failed. Means if for some reason a pod fails and and comes back to running state again then its restart count gets incremented by one. This process of starting, crashing and starting again is the state of crashLoopBackOff and the pod will try to restart the container again and again till its crashLoopBackOff limit is reached if it's restartPolicy is Always.
So to conclude the restart count indicates the number of dead containers.
If we talk about pod level then it only has a phase whether ready, running,... Since it's just a scheduling unit hence we don't actually restart it. What I mean to say is that the pod is never dead, it just switches through different phases which in turn is dependent on the containers inside it.
I hope it clears things a bit ...
@deod @shawkins @rohanKanojia Moreover instead of pod restart count we can maybe count the number of times it has gone into the Error state. Whenever it goes back into the ready state then we can increment count by one.
Moreover instead of pod restart count we can maybe count the number of times it has gone into the Error state. Whenever it goes back into the ready state then we can increment count by one.
The client doesn't hold a state of the Pod (or any K8s resource). These fields are calculated from what the Pod status reports.
AFAIK, usually this field reports the total sum of container restarts.
This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!