sm0ke21
sm0ke21

Reputation: 581

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.

I am getting the error 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:

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:

Prometheus Adapter Values

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

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

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

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. Is this correct?

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? How does that make sense?

Upvotes: 3

Views: 6521

Answers (2)

yixing yan
yixing yan

Reputation: 183

@sm0ke21

The hpa api version autoscaling/v2beta1 is not supported, you should use the autoscaling/v2beta2 or new apiversion

Upvotes: 0

sm0ke21
sm0ke21

Reputation: 581

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:

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:

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.

Upvotes: 4

Related Questions