kubernetes的PodDisruptionBudget探究

名字解析:PodDisrutionBudget

pod的中断开销,销毁的pod的数量,反过来意思就是允许正常运行的pod的个数;

三大关键参数:

spec.selector

minAvailable

maxUnavailable

区分maxUnavailable:

部署控制器如deployment,也有maxUnavailable字段,但是与PodDisruptionBudget的maxUnavailable不一样:

部署的maxUnavailable字段

     The maximum number of pods that can be unavailable during the update. Value
     can be an absolute number (ex: 5) or a percentage of desired pods (ex:
     10%!)(MISSING). Absolute number is calculated from percentage by rounding
     down. This can not be 0 if MaxSurge is 0. Defaults to 25%!
     (MISSING)Example: when this is set to 30%!,(MISSING) the old ReplicaSet can
     be scaled down to 70%!o(MISSING)f desired pods immediately when the rolling
     update starts. Once new pods are ready, old ReplicaSet can be scaled down
     further, followed by scaling up the new ReplicaSet, ensuring that the total
     number of pods available at all times during the update is at least
     70%!o(MISSING)f desired pods.

     IntOrString is a type that can hold an int32 or a string. When used in JSON
     or YAML marshalling and unmarshalling, it produces or consumes the inner
     type. This allows you to have, for example, a JSON field that can accept a
     name or number.

驱逐的maxUnavailable字段

     An eviction is allowed if at most "maxUnavailable" pods selected by
     "selector" are unavailable after the eviction, i.e. even in absence of the
     evicted pod. For example, one can prevent all voluntary evictions by
     specifying 0. This is a mutually exclusive setting with "minAvailable".

     IntOrString is a type that can hold an int32 or a string. When used in JSON
     or YAML marshalling and unmarshalling, it produces or consumes the inner
     type. This allows you to have, for example, a JSON field that can accept a
     name or number.

总体上概括就是:

在Deployment 中 .spec.strategy.rollingUpdate.maxUnavailable 等于一个值,然后继续为 Deployment 设置一个 PDB ,其中 .spec.maxUnavailable 设置一个不同的值,优先级究竟是哪一个高呢?基于2种情况分析如下:

 如果是滚动更新的操作,那么 Deployment中 .spec.strategy.rollingUpdate.maxUnavailable 的设定值会生效,即使 PDB 的 .spec.maxUnavailable 设置的值更小。

如果是 Eviction 的情况,那么 PDB 的 .spec.maxUnavailable 的值将优先级更高,即使 Deployment 中 .spec.strategy.rollingUpdate.maxUnavailable 的值设置的更小。

这种逻辑很显然更符合 Kubernetes 的工作原理,Deployment不会去读取 PDB 的字段的,同样的 PDB 也不会去读取 Deployment 的字段。他们是2个独立的控制器。所以其实他们没有关联,只是字段名一样而已。

控制器主要是通过设置应用 Pod 处于正常状态的最低个数或最低百分比,达到应用服务的不中断:

如果不设置任何明确的 PodDistruptionBuget,Pod 将会被直接杀死,然后在别的节点重新调度,这可能导致服务中断

Example:

apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: podinfo-pdb
spec:
  # 如果不满足 PDB,Pod 驱逐将会失败!
  minAvailable: 1      # 最少也要维持一个 Pod 可用
#   maxUnavailable: 1  # 最大不可用的 Pod 数,与 minAvailable 不能同时配置!二选一
  selector:
    matchLabels:
      app: podinfo
YAML 复制 全屏

ClusterAutoscaler 等集群节点伸缩组件,在缩容节点时也会考虑 PodDisruptionBudget. 如果你的集群使用了 ClusterAutoscaler 等动态扩缩容节点的组件,强烈建议设置为所有服务设置 PodDisruptionBudget.

在使用百分比时,计算出的实例数都会被向上取整,这会造成两个现象:

如果使用 minAvailable,实例数较少的情况下,可能会导致 ALLOWED DISRUPTIONS 为 0,所有实例都无法被驱逐了。如果使用 maxUnavailable,因为是向上取整,ALLOWED DISRUPTIONS 的值一定不会低于 1,至少有 1 个实例可以被驱逐。

从便于驱逐的角度看,如果你的服务至少有 2-3 个实例,建议在 PDB 中使用百分比配置 maxUnavailable,而不是 minAvailable. 相对的从确保服务稳定性的角度看,我们则应该使用 minAvailable,确保至少有 1 个实例可用。

 

引用自https://www.cnblogs.com/Eddy24/articles/16955508.html

posted @   若-飞  阅读(115)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示