How to find out the time spent of a Pod?

4/17/2020

I have a requirement I want to know every part of the time spent of a Pod.

  1. how much time to pull a docker image? Maybe a Pod has multiple initContainers and containers. I want to know every part of them.

Maybe I can analysis the Events using 'kubectl describe pod-name...'

  1. how much time a Pod get ready? From being created and get readiness ready.

For a bare Pod, I can know the startTime of the Pod and which time it is finished. Then I can calculate the duration.

But for pods that created by Deployment,StatefulSet,DaemonSet, I cannot find any time flag that indicating the first time that the Pod becomes readiness ready.

I want to know how much time spent to get the Pod ready. Not the age of the Pod.

-- Cain
kubernetes

1 Answer

4/20/2020

The easiest method would be to subscribe to api-server to notify you if some changes occur in your cluster.

For example, I issued:

$ kubectl get pods --output-watch-events --watch

and then created a new pod. Here is the output:

EVENT      NAME            READY   STATUS              RESTARTS   AGE
ADDED      example-pod     0/1     Pending             0          0s
MODIFIED   example-pod     0/1     ContainerCreating   0          0s
MODIFIED   example-pod     0/1     Running             0          19s
MODIFIED   example-pod     1/1     Running             0          23s

and here is a little explanation:

  • As you can see first event is ADDED and it is in Pending state which means that pod object just got created.
  • Second event is MODIFIED with ContainerCreating status, age 0 which means it took less than 1s time to assing/schedule the pod to a node. Now kubelet starts downloading continer image.
  • Third event has Running status meaning continer is started running. Looking at age column you can see it took 19s since previous event so it took around 19s to download the image and start the container. If you take a look at READY column you can see 0/1 value, so container is running but it is not yet in ready state.
  • Fourth event has READY column set to 1/1 so readiness probe has passed successfully. If you now look at the age column, you can see it took around 4s (32-19) to check readiness probe and change pod status.

If this information in not enough you can use --output= parameter to receive full pod specification on every change.

You can also play with kubectl get events to receive some more events. And of course, by adding --watch flag you can watch events in real time.

If you want higher level of felxibility, use dedicated kubernetes clinet libraries instead of kuebctl to receive this information and to process it.

-- HelloWorld
Source: StackOverflow