简体   繁体   English

K8S HPA - 无法从外部指标中获取指标 API

[英]K8S HPA - Cannot fetch metrics from External metrics API

I am trying to get Kafka topic lag into Prometheus and finally to the APIServer in order to utilize an external metrics HPA for my application.我试图让 Kafka 主题滞后到 Prometheus,最后到 APIServer,以便为我的应用程序使用外部指标 HPA。

I am getting the error no metrics returned from external metrics API我收到错误no metrics returned from external metrics API

70m         Warning   FailedGetExternalMetric        horizontalpodautoscaler/kafkademo-hpa   unable to get external metric default/kafka_lag_metric_sm0ke/&LabelSelector{MatchLabels:map[string]string{topic: prices,},MatchExpressions:[]LabelSelectorRequirement{},}: no metrics returned from external metrics API
66m         Warning   FailedComputeMetricsReplicas   horizontalpodautoscaler/kafkademo-hpa   invalid metrics (1 invalid out of 1), first error is: failed to get external metric kafka_lag_metric_sm0ke: unable to get external metric default/kafka_lag_metric_sm0ke/&LabelSelector{MatchLabels:map[string]string{topic: prices,},MatchExpressions:[]LabelSelectorRequirement{},}: no metrics returned from external metrics API

This happens even though I can see the following output when querying the external API:即使在查询外部 API 时我可以看到以下 output,也会发生这种情况:

kubectl get --raw /apis/external.metrics.k8s.io/v1beta1 | jq
{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "external.metrics.k8s.io/v1beta1",
  "resources": [
    {
      "name": "kafka_lag_metric_sm0ke",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    }
  ]
}

Here's the set-up:这是设置:

  • Kafka: v2.7.0卡夫卡:v2.7.0
  • Prometheus: v2.26.0普罗米修斯:v2.26.0
  • Prometheus Adapter: v0.8.3普罗米修斯适配器:v0.8.3

Prometheus Adapter Values Prometheus 适配器值

rules:
  external:
  - seriesQuery: 'kafka_consumergroup_group_lag{topic="prices"}'
    resources:
      template: <<.Resource>>
    name:
      as: "kafka_lag_metric_sm0ke"
    metricsQuery: 'avg by (topic) (round(avg_over_time(<<.Series>>{<<.LabelMatchers>>}[1m])))'

HPA HPA

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: kafkademo-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: kafkademo
  minReplicas: 3
  maxReplicas: 12
  metrics:
  - type: External
    external:
      metricName: kafka_lag_metric_sm0ke
      metricSelector:
        matchLabels:
          topic: prices
      targetValue: 5

HPA information HPA信息

kubectl describe hpa kafkademo-hpa 
Name:                                       kafkademo-hpa
Namespace:                                  default
Labels:                                     <none>
Annotations:                                <none>
CreationTimestamp:                          Sat, 17 Apr 2021 20:01:29 +0300
Reference:                                  Deployment/kafkademo
Metrics:                                    ( current / target )
  "kafka_lag_metric_sm0ke" (target value):  <unknown> / 5
Min replicas:                               3
Max replicas:                               12
Deployment pods:                            3 current / 0 desired
Conditions:
  Type           Status  Reason                   Message
  ----           ------  ------                   -------
  AbleToScale    True    SucceededGetScale        the HPA controller was able to get the target's current scale
  ScalingActive  False   FailedGetExternalMetric  the HPA was unable to compute the replica count: unable to get external metric default/kafka_lag_metric_sm0ke/&LabelSelector{MatchLabels:map[string]string{topic: prices,},MatchExpressions:[]LabelSelectorRequirement{},}: no metrics returned from external metrics API
Events:
  Type     Reason                        Age                     From                       Message
  ----     ------                        ----                    ----                       -------
  Warning  FailedComputeMetricsReplicas  70m (x335 over 155m)    horizontal-pod-autoscaler  invalid metrics (1 invalid out of 1), first error is: failed to get external metric kafka_lag_metric_sm0ke: unable to get external metric default/kafka_lag_metric_sm0ke/&LabelSelector{MatchLabels:map[string]string{topic: prices,},MatchExpressions:[]LabelSelectorRequirement{},}: no metrics returned from external metrics API
  Warning  FailedGetExternalMetric       2m30s (x366 over 155m)  horizontal-pod-autoscaler  unable to get external metric default/kafka_lag_metric_sm0ke/&LabelSelector{MatchLabels:map[string]string{topic: prices,},MatchExpressions:[]LabelSelectorRequirement{},}: no metrics returned from external metrics API

-- Edit 1 -- 编辑 1

When i query the default namespace i get this:当我查询默认命名空间时,我得到这个:

kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/default/kafka_lag_metric_sm0ke |jq
{
  "kind": "ExternalMetricValueList",
  "apiVersion": "external.metrics.k8s.io/v1beta1",
  "metadata": {},
  "items": []
}

I can see that the "items" field is empty.我可以看到“项目”字段是空的。 What does this mean?这是什么意思?

What i don't seem to comprehend is the chain of events that happen behind the scenes.我似乎不理解的是幕后发生的一系列事件。

AFAIK this is what happens. AFAIK这就是发生的事情。 Is this correct?这样对吗?

  • prometheus-adapter queries Prometheus, executes the seriesQuery, computes the metricsQuery and creates "kafka_lag_metric_sm0ke" prometheus-adapter 查询 Prometheus,执行 seriesQuery,计算 metricsQuery 并创建“kafka_lag_metric_sm0ke”
  • It registers an endpoint with the api server for external metrics.它向 api 服务器注册一个端点以获取外部指标。
  • The API Server will periodically update its stats based on that endpoint. API 服务器将根据该端点定期更新其统计信息。
  • The HPA checks "kafka_lag_metric_sm0ke" from the API server and performs the scaling according to the supplied values. HPA 从 API 服务器检查“kafka_lag_metric_sm0ke”并根据提供的值执行缩放。

I also don't seem to understand the significance of namespaces in all this.我似乎也不理解命名空间在这一切中的重要性。 I can see that the stat is namespaced.我可以看到该统计信息已命名空间。 Does that mean that there will be 1 stat per namespace?这是否意味着每个命名空间将有 1 个统计信息? How does that make sense?这有什么意义?

In a long tradition of answering my own questions after I ask them, here's what's wrong with the above configuration.在我提出问题后回答我自己的问题的悠久传统中,这就是上述配置的问题所在。

The error lies in the prometheus-adapter yaml:错误在于prometheus-adapter yaml:

rules:
  external:
    - seriesQuery: 'kafka_consumergroup_group_lag{topic="prices"}'
      resources:
        template: <<.Resource>>
      name:
        as: "kafka_lag_metric_sm0ke"
      metricsQuery: 'avg by (topic) (round(avg_over_time(<<.Series>>{<<.LabelMatchers>>}[1m])))'

I removed <<.LabelMatchers>> and now it works:我删除了<<.LabelMatchers>>现在它可以工作了:

kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/default/kafka_lag_metric_sm0ke |jq
{
  "kind": "ExternalMetricValueList",
  "apiVersion": "external.metrics.k8s.io/v1beta1",
  "metadata": {},
  "items": [
    {
      "metricName": "kafka_lag_metric_sm0ke",
  "metricLabels": {
        "topic": "prices"
      },
      "timestamp": "2021-04-21T16:55:18Z",
      "value": "0"
    }
  ]
}

I am still unsure as to why it works.我仍然不确定它为什么起作用。 I know that <<.LabelMatchers>> in this case will be substituted with something that doesn't produce a valid query, but I don't know what it is.我知道在这种情况下<<.LabelMatchers>>将被替换为不会产生有效查询的东西,但我不知道它是什么。

@sm0ke21 @sm0ke21

The hpa api version autoscaling/v2beta1 is not supported, you should use the autoscaling/v2beta2 or new apiversion不支持 hpa api 版本autoscaling/v2beta1 ,您应该使用autoscaling/v2beta2或新的 apiversion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM