我们正在尝试使用自定义 JVM 指标 (
jvm_memory_bytes_used{area="heap"}
) 通过 Horizontal Pod Autoscaler (HPA) 来扩展 GKE 集群中的部署。
设置:
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 配置中指定它的正确方法是什么?
我必须改变三件事才能使其正常工作:
selector
子句按标签过滤指标;和Prometheus 指标按照以下约定存储:
- 前缀
。prometheus.googleapis.com
- 此后缀通常是
、gauge
、counter
或summary
之一,尽管histogram
指标可能具有untyped
或unknown
后缀。要验证后缀,请使用 Metrics Explorer 在 Cloud Monitoring 中查找指标。unknown:counter
所以我做到了,转到 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 对自定义指标做出响应。