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

时间:2019-01-09 09:47:23

标签: kubernetes prometheus

我已经设置了Prometheus适配器,以便能够将Prometheus指标用作kubernetes中的自定义指标,但是在使用对象HPA自动扩展部署方面存在问题。

这是我从自定义指标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"
    }
  ]
```

然后是我的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
```

似乎我可以通过直接调用api来获取指标,但HPA无法检索指标。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

当我遇到类似的问题时,我的问题是我尝试扩展的部署不是产生我所依赖的指标的那个。
就我而言,我想扩展一个吊舱,这是对我有用的

  1. 我向我保证,尝试扩展的吊舱是部署的一部分。
  2. 我保证该Pod可以在特定端口上公开所需的指标,而Prometheus会使用注释将其删除。
  3. 我保证该部署与Hpa pod处于同一名称空间。