Cluster Proportional Autoscaler
Cluster Proportional Autoscaler(CPA) 是 Kubernetes 集群中的一种自动扩展机制,旨在根据集群中运行的 Pod 数量动态调整特定类型的资源(如 DaemonSet 或 StatefulSet)的副本数。CPA 主要用于确保这些资源能够根据集群的负载情况进行适当的扩展和收缩,从而优化资源利用率和性能。
工作原理
CPA 通过监控集群中的节点数量或可调度的核心数,根据预定义的线性或阶梯式配置来调整目标资源的副本数。主要特点:
- 线性模式(Linear):根据节点数量或核心数按照线性比例计算副本数
- 阶梯模式(Ladder):根据不同的阈值范围设置不同的副本数
- 适用场景:适合 CoreDNS、Metrics Server 等系统组件的自动扩缩容
Helm 安装
添加 Helm 仓库
helm repo add cluster-proportional-autoscaler https://kubernetes-sigs.github.io/cluster-proportional-autoscaler
helm repo update
安装 CPA
helm install cluster-proportional-autoscaler \
cluster-proportional-autoscaler/cluster-proportional-autoscaler \
--namespace kube-system \
--create-namespace \
-f values.yaml
快速安装示例
为 CoreDNS 配置自动扩缩容:
helm install coredns-autoscaler \
cluster-proportional-autoscaler/cluster-proportional-autoscaler \
--namespace kube-system \
--set config.linear.coresPerReplica=256 \
--set config.linear.nodesPerReplica=16 \
--set config.linear.min=2 \
--set config.linear.max=10 \
--set options.target="deployment/coredns"
配置说明
Values.yaml 配置示例
# 镜像配置
image:
repository: registry.k8s.io/cpa/cluster-proportional-autoscaler
tag: v1.8.9
pullPolicy: IfNotPresent
# 资源限制
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 32Mi
# 配置模式:linear(线性)或 ladder(阶梯)
config:
# 线性模式配置
linear:
coresPerReplica: 256 # 每个副本对应的 CPU 核心数
nodesPerReplica: 16 # 每个副本对应的节点数
min: 2 # 最小副本数
max: 10 # 最大副本数
preventSinglePointFailure: true # 防止单点故障,至少保持 2 个副本
includeUnschedulableNodes: true # 是否包含不可调度的节点
# CPA 运行选项
options:
namespace: kube-system # 目标资源所在的命名空间
target: "deployment/coredns" # 目标资源(deployment/xxx 或 replicationcontroller/xxx)
pollPeriodSeconds: 10 # 轮询周期(秒)
logtostderr: true # 日志输出到 stderr
v: 2 # 日志级别(0-4)
# ServiceAccount 配置
serviceAccount:
create: true
name: cluster-proportional-autoscaler
annotations: { }
# 节点选择器和容忍度
nodeSelector: { }
tolerations: [ ]
affinity: { }
阶梯模式配置
config:
ladder:
nodesToReplicas:
- [ 1, 1 ] # 1 个节点时,1 个副本
- [ 2, 2 ] # 2-4 个节点时,2 个副本
- [ 5, 3 ] # 5-9 个节点时,3 个副本
- [ 10, 5 ] # 10+ 个节点时,5 个副本
coresToReplicas:
- [ 1, 1 ]
- [ 64, 3 ]
- [ 128, 5 ]
- [ 256, 7 ]
参数说明
核心参数
| 参数 | 说明 | 默认值 | 示例 |
|---|---|---|---|
options.target | 目标资源类型和名称 | 必填 | deployment/coredns |
options.namespace | 目标资源所在命名空间 | default | kube-system |
options.pollPeriodSeconds | 轮询检查周期(秒) | 10 | 30 |
线性模式参数
| 参数 | 说明 | 计算公式 |
|---|---|---|
config.linear.coresPerReplica | 每个副本对应的 CPU 核心数 | replicas = max(ceil(cores / coresPerReplica), ceil(nodes / nodesPerReplica)) |
config.linear.nodesPerReplica | 每个副本对应的节点数 | 同上 |
config.linear.min | 最小副本数 | 保证最小副本数 |
config.linear.max | 最大副本数 | 限制最大副本数 |
config.linear.preventSinglePointFailure | 防止单点故障 | 当计算结果为 1 时,强制设为 2 |
config.linear.includeUnschedulableNodes | 是否计入不可调度节点 | 默认 true |
阶梯模式参数
| 参数 | 说明 | 示例 |
|---|---|---|
config.ladder.nodesToReplicas | 节点数到副本数的映射 | [[1,1], [5,3], [10,5]] |
config.ladder.coresToReplicas | CPU 核心数到副本数的映射 | [[1,1], [128,5], [256,7]] |
阶梯模式说明:
- 每个映射项
[threshold, replicas]表示当资源数量 >= threshold 时,副本数为 replicas - 如果同时配置了
nodesToReplicas和coresToReplicas,取两者计算结果的最大值
使用示例
示例 1:为 CoreDNS 配置自动扩缩容
# coredns-autoscaler-values.yaml
config:
linear:
coresPerReplica: 256
nodesPerReplica: 16
min: 2
max: 20
preventSinglePointFailure: true
options:
namespace: kube-system
target: "deployment/coredns"
pollPeriodSeconds: 10
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 20m
memory: 32Mi
安装:
helm install coredns-autoscaler \
cluster-proportional-autoscaler/cluster-proportional-autoscaler \
-f coredns-autoscaler-values.yaml \
--namespace kube-system
示例 2:使用阶梯模式
# metrics-server-autoscaler-values.yaml
config:
ladder:
nodesToReplicas:
- [ 1, 1 ]
- [ 5, 2 ]
- [ 20, 3 ]
- [ 50, 5 ]
- [ 100, 10 ]
options:
namespace: kube-system
target: "deployment/metrics-server"
pollPeriodSeconds: 30
示例 3:多条件配置
config:
linear:
coresPerReplica: 128
nodesPerReplica: 8
min: 3
max: 50
preventSinglePointFailure: true
includeUnschedulableNodes: false # 不计入不可调度节点
options:
namespace: kube-system
target: "deployment/kube-state-metrics"
pollPeriodSeconds: 15
v: 2
计算示例
线性模式计算示例
示例 1:标准线性计算
集群状态:
- 节点数:32 个
- CPU 总核心数:512 核
CPA 配置:
config:
linear:
coresPerReplica: 256
nodesPerReplica: 16
min: 2
max: 10
计算过程:
步骤 1 - 基于核心数计算:
ceil(512 / 256) = ceil(2) = 2
步骤 2 - 基于节点数计算:
ceil(32 / 16) = ceil(2) = 2
步骤 3 - 取两者最大值:
max(2, 2) = 2
步骤 4 - 应用 min/max 限制:
max(min(2, 10), 2) = 2
最终副本数:2
示例 2:触发最小值限制
集群状态:
- 节点数:3 个
- CPU 总核心数:48 核
CPA 配置:
config:
linear:
coresPerReplica: 256
nodesPerReplica: 16
min: 5
max: 20
计算过程:
步骤 1 - 基于核心数计算:
ceil(48 / 256) = ceil(0.1875) = 1
步骤 2 - 基于节点数计算:
ceil(3 / 16) = ceil(0.1875) = 1
步骤 3 - 取两者最大值:
max(1, 1) = 1
步骤 4 - 应用 min/max 限制:
max(min(1, 20), 5) = max(1, 5) = 5
最终副 本数:5(触发最小值限制)
示例 3:触发最大值限制
集群状态:
- 节点数:200 个
- CPU 总核心数:3200 核
CPA 配置:
config:
linear:
coresPerReplica: 128
nodesPerReplica: 8
min: 2
max: 15
preventSinglePointFailure: false
计算过程:
步骤 1 - 基于核心数计算:
ceil(3200 / 128) = ceil(25) = 25
步骤 2 - 基于节点数计算:
ceil(200 / 8) = ceil(25) = 25
步骤 3 - 取两者最大值:
max(25, 25) = 25
步骤 4 - 应用 min/max 限制:
max(min(25, 15), 2) = max(15, 2) = 15
最终副本数:15(触发最大值限制)
示例 4:防止单点故障
集群状态:
- 节点数:2 个
- CPU 总核心数:32 核
CPA 配置:
config:
linear:
coresPerReplica: 64
nodesPerReplica: 4
min: 1
max: 10
preventSinglePointFailure: true
计算过程:
步骤 1 - 基于核心数计算:
ceil(32 / 64) = ceil(0.5) = 1
步骤 2 - 基于节点数计算:
ceil(2 / 4) = ceil(0.5) = 1
步骤 3 - 取两者最大值:
max(1, 1) = 1
步骤 4 - 应用防单点故障规则:
因为 preventSinglePointFailure = true 且结果为 1
强制设为 2