Home > OS >  kubernetes - is age of pod always since last restart
kubernetes - is age of pod always since last restart

Time:04-14

My understanding is that the AGE shown for a pod when using kubectl get pod, shows the time that the pod has been running since the last restart. So, for the pod shown below, my understanding is that it intially restarted 14 times, but hasn't restarted in the last 17 hours. Is this correct, and where is a kubernetes reference that explains this?

enter image description here

CodePudding user response:

Hope you're enjoying your Kubernetes journey !

In fact, the AGE Headers when using kubectl get pod shows you for how long your pod has been created and it's running. But do not confuse POD and container:

The header "RESTARTS" is actually linked to the parameters > '.status.containerStatuses[0].restartCount' of the pod manifest. That means that this header is linked to the number of restarts, not of the pod, but of the container inside the pod.

Here is an example: I just deployed a new pod:

NAME                       READY   STATUS    RESTARTS   AGE
test-bg-7d57d546f4-f4cql   2/2     Running   0          9m38s

If I check the yaml configuration of this pod, we can see that in the "status" section we have the said "restartCount" field:

❯ k get po test-bg-7d57d546f4-f4cql -o yaml
apiVersion: v1
kind: Pod
metadata:
...
spec:
...
status:
...
  containerStatuses:
  ...
  - containerID: docker://3f53f140f775416644ea598d554e9b8185e7dd005d6da1940d448b547d912798
    ...
    name: test-bg
    ready: true
    restartCount: 0
    ...

So, to demonstrate what I'm saying, I'm going to connect into my pod and kill the main process's my pod is running:

❯ k exec -it test-bg-7d57d546f4-f4cql -- bash
I have no name!@test-bg-7d57d546f4-f4cql:/tmp$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
1000         1  0.0  0.0   5724  3256 ?        Ss   03:20   0:00 bash -c source /tmp/entrypoint.bash
1000        22  1.5  0.1 2966140 114672 ?      Sl   03:20   0:05 java -jar test-java-bg.jar
1000        41  3.3  0.0   5988  3592 pts/0    Ss   03:26   0:00 bash
1000        48  0.0  0.0   8588  3260 pts/0    R    03:26   0:00 ps aux
I have no name!@test-bg-7d57d546f4-f4cql:/tmp$ kill 22
I have no name!@test-bg-7d57d546f4-f4cql:/tmp$ command terminated with exit code 137

and after this, if I reexecute the "kubectl get pod" command, I got this:

NAME                       READY   STATUS    RESTARTS   AGE
test-bg-7d57d546f4-f4cql   2/2     Running   1          11m

Then, if I go back to my yaml config, We can see that the restartCount field is actually linked to my container and not to my pod.

❯ k get po test-bg-7d57d546f4-f4cql -o yaml
apiVersion: v1
kind: Pod
metadata:
...
spec:
...
status:
...
  containerStatuses:
  ...
  - containerID: docker://3f53f140f775416644ea598d554e9b8185e7dd005d6da1940d448b547d912798
    ...
    name: test-bg
    ready: true
    restartCount: 1
    ...

So, to conclude, the RESTARTS header is giving you the restartCount of the container not of the pod, but the AGE header is giving you the age of the pod.

This time, if I delete the pod:

❯ k delete pod test-bg-7d57d546f4-f4cql
pod "test-bg-7d57d546f4-f4cql" deleted

we can see that the restartCount is back to 0 since its a brand new pod with a brand new age:

NAME                       READY   STATUS        RESTARTS   AGE
test-bg-7d57d546f4-bnvxx   2/2     Running       0          23s
test-bg-7d57d546f4-f4cql   2/2     Terminating   2          25m

For your example, it means that the container restarted 14 times, but the pod was deployed 17 hours ago.

I can't find the exact documentation of this but (as it is explained here: https://kubernetes.io/docs/concepts/workloads/_print/#working-with-pods): "Note: Restarting a container in a Pod should not be confused with restarting a Pod. A Pod is not a process, but an environment for running container(s). A Pod persists until it is deleted."

Hope this has helped you better understand. Here is a little tip from https://kubernetes.io/docs/reference/kubectl/cheatsheet/: kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' (to sort your pods by their restartCount number :p)

Bye

  • Related