简体   繁体   English

Kubernetes 如何计算 HPA 的 CPU 利用率?

[英]How Kubernetes computes CPU utilization for HPA?

I want to understand how HPA computes CPU utilization across Pods.我想了解 HPA 如何计算跨 Pod 的 CPU 利用率。

According to this doc it takes the average of CPU utilization of a pod (average across the last 1 minute) divided by the CPU requested by the pod.根据此文档,它采用 pod 的 CPU 利用率的平均值(过去 1 分钟的平均值)除以 pod 请求的 CPU。 Then it computes the arithmetic mean of all the pods' CPU.然后计算所有 Pod 的 CPU 的算术平均值。

Unfortunately the doc contains some information that are outdated like for example that --horizontal-pod-autoscaler-sync-period is by default set to 30 seconds but in the official doc , the default value is 15 seconds.不幸的是,该文档包含一些过时的信息,例如--horizontal-pod-autoscaler-sync-period默认设置为 30 秒,但在官方文档中,默认值为 15 秒。

When I tested, I noticed that HPA scales up even before that average CPU reaches the threshold I set (which is 90%), Which made me think that maybe it takes the maximum CPU across Pods and not the average.当我测试时,我注意到 HPA 甚至在平均 CPU 达到我设置的阈值(90%)之前就扩展了,这让我认为它可能需要跨 Pod 的最大 CPU 而不是平均值。

在此处输入图片说明

My question is where I can find an updated documentation to understand exactly how HPA works?我的问题是在哪里可以找到更新的文档以准确了解 HPA 的工作原理?

Note that I've not a Kubernetes cluster at hand, this is a theoretical answer based on the source code of k8s.请注意,我手头没有 Kubernetes 集群,这是基于 k8s 源代码的理论答案。
See if this actually matches your experience.看看这是否真的符合你的经验。


Kubernetes is opensource, here seems to be the HPA code . Kubernetes 是开源的,这里似乎是HPA 代码

The functions GetResourceReplica and calcPlainMetricReplicas (for non-utilization percentage) compute the number of replicas given the current metrics.函数GetResourceReplicacalcPlainMetricReplicas (用于非利用率百分比)计算给定当前指标的副本数。
Both use the usageRatio returned by GetMetricUtilizationRatio , this value is multiplied by the number of currently ready pods in the Replica to get the new number of pods:两者都使用usageRatio返回的GetMetricUtilizationRatio ,该值乘以 Replica 中当前准备好的 pod 数量,以获得新的 pod 数量:

New_number_of_pods = Old_numbers_of_ready_pods * usageRatio

There is a tolerance check (ie if the usageRatio falls close enough to 1, nothing is done) and the pending and unkown-state pods are ignored (considered to use 0% of the resource) while the pods without metrics are considered to use 100% of the resource.有一个容忍检查(即如果usageRatio足够接近1,什么都不做)并且挂起和未知状态的pods 被忽略(被认为使用0% 的资源)而没有metrics 的pods 被认为使用100 % 的资源。

The usageRatio is computed by GetResourceUtilizationRatio that is passed the metrics and the requests (of resources) of all the pods, it goes as follow: usageRatioGetResourceUtilizationRatio计算,它传递了所有Pod 的指标和(资源的)请求,如下所示:

utilization = Total_sum_resource_usage_all_pods / Total_sum_resource_requests_all_pods
usageRatio = utilization * 100 / targetUtilization

Where targetUtilization comes from the HPA spec. targetUtilization来自 HPA 规范。
The code is easier to read than this summary of mine, in this context the term request means "resource request" (that's an educated guess).代码比我的这个摘要更容易阅读,在这种情况下,术语请求的意思是“资源请求”(这是一个有根据的猜测)。

So I'd say that 90% is the resource usage across all pods computed as they were all a single pod requesting the sum of each pod's request and collecting the metrics as they were all running on a single dedicated node.所以我会说 90% 是所有 pod的资源使用情况因为它们都是一个单独的 pod,请求每个 pod 的请求总和并收集指标,因为它们都在单个专用节点上运行。

According to https://github.com/kubernetes/kubernetes/issues/78988#issuecomment-502106361 this is configuration dependent and an issue of the metrics server and the kublet reporting, the HPA should rather only using the information: https://kubernetes.io/docs/tasks/debug-application-cluster/resource-metrics-pipeline/#cpu根据https://github.com/kubernetes/kubernetes/issues/78988#issuecomment-502106361,这取决于配置,并且是指标服务器和 kublet 报告的问题,HPA 应该只使用以下信息: https:// kubernetes.io/docs/tasks/debug-application-cluster/resource-metrics-pipeline/#cpu

I think the duration is should be defined by the kubelet's --housekeeping-interval and defaults to 10 seconds我认为持续时间应该由 kubelet 的 --housekeeping-interval 定义,默认为 10 秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM