简体   繁体   English

HPA无法从自定义指标API中获取指标

[英]HPA is unable to fetch metrics from custom metrics API

I have setting a prometheus adapter to be able to use prometheus metrics as custom metrics in kubernetes but I have an issue on auto-scaling a deployment with Object HPA. 我已经设置了Prometheus适配器以能够将Prometheus指标用作kubernetes中的自定义指标,但是在使用Object HPA自动扩展部署时遇到了问题。

Here everything i have from custom-metrics api. 在这里,我从custom-metrics api获得的所有内容。

```
kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/ | jq .
{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "custom.metrics.k8s.io/v1beta1",
  "resources": [
    {
      "name": "services/kong_http_status_per_second",
      "singularName": "",
      "namespaced": false,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    }
  ]
}
```

```
kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/services/*/kong_http_status_per_second | jq .
{
  "kind": "MetricValueList",
  "apiVersion": "custom.metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/custom.metrics.k8s.io/v1beta1/services/%2A/kong_http_status_per_second"
  },
  "items": [
    {
      "describedObject": {
        "kind": "Service",
        "name": "httpbin",
        "apiVersion": "/v1"
      },
      "metricName": "kong_http_status_per_second",
      "timestamp": "2019-01-08T10:30:25Z",
      "value": "339m"
    },
    {
      "describedObject": {
        "kind": "Service",
        "name": "httpbin",
        "apiVersion": "/v1"
      },
      "metricName": "kong_http_status_per_second",
      "timestamp": "2019-01-08T10:30:25Z",
      "value": "339m"
    }
  ]
```

And then my HPA : 然后是我的HPA:

```
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: httpbin
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: httpbin
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Object
    object: 
      metricName: kong_http_status_per_second
      target: 
        apiVersion: v1
        kind: Service
        name: httpbin
      targetValue: 1
```
```
kubectl describe hpa
Name:                                                httpbin
Namespace:                                           lgrondin
Labels:                                              <none>
Annotations:                                         kubectl.kubernetes.io/last-applied-configuration:
                                                       {"apiVersion":"autoscaling/v2beta1","kind":"HorizontalPodAutoscaler","metadata":{"annotations":{},"name":"httpbin","namespace":"lgrondin"}...
CreationTimestamp:                                   Tue, 08 Jan 2019 10:22:24 +0100
Reference:                                           Deployment/httpbin
Metrics:                                             ( current / target )
  "kong_http_status_per_second" on Service/httpbin:  <unknown> / 1
Min replicas:                                        2
Max replicas:                                        10
Deployment pods:                                     2 current / 2 desired
Conditions:
  Type           Status  Reason                 Message
  ----           ------  ------                 -------
  AbleToScale    True    SucceededGetScale      the HPA controller was able to get the target's current scale
  ScalingActive  False   FailedGetObjectMetric  the HPA was unable to compute the replica count: unable to get metric kong_http_status_per_second: Service on lgrondin httpbin/unable to fetch metrics from custom metrics API: the server could not find the metric kong_http_status_per_second for services
Events:
  Type     Reason                 Age                    From                       Message
  ----     ------                 ----                   ----                       -------
  Warning  FailedGetObjectMetric  87s (x449 over 4h31m)  horizontal-pod-autoscaler  unable to get metric kong_http_status_per_second: Service on lgrondin httpbin/unable to fetch metrics from custom metrics API: the server could not find the metric kong_http_status_per_second for services
```

Seems i can get the metric by calling directly the api but the HPA cannot retrieve the metric. 似乎我可以通过直接调用api来获取指标,但是HPA无法检索指标。

Thanks for any help. 谢谢你的帮助。

When I had a similar problem my issue was that the deployment I was attempting to scale was not the one that produced the metric I was relying on. 当我遇到类似的问题时,我的问题是我尝试扩展的部署不是产生我所依赖的指标的部署。
In my case I wanted to scale a pod and here is what worked for me 在我的情况下,我想缩放一个吊舱,这对我有用

  1. I assured that the pod I was attempting to scale was a part of deployment. 我向我保证,尝试扩展的Pod是部署的一部分。
  2. I assured that that pod was exposing the desired metrics on a specific port and that Prometheus scraped these metrics using annotation. 我保证该Pod可以在特定端口上公开所需的指标,而Prometheus会使用注释将其删除。
  3. I assured that that deployment was on the same namespace as the Hpa pod. 我保证该部署与Hpa pod在同一名称空间上。

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

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