【云原生】Kubernetes PDB(Pod Disruption Budget)介绍与简单使用

一、概述

Pod Disruption Budget (pod 中断 预算) 简称PDB,含义其实是终止pod前通过 labelSelector 机制获取正常运行的pod数目的限制,目的是对 自愿中断的保护措施主动驱逐PodKubernetes version >= 1.7 才支持 PodDisruptionBudget

官方文档:https://kubernetes.io/zh-cn/docs/tasks/run-application/configure-pdb/

完整版 k8s 内置资源结构图:

【云原生】Kubernetes PDB(Pod Disruption Budget)介绍与简单使用

二、PDB 应用场景

  • 节点维护或升级时 ( kubectl drain )
  • 对应用的自动缩容操作(autoscaling down)

【温馨提示】如果 Node 状态处于 not ready,PDB 是不会生效,因为 PDB 只能针对自愿中断生效。

1)自愿中断和非自愿中断场景

Pod 不会消失,直到有人(人类或控制器)将其销毁,或者当出现不可避免的硬件或系统软件错误。

1、非自愿性中断场景

常见的 非自愿性中断场景,例如:

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:3568f5f3-af2c-4c1d-be64-75b632bb71c2

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:06eaae86-c122-4307-b121-e1d6f066f899

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:5f04d017-56c8-4f12-922b-75f576d90d70

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:89e0da44-7f98-4cb4-ae47-aad54f401b26

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:607348e4-ef28-4225-8be0-bf9bb090bff1

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c676b658-2667-4c03-a8ae-4711e81e3196

* 内核恐慌(kernel panic)
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:84cb918f-5cd9-43b5-be76-2a7f9e482a0e

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:186d3a7f-968a-4250-8f90-3a08f7fa5c67

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:de9fd4d0-6dd7-4d91-b1b8-292eadaec068

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:afaf538e-ffd4-47d8-a421-cccd9ced073a

2、自愿性中断场景

常见的 自愿性中断场景,例如:

包括由应用程序所有者发起的操作和由集群管理员发起的操作。

  • 删除管理该 pod 的 Deployment 或其他控制器。
  • 更新了 Deployment 的 pod 模板导致 pod 重启。
  • 直接删除 pod。
  • 排空(drain)节点进行修复或升级。
    [TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:cf62ab99-e752-45ee-9c51-34cc4be609e2
    [En]

    [TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:949116d2-6a82-4371-8c48-dd8717880f5d

  • 从节点中移除一个 pod,以允许其他 pod 使用该节点。

2)PDB 关键参数和注意事项

  • .spec.selector——用于指定其所作用的 Pod 集合(pod label),该字段为 必需字段
  • .spec.minAvailable——表示驱逐后仍须保证可用的 Pod 数量。即使因此影响到 Pod 驱逐 (即该条件在和 Pod 驱逐发生冲突时优先保证)。 minAvailable 值可以是 pod数量或者 百分比
  • .spec.maxUnavailable——表示发生自愿中断的过程中,要保证最大不可用的 pod数量或者 百分比

如果上面配置只能用来应用内置的 DeploymentRSRCStatefulSetStatefulSet的Pods,推荐优先使用 .spec.maxUnavailable。如果你使用了例如 Operator 的其他控制器,那么设置时就要注意以下两点:

  • 只能配置 .spec.minAvailable, 不能使用 maxUnavailable;
  • .spec.minAvailable 只能为整型值,不能是百分比。

【注意事项】

  • 同一个 PDB Object 中 不能同时定义 .spec.minAvailable.spec.maxUnavailable
  • 如果即没有设置 .spec.minAvailable 也没有设置 .spec.maxUnavailable 的话,那么会 默认设置 .spec.minAvailable1
  • 前面提到,应用滚动更新时Pod的 deleteunavailable虽然也属于自愿中断,但是实际上滚动更新有自己的策略控制( marSurgemaxUnavailable),因此PDB不会干预这个过程。
  • PDB 只能保证自愿中断时的副本数,比如 evict pod过程中刚好满足 .spec.minAvailable.spec.maxUnavailable,这时某个本来正常的Pod突然因为Node Down(非自愿中断)挂了,那么这个时候实际Pods数就比PDB中要求的少了,因此PDB不是万能的。

三、示例演示

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: zookeeper
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: zookeeper

Kubernetes PDB 的介绍就先到这里了,其实PDB的使用场景不是很多,有疑问的小伙伴欢迎给我留言哦,后续会持续更新 【云原生和大数据相关的文章,请小伙伴耐心等待哦~

Original: https://www.cnblogs.com/liugp/p/16685823.html
Author: 大数据老司机
Title: 【云原生】Kubernetes PDB(Pod Disruption Budget)介绍与简单使用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562621/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球