在 GKE 上的 Kubernetes HorizontalPodAutoscaler 上描述的指标是啥?
Posted
技术标签:
【中文标题】在 GKE 上的 Kubernetes HorizontalPodAutoscaler 上描述的指标是啥?【英文标题】:What do the metrics from describe on Kubernetes HorizontalPodAutoscaler on GKE?在 GKE 上的 Kubernetes HorizontalPodAutoscaler 上描述的指标是什么? 【发布时间】:2020-09-12 17:26:05 【问题描述】:我设置了我的 HorizontalPodAutoscaler,如下所述 https://cloud.google.com/kubernetes-engine/docs/tutorials/external-metrics-autoscaling 以根据来自我的 Pub/Sub 的未确认消息的数量收听缩放。我的愿望是,如果有超过 1 条未确认的消息,则 pod 会扩展。当我运行k describe hpa
时,我得到:
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
"apiVersion":"autoscaling/v2beta1","kind":"HorizontalPodAutoscaler","metadata":"annotations":,"name":"foobar-gke-prod","namespace":"defau...
CreationTimestamp: Mon, 25 May 2020 18:01:33 -0700
Reference: Deployment/foobar-gke-prod
Metrics: ( current / target )
"pubsub.googleapis.com|subscription|num_undelivered_messages" (target average value): 200m / 1
Min replicas: 3
Max replicas: 9
Deployment pods: 5 current / 5 desired
返回的指标数据让我感到困惑。根据控制台指标,当我运行该命令时,未确认的知识消息数量约为 4。所以我不明白200m
是什么意思?为什么不说 4?
这是我对 HPA 的配置
# Template from https://cloud.google.com/kubernetes-engine/docs/tutorials/external-metrics-autoscaling
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: foobar-gke-prod
spec:
minReplicas: 3
maxReplicas: 9
metrics:
- external:
metricName: pubsub.googleapis.com|subscription|num_undelivered_messages
metricSelector:
matchLabels:
resource.labels.subscription_id: prod_foobar_subscription
targetAverageValue: "1"
type: External
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: foobar-gke-prod
【问题讨论】:
在 HPA 指标中,小数值以毫单位表示。 200m 意味着您只利用了 20% 的容量。您可能需要仔细检查未确认消息的数量。 【参考方案1】:参考示例:
Name: pubsub
...
Metrics: ( current / target )
"pubsub.googleapis.com|subscription|num_undelivered_messages" (target average value): 2250m / 2
Min replicas: 1
Max replicas: 4
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True SucceededRescale the HPA controller was able to update the target scale to 4
ScalingLimited True TooManyReplicas the desired replica count is more than the maximum replica count
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulRescale 7s horizontal-pod-autoscaler New size: 4; reason: external metric pubsub.googleapis.com|subscription|num_undelivered_messages(&LabelSelectorMatchLabels:map[string]stringresource.labels.subscription_id: echo-read,,MatchExpressions:[],) above target
“指标”部分给出了 HPA 观察到的指标的最后一个值。 小数值以毫单位表示。例如,在上面的输出中,应用程序有 4 个副本,而当前 Pub/Sub 订阅中未确认的消息数为 9。因此每个副本的平均消息数为 2.25 或 2250m。
这意味着在您的情况下,返回的指标数据让我感到困惑。根据控制台指标,当我运行该命令时,未确认的知识消息数量约为 4。所以我不明白200m是什么意思?为什么不说 4?
200m/1
意味着在 HPA 测量时,每个运行副本的平均未传递消息数为 0.2(20%)。
注意事项:
确保您大致同时在指标控制台和 HPA 上读取读数,以避免在读取期间因扩展运行而出现差异。读取 5 个 pod 的 4 条消息将导致 800m 的负载,但此时 hpa 可能已经在运行另一个扩展事件。
我鼓励您同时阅读指标控制台和 hpa 并再次验证。
如果您仍然认为此处发布的结果与更新后的 hpa 描述不匹配,我们可以再看看。
编辑:
有没有办法让指标不是跨 pod 的平均值? IE。如果有 5 条未确认的消息,指标数据将读取 5000m?
来自 Kubernetes API 参考ExternalMetricSource v2beta1 Autoscaling:
targetAverageValue
是全局指标的每个 pod 的目标值(作为数量)。
targetValue
是指标的目标值(作为数量)。
注意targetAverageValue
和targetValue
是互斥的。
因此,如果您想要总数而不是平均值,只需在 HPA 上交换它们即可。
【讨论】:
感谢您的详细解答!我现在知道了。有没有办法让这个指标不是跨 pod 的平均值? IE。如果有 5 条未确认的消息,度量数据将读取 5000m 是的,您必须将targetAveraveValue
更改为targetValue
。我将通过一些参考链接将其添加到我的答案中。以上是关于在 GKE 上的 Kubernetes HorizontalPodAutoscaler 上描述的指标是啥?的主要内容,如果未能解决你的问题,请参考以下文章