Kubernetes pod cpu HPA 的使用计算方法

Kubernetes pod cpu usage calculation method for HPA

有人可以解释如何在 pods 中计算 cpu 的使用情况吗?多个容器与水平 Pod 自动缩放器一起使用? 它是平均值吗?它是如何计算的?

例如: 如果我们有 2 个容器:

如果我们分别计算并取平均值:(0% + 200%)/2 = 100% 使用率?

如果我们求和并取平均值:2/1.5 = 133% 使用率?

还是我的逻辑不对?

在 Horizo​​ntal Pod Autoscaling 设计文档中明确写道,它采用 pods' CPU 利用率的算术平均值与目标值进行比较。正文如下:

The autoscaler is implemented as a control loop. It periodically queries pods described by Status.PodSelector of Scale subresource, and collects their CPU utilization. Then, it compares the arithmetic mean of the pods' CPU utilization with the target defined in Spec.CPUUtilization, and adjusts the replicas of the Scale if needed to match the target (preserving condition: MinReplicas <= Replicas <= MaxReplicas).

pods的目标人数是根据以下公式计算的:

TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)

更多详情:https://github.com/kubernetes/community/blob/master/contributors/design-proposals/autoscaling/horizontal-pod-autoscaler.md

从 kubernetes 1.9 开始,HPA 将 pod cpu 利用率计算为 pod 中所有容器的总 cpu 使用率除以总请求。因此,在您的示例中,计算出的使用率为 133%。我不认为这是在任何地方的文档中指定的,但相关代码在这里:https://github.com/kubernetes/kubernetes/blob/v1.9.0/pkg/controller/podautoscaler/metrics/utilization.go#L49

但是,我认为这是一个实现细节。因此,它可以在未来的版本中轻松更改。