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?
From https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy:
A PodSpec has a
restartPolicyfield with possible valuesAlways,OnFailure, andNever. The default value isAlways.restartPolicyapplies 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 runningrestartPolicy: 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 restartedHowever, 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"]