HPA 无法从普罗米修斯适配器获取自定义指标

HPA is unable to fetch custom metrics from prometheus adapter

我已将 Prometheus 适配器配置为从 Prometheus 获取自定义指标。 当我执行命令时: kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1

结果如下

 {
      "name": "namespaces/envoy_http_ingress_http_downstream_cx_http1",
      "singularName": "",
      "namespaced": false,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "namespaces/envoy_cluster_xds_cluster_upstream_cx_rx_bytes",
      "singularName": "",
      "namespaced": false,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "jobs.batch/statsd_exporter_lines",
      "singularName": "",
      "namespaced": true,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "pods/fs_writes_merged",
      "singularName": "",
      "namespaced": true,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },

我的HPA配置如下:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: scale
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: billing-app
  minReplicas: 1
  maxReplicas: 10
  # targetCPUUtilizationPercentage: 50
  metrics:
    - type: External
      external:
        metricName: fs_writes_merged
        targetValue: 100

Hpa 结果未知。不确定为什么它无法获取指标。

Hpa 必须能够读取自定义指标。

回答

由于您的 HPA 配置将指标声明为 type: External,HPA 尝试从外部指标 API (/apis/custom.metrics.k8s.io) 中获取它,但 Prometheus 适配器将其暴露在自定义指标 API (/apis/custom.metrics.k8s.io).

由于您的指标来自您尝试自动缩放的部署的 Pods,因此您应该使用 Pods 指标类型。因此,将 HPA 配置更改为:

  # ...
  metrics:
    - type: Pods
      pods:
        metricName: fs_writes_merged
        targetValue: 100

背景

您可以在此处查看所有可用的 HPA 指标类型及其用法:

kubectl explain --api-version=autoscaling/v2beta1 hpa.spec.metrics

有四种指标类型,它们映射到各种指标 API,如下所示:

  • Resource:资源指标 API (/apis/metrics.k8s.io/v1beta1)
  • Pods:自定义指标 API (/apis/custom.metrics.k8s.io/v1beta1)
  • Object:自定义指标 API (/apis/custom.metrics.k8s.io/v1beta1)
  • External:外部指标 API (/apis/external.metrics.k8s.io/v1beta1)

参见 HPA docs, and design documents for Resource Metrics API, Custom Metrics API, and External Metrics API