Container garbage collection in Kubernetes Container garbage collection in Kubernetes kubernetes kubernetes

Container garbage collection in Kubernetes


From https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy:

A PodSpec has a restartPolicy field with possible values Always, OnFailure, and Never. The default value is Always. restartPolicy applies to all Containers in the Pod.

In practice this means the following for Pods:

  • restartPolicy: Never: if one container terminates then all other containers will keep running
  • restartPolicy: OnFailure: if one container terminates in an error state then that container is restarted; if it terminates cleanly (completes) then the container is not restarted. In both cases the other containers keep running. If all containers terminate cleanly the Pod goes into Completed state and stays like that.
  • restartPolicy: Always: if one container terminates in an error state then that container is restarted

However, you are most likely using a Deployment, which mandates restartPolicy: OnFailure in the Pod template. This means that if a container terminates that containers will be restarted. It is not possible to have a container that only runs for a few minutes.

Depending on what you are trying to do initContainers might be a solution.

Maybe experiment a little with a Pod like this:

kind: Podmetadata:  name: busyboxspec:  restartPolicy: Always  containers:  - name: date    image: busybox    command: ["sh","-c","while date; do sleep 1; done"]  - name: sleep15    image: busybox    command: ["sh","-c","sleep 15; exit 1"]