2

I have a question regarding to the container garbage collection within a pod. I have a main container and sidecar container running in a pod. If main container finishes but sidecar is still running. Would Kubernetes garbage collects the main container? Can we guarantee that main container will not be garbage collected until sidecar finishes? If not, is there way of achieving this?

How is MaxPerPodContainer flag relates to this?

1 Answer 1

0

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: Pod
metadata:
  name: busybox
spec:
  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"]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.