如何为类型配置自动缩放:对象

时间:2018-10-09 18:32:57

标签: kubernetes autoscaling

我配置了一个自定义指标。

当我跑步时:

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/my-namespace/deployments.extensions/sample-app/queue_length" | jq

我知道了

...
  "items": [
    {
      "describedObject": {
        "kind": "Deployment",
        "namespace": "my-namespace",
        "name": "sample-app",
        "apiVersion": "extensions/v1beta1"
      },
      "metricName": "queue_length",
      "timestamp": "2018-10-09T18:14:15Z",
      "value": "30"
    }
  ]
...

因此,我想使用此指标扩展部署:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
  name: sample-app
  namespace: my-namespace
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: sample-app
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Object
    object:
      metricName: queue_length
      target:
        apiVersion: extensions/v1beta1
        kind: Deployment
        name: sample-app
      targetValue: 20

但是它似乎不起作用,也许我必须指定- type: Object有点不同。

我尝试将kind更改为Deployments,Deployments.extension,Deployments.extensions等,但我不断收到错误FailedGetScale

更新。:我发现HPA对象和目标对象的名称空间应该相同,因此我对其进行了更改。现在我得到

the HPA was unable to compute the replica count: unable to get metric queue_length: Deployment on default sample-app/unable to fetch metrics from custom metrics API: the server could not find the metric queue_length for deployments.extensions sample-app

Upd2。:弄清楚:hpa在命名空间default中,而指标暴露在my-namespace中。因此,应谨慎使用名称空间!

1 个答案:

答案 0 :(得分:0)

尝试一下:

---
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
  name: sample-app
  namespace: my-namespace
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: sample-app
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Object
    object:
      target:
        kind: Service
        name: sample-app
      metricName: queue_length
      targetValue: 20