GKE 中具有 Stackdriver 自定义指标的水平 Pod 自动缩放器失败,并出现“无效指标名称”错误

问题描述 投票:0回答:1

我们正在尝试使用自定义 JVM 指标 (

jvm_memory_bytes_used{area="heap"}
) 通过 Horizontal Pod Autoscaler (HPA) 来扩展 GKE 集群中的部署。

设置:

  • 启用 Stackdriver 管理的 Prometheus
  • 在 JVM 上安装 JMX Exporter 并将其配置为导出所需的指标
  • 部署了用于自定义指标的 Stackdriver 适配器
  • 创建了引用自定义指标的 HPA:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-autoscale
  namespace: somenamespace
spec:
  maxReplicas: 3
  metrics:
  - pods:
      metric:
        name: jvm_memory_bytes_used{area="heap"}  # Metric name in question
      target:
        averageValue: 2G
        type: AverageValue
  type: Pods
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-java-app

问题:

HPA 创建失败并出现以下错误:

Error 400: Invalid metric name: custom.googleapis.com/jvm_memory_bytes_used{area="heap"},

我们尝试了围绕指标名称和区域标签的各种引号组合,但没有任何效果。

问题:

是否可以在 GKE 中使用此特定的自定义指标进行 HPA 扩展?如果是这样,在 HPA 配置中指定它的正确方法是什么?

kubernetes google-kubernetes-engine prometheus google-cloud-stackdriver hpa
1个回答
0
投票

我必须改变三件事才能使其正常工作:

  1. 为指标名称添加适当的前缀和后缀;
  2. 使用
    selector
    子句按标签过滤指标;和
  3. 向指标标签名称添加正确的前缀。

Prometheus 指标有前缀和后缀

来自 水平 Pod 自动缩放 (HPA) |运营套件谷歌云

Prometheus 指标按照以下约定存储:

  • 前缀
    prometheus.googleapis.com
  • 此后缀通常是
    gauge
    counter
    summary
    histogram
    之一,尽管
    untyped
    指标可能具有
    unknown
    unknown:counter
    后缀。要验证后缀,请使用 Metrics Explorer 在 Cloud Monitoring 中查找指标。

所以我做到了,转到 Metrics Explorer,启用构建并搜索我想要使用的指标:

使用指标标签选择器

我想按

area
标签进行过滤,但我们不应该以指标的名称传递它。按标签过滤应通过在
selector
子句中使用
metric
来完成。

还有...

指标标签名称有前缀

我们不能只向

matchLabels
/
matchExpressions
添加指标名称。每个指标标签名称应以
metric.labels.
:

为前缀
metric:
  name: prometheus.googleapis.com|jvm_memory_bytes_used|gauge
    selector:
      matchLabels:
        area: heap

最终结果是这样的:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-autoscale
  namespace: somenamespace
spec:
  maxReplicas: 3
  metrics:
  - pods:
      metric:
        name: prometheus.googleapis.com|jvm_memory_bytes_used|gauge
        selector:
           matchLabels:
             metric.labels.area: heap
      target:
        averageValue: 2G
        type: AverageValue
  type: Pods
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-java-app

这样,我设法让 HPA 对自定义指标做出响应。

© www.soinside.com 2019 - 2024. All rights reserved.