简体   繁体   English

Kubernetes中的容器垃圾收集

[英]Container garbage collection in Kubernetes

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. 如果主容器完成但sidecar仍在运行。 Would Kubernetes garbage collects the main container? Kubernetes垃圾会收集主要容器吗? 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? MaxPerPodContainer标志与此有何关系?

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

A PodSpec has a restartPolicy field with possible values Always , OnFailure , and Never . PodSpec具有restartPolicy字段,其可能值为AlwaysOnFailureNever The default value is Always . 默认值为Always restartPolicy applies to all Containers in the Pod. restartPolicy适用于Pod中的所有容器。

In practice this means the following for Pods : 实际上,对于Pod而言,这意味着:

  • restartPolicy: Never : if one container terminates then all other containers will keep running restartPolicy: Never :如果一个容器终止,则所有其他容器将继续运行
  • restartPolicy: OnFailure : if one container terminates in an error state then that container is restarted; restartPolicy: OnFailure :如果一个容器以错误状态终止,则该容器将重新启动;否则,容器将重新启动。 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. 如果所有容器都干净地终止,则Pod会进入Completed状态并保持原状。
  • restartPolicy: Always : if one container terminates in an error state then that container is restarted restartPolicy: Always :如果一个容器以错误状态终止,则该容器将重新启动

However, you are most likely using a Deployment , which mandates restartPolicy: OnFailure in the Pod template. 但是,您最有可能使用Deployment ,该部署在Pod模板中强制要求restartPolicy: OnFailure 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. 根据您要尝试执行的操作, initContainers可能是一个解决方案。

Maybe experiment a little with a Pod like this: 也许可以像下面这样试验Pod:

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM