首页 > 解决方案 > 是否可以从 configmap 为 Horizo​​ntalPodAutoscaler 设置外部指标的名称?GKE

问题描述

我正在修改使用 Horizo​​ntalPodAutoscaler (HPA) 自动缩放的部署。此部署是管道的一部分,在该管道中,工作人员从 pubsub 订阅中读取消息,执行一些工作并发布到下一个主题。现在我使用 configmap 来定义部署的管道(configmap 包含输入订阅和输出主题)。HPA 根据输入订阅上的消息数自动缩放。如果可能,我希望能够从 configmap 中提取 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可能来自 configmap。

标签: kubernetesgoogle-kubernetes-enginegoogle-cloud-pubsub

解决方案


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

回答帖子中的问题:

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

正如用户@Abdennour TOUMI 所指出的,不可能使用HPAa设置使用的指标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:教程:Autoscaling-metrics:PubSub


推荐阅读