kubernetes - 是否可以从 configmap 为 HorizontalPodAutoscaler 设置外部指标的名称?GKE
问题描述
我正在修改使用 HorizontalPodAutoscaler (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。
解决方案
将此答案发布为社区 wiki 以获得更好的可见性,并在评论中提供了答案。
回答帖子中的问题:
如果可能,我希望能够从 configmap 中提取 HPA 的订阅名称?有没有办法做到这一点?
正如用户@Abdennour TOUMI 所指出的,不可能使用HPA
a设置使用的指标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
推荐阅读
- python-3.x - 如何传递多个 kwargs
- python - 如何从 GeometryArray 或 LineString 中提取坐标?
- python - Atom python IDE,使用命令行执行?
- r - 如何将 R Markdown 文件编织成 pdf
- python - Pandas groupby 加温均值
- java - 在父类的字段初始化上使用派生类的EntityManger
- sql-server - Perform SUM query in ADO.NET
- python - 我如何解释我的 knn 和决策树的结果?
- python - Scrapy无法访问子div类
- javascript - Vanilla JS TypeError:无法读取属性