【问题标题】:Is it possible to set the name of an external metric for a HorizontalPodAutoscaler from a configmap? GKE是否可以从 configmap 为 Horizo​​ntalPodAutoscaler 设置外部指标的名称? GKE
【发布时间】:2020-11-01 04:21:58
【问题描述】:

我正在修改使用 Horizo​​ntalPodAutoscaler (HPA) 自动缩放的部署。此部署是管道的一部分,在该管道中,工作人员从 pubsub 订阅中读取消息,执行一些工作并发布到下一个主题。现在我使用 configmap 来定义部署的管道(configmap 包含输入订阅和输出主题)。 HPA 根据输入订阅上的消息数自动缩放。如果可能,我希望能够从配置映射中提取 HPA 的订阅名称?有没有办法做到这一点?

HPA 示例:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: my-deployment-hpa
  namespace: default
  labels:
    name: my-deployment-hpa
spec:
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - external:
        metricName: pubsub.googleapis.com|subscription|num_undelivered_messages
        metricSelector:
          matchLabels:
            resource.labels.subscription_id: "$INPUT_SUBSCRIPTION"
        targetAverageValue: "2"
      type: External
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-deployment

目前来自 HPA 的值 $INPUT_SUBSCRIPTION 理想情况下可以来自配置映射。

【问题讨论】:

  • 不幸的是,你不能......但你可以使用 prometheus-adapter + HPA 。检查这个教程:itnext.io/…
  • @AbdennourTOUMI 谢谢。即使似乎您仍然会被绑定到在 HPA 的对象度量中对服务名称进行硬编码。如果 HPA 不能自行动态配置,那么我猜你是对的,而且这是不可能的。
  • @AbdennourTOUMI 请提供您的评论作为答案,因为它阐明了 HPA 可以/不能做什么。

标签: kubernetes google-kubernetes-engine google-cloud-pubsub


【解决方案1】:

将此答案发布为社区 wiki 以获得更好的可见性,并且答案已在 cmets 中提供。

从帖子中回答问题:

如果可能,我希望能够从配置映射中提取 HPA 的订阅名称?有没有办法做到这一点?

正如用户@Abdennour TOUMI 所指出的,不可能将HPA 使用的指标设置为ConfigMap

不幸的是,你不能......但你可以使用 prometheus-adapter + HPA 。查看这个教程:itnext.io/...


至于手动解决方法,您可以使用脚本从configMap 中提取所需的指标名称,并使用模板替换和应用新的HPA

configMap 喜欢:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example
data:
  metric_name: "new_awesome_metric" # <-
  not_needed: "only for example" 

以下脚本:

#!/bin/bash

# variables
hpa_file_name="hpa.yaml"
configmap_name="example"
string_to_replace="PLACEHOLDER"

# extract the metric name used in a configmap
new_metric=$(kubectl get configmap $configmap_name -o json | jq '.data.metric_name')

# use the template to replace the $string_to_replace with your $new_metric and apply it
sed "s/$string_to_replace/$new_metric/g" $hpa_file_name | kubectl apply -f -

此脚本需要有一个带有模板的hpa.yaml 才能将其应用为资源(问题中的示例可以在更改后使用:

  • resource.labels.subscription_id: PLACEHOLDER

如需更多参考,此 HPA 定义可基于本指南:

Cloud.google.com: Kubernetes Engine: Tutorials: Autoscaling-metrics: PubSub

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 2020-09-12
    • 2019-04-30
    • 1970-01-01
    • 2021-03-17
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多