kubernetes-client icon indicating copy to clipboard operation
kubernetes-client copied to clipboard

Get pod restart count?

Open deod opened this issue 3 years ago • 7 comments

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

deod avatar May 24 '22 21:05 deod

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.

rohanKanojia avatar May 25 '22 06:05 rohanKanojia

@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 avatar May 25 '22 22:05 shawkins

@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 avatar May 26 '22 06:05 rohanKanojia

@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.

shawkins avatar May 26 '22 11:05 shawkins

@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 ...

Ananya2001-an avatar Jul 10 '22 13:07 Ananya2001-an

@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.

Ananya2001-an avatar Jul 10 '22 13:07 Ananya2001-an

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.

manusa avatar Jul 11 '22 05:07 manusa

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!

stale[bot] avatar Oct 09 '22 05:10 stale[bot]