<h1>六、pod的生命周期</h1>
<div class="lake-content" typography="classic">
<p id="u09d728bc" class="ne-p"><img src="https://cdn.nlark.com/yuque/0/2022/png/29649025/1666799737558-78e4de33-deb3-4f07-8eb4-885d45ed8cca.png" width="1263.2" id="ufa8b61b9" class="ne-image"></p>
<p id="u1d159fd3" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">和一个个独立的应用容器一样,Pod 也被认为是相对临时性(而不是长期存在)的实体。 Pod 会被创建、赋予一个唯一的 ID(</span><a href="https://kubernetes.io/zh-cn/docs/concepts/overview/working-with-objects/names/#uids" data-href="https://kubernetes.io/zh-cn/docs/concepts/overview/working-with-objects/names/#uids" target="_blank" class="ne-link"><span class="ne-text">UID</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">), 并被调度到节点,并在终止(根据重启策略)或删除之前一直运行在该节点。</span></p>
<p id="u13d0d4e4" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">如果一个</span><a href="https://kubernetes.io/zh-cn/docs/concepts/architecture/nodes/" data-href="https://kubernetes.io/zh-cn/docs/concepts/architecture/nodes/" target="_blank" class="ne-link"><span class="ne-text">节点</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">死掉了,调度到该节点的 Pod 也被计划在给定超时期限结束后</span><a href="https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-garbage-collection" data-href="https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-garbage-collection" target="_blank" class="ne-link"><span class="ne-text">删除</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">。</span></p>
<p id="u6c96d809" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">Pod 自身不具有自愈能力。如果 Pod 被调度到某</span><a href="https://kubernetes.io/zh-cn/docs/concepts/architecture/nodes/" data-href="https://kubernetes.io/zh-cn/docs/concepts/architecture/nodes/" target="_blank" class="ne-link"><span class="ne-text">节点</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">而该节点之后失效, Pod 会被删除;类似地,Pod 无法在因节点资源耗尽或者节点维护而被驱逐期间继续存活。 Kubernetes 使用一种高级抽象来管理这些相对而言可随时丢弃的 Pod 实例, 称作</span><a href="https://kubernetes.io/zh-cn/docs/concepts/architecture/controller/" data-href="https://kubernetes.io/zh-cn/docs/concepts/architecture/controller/" target="_blank" class="ne-link"><span class="ne-text">控制器</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">。</span></p>
<p id="ucc2e9e0f" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">任何给定的 Pod (由 UID 定义)从不会被“重新调度(rescheduled)”到不同的节点; 相反,这一 Pod 可以被一个新的、几乎完全相同的 Pod 替换掉。 如果需要,新 Pod 的名字可以不变,但是其 UID 会不同。</span></p>
<p id="udaebc062" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">如果某物声称其生命期与某 Pod 相同,例如存储</span><a href="https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/" data-href="https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/" target="_blank" class="ne-link"><span class="ne-text">卷</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">, 这就意味着该对象在此 Pod (UID 亦相同)存在期间也一直存在。 如果 Pod 因为任何原因被删除,甚至某完全相同的替代 Pod 被创建时, 这个相关的对象(例如这里的卷)也会被删除并重建。</span></p>
<p id="u54f82026" class="ne-p" style="text-align: left"><img src="https://cdn.nlark.com/yuque/0/2022/svg/29649025/1666802667072-8b4eeb04-0d54-45bd-9360-4e0591ada04e.svg" width="141" id="ucc72f7b1" class="ne-image" style="color: rgb(34, 34, 34); font-size: 16px"></p>
<h4 id="yAyHF" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34)">Pod 结构图例</span></h4>
<p id="uf588bee3" class="ne-p" style="text-align: left"><em><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">一个包含多个容器的 Pod 中包含一个用来拉取文件的程序和一个 Web 服务器, 均使用持久卷作为容器间共享的存储。</span></em></p>
<h2 id="WeGYM" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34)">容器状态</span></h2>
<p id="ud002ebac" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">Kubernetes 会跟踪 Pod 中每个容器的状态,就像它跟踪 Pod 总体上的</span><a href="https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase" data-href="https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase" target="_blank" class="ne-link"><span class="ne-text">阶段</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">一样。 你可以使用</span><a href="https://kubernetes.io/zh-cn/docs/concepts/containers/container-lifecycle-hooks/" data-href="https://kubernetes.io/zh-cn/docs/concepts/containers/container-lifecycle-hooks/" target="_blank" class="ne-link"><span class="ne-text">容器生命周期回调</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">来在容器生命周期中的特定时间点触发事件。</span></p>
<p id="u7f46bed7" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">一旦</span><a href="https://kubernetes.io/zh-cn/docs/reference/command-line-tools-reference/kube-scheduler/" data-href="https://kubernetes.io/zh-cn/docs/reference/command-line-tools-reference/kube-scheduler/" target="_blank" class="ne-link"><span class="ne-text">调度器</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">将 Pod 分派给某个节点,</span><span class="ne-text" style="color: inherit; font-size: 14px">kubelet</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">就通过</span><a href="https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes" data-href="https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes" target="_blank" class="ne-link"><span class="ne-text">容器运行时</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">开始为 Pod 创建容器。容器的状态有三种:</span><span class="ne-text" style="color: inherit; font-size: 14px">Waiting</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">(等待)、</span><span class="ne-text" style="color: inherit; font-size: 14px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">(运行中)和</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">(已终止)。</span></p>
<p id="ubb96d9f8" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">要检查 Pod 中容器的状态,你可以使用</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">kubectl describe pod <pod 名称></span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">。 其输出中包含 Pod 中每个容器的状态。</span></p>
<p id="u4837537f" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">每种状态都有特定的含义:</span></p>
<h3 id="uDqbR" style="text-align: left"><span class="ne-text" style="color: rgb(201, 115, 0); font-size: 22px">Waiting</span><span class="ne-text" style="color: rgb(34, 34, 34)"> </span><span class="ne-text" style="color: rgb(34, 34, 34)">(等待)</span></h3>
<p id="ub67d3c60" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">如果容器并不处在</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">或</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态之一,它就处在</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Waiting</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态。 处于</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Waiting</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的容器仍在运行它完成启动所需要的操作:例如, 从某个容器镜像仓库拉取容器镜像,或者向容器应用</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><a href="https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/" data-href="https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/" target="_blank" class="ne-link"><span class="ne-text">Secret</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">数据等等。 当你使用</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">kubectl</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">来查询包含</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Waiting</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的容器的 Pod 时,你也会看到一个 Reason 字段,其中给出了容器处于等待状态的原因。</span></p>
<h3 id="zXy4h" style="text-align: left"><span class="ne-text" style="color: rgb(201, 115, 0); font-size: 22px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34)">(运行中)</span></h3>
<p id="uf2a37cff" class="ne-p" style="text-align: left"><span class="ne-text" style="color: inherit; font-size: 14px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态表明容器正在执行状态并且没有问题发生。 如果配置了</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">postStart</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">回调,那么该回调已经执行且已完成。 如果你使用</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">kubectl</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">来查询包含</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的容器的 Pod 时, 你也会看到关于容器进入</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Running</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的信息。</span></p>
<h3 id="pReMC" style="text-align: left"><span class="ne-text" style="color: rgb(201, 115, 0); font-size: 22px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34)">(已终止)</span></h3>
<p id="u6dca059b" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">处于</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的容器已经开始执行并且或者正常结束或者因为某些原因失败。 如果你使用</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">kubectl</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">来查询包含</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态的容器的 Pod 时, 你会看到容器进入此状态的原因、退出代码以及容器执行期间的起止时间。</span></p>
<p id="u692386be" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">如果容器配置了</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">preStop</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">回调,则该回调会在容器进入</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">Terminated</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">状态之前执行。</span></p>
<h2 id="OAACI" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34)">容器重启策略</span></h2>
<p id="u3adf714d" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">Pod 的</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">spec</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">中包含一个</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">restartPolicy</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">字段,其可能取值包括 Always、OnFailure 和 Never。默认值是 Always。</span></p>
<p id="u16b17b02" class="ne-p" style="text-align: left"><span class="ne-text" style="color: inherit; font-size: 14px">restartPolicy</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">适用于 Pod 中的所有容器。</span><span class="ne-text" style="color: inherit; font-size: 14px">restartPolicy</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">仅针对同一节点上</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: inherit; font-size: 14px">kubelet</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">的容器重启动作。当 Pod 中的容器退出时,</span><span class="ne-text" style="color: inherit; font-size: 14px">kubelet</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">会按指数回退方式计算重启的延迟(10s、20s、40s、...),其最长延迟为 5 分钟。 一旦某容器执行了 10 分钟并且没有出现问题,</span><span class="ne-text" style="color: inherit; font-size: 14px">kubelet</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">对该容器的重启回退计时器执行重置操作。</span></p>
<h2 id="mHnS6" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34)">Pod 状况</span></h2>
<p id="u170b181f" class="ne-p" style="text-align: left"><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">Pod 有一个 PodStatus 对象,其中包含一个</span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#podcondition-v1-core" data-href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#podcondition-v1-core" target="_blank" class="ne-link"><span class="ne-text">PodConditions</span></a><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px"> </span><span class="ne-text" style="color: rgb(34, 34, 34); font-size: 16px">数组。Pod 可能通过也可能未通过其中的一些状况测试。 Kubelet 管理以下 PodCondition:</span></p>
<ul class="ne-ul">