You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GKE中用Stackdriver自定义JVM指标配置HPA时提示“无效指标名称”

问题解答

可行性确认

完全可以在GKE中使用自定义JVM堆内存指标实现HPA扩缩容,你的环境配置(Stackdriver托管Prometheus、JMX Exporter、自定义指标适配器)已经满足核心前提条件。

错误原因

HPA v2的配置规则中,metric.name字段仅允许填写纯指标名称,不能直接附加Prometheus风格的标签过滤条件(如{area="heap"})。标签过滤需要通过metric.selector字段单独配置,这是导致你报错的核心原因。

正确的HPA配置

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

关键配置说明

  • 将原配置中带标签的指标名拆分为两部分:name: jvm_memory_bytes_used(纯指标名)+ selector.matchLabels: {area: "heap"}(标签过滤)
  • 确保selector中的标签键值对与JMX Exporter导出的指标标签完全一致(注意大小写、拼写)

验证步骤

  1. 确认自定义指标已被正确采集到Stackdriver:
    gcloud monitoring metrics list --filter='metric.type="custom.googleapis.com/jvm_memory_bytes_used"'
    
  2. 检查指标适配器是否能正常获取该指标:
    kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/somenamespace/pods/*/jvm_memory_bytes_used" | jq .
    
  3. 创建HPA后,查看状态确认是否正常:
    kubectl get hpa my-autoscale -n somenamespace
    

内容的提问来源于stack exchange,提问作者brandizzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:48:21