问题描述
我已经配置了Prometheus-adapter,以从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
)上. /p>
由于度量标准来自要自动缩放的部署"窗格,因此应使用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文档,并为 Resource Metrics API ,自定义指标API 和外部指标API .
I have configured Prometheus-adapter to fetch custom metrics from Prometheus.When I execute the command:kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1
Following is the result.
{
"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"
]
},
My HPA configuration is as follows:
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 results in unknown. Not sure why it is not able to fetch metrics.
Hpa must be able to read the custom metrics.
Answer
Since your HPA configuration declares the metric as type: External
, the HPA tries to fetch it from the External Metrics API (/apis/custom.metrics.k8s.io
), but the Prometheus Adapter exposes it on the Custom metrics API (/apis/custom.metrics.k8s.io
).
Since your metric comes from the Pods of the Deployment that you're trying to autoscale, you should use the Pods
metric type. So, change your HPA configuration to:
# ...
metrics:
- type: Pods
pods:
metricName: fs_writes_merged
targetValue: 100
Background
You can see all the available HPA metric types and their usages here:
kubectl explain --api-version=autoscaling/v2beta1 hpa.spec.metrics
There are four metric types, and they map to the various metric APIs as follows:
Resource
: Resource Metrics API (/apis/metrics.k8s.io/v1beta1
)Pods
: Custom Metrics API (/apis/custom.metrics.k8s.io/v1beta1
)Object
: Custom Metrics API (/apis/custom.metrics.k8s.io/v1beta1
)External
: External Metrics API (/apis/external.metrics.k8s.io/v1beta1
)
See HPA docs, and design documents for Resource Metrics API, Custom Metrics API, and External Metrics API.
这篇关于HPA无法从Prometheus适配器获取自定义指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!