kubernetes 如何使HPA基于另一个部署产生的度量来扩展部署

7eumitmz  于 2023-04-29  发布在  Kubernetes
关注(0)|答案(2)|浏览(162)

我试图实现的是创建一个Horizontal Pod Autoscaler,它能够根据controller Pod生成的自定义度量来缩放worker Pod。
我已经让Prometheus scraping、Prometheus Adapater、Custom Metric Server完全运行,并使用worker pod生成的自定义指标my_controller_metric扩展worker部署。
现在我的worker pod不再产生这个指标,但controller可以。似乎API autoscaling/v1不支持此功能。如果需要的话,我可以使用autoscaling/v2 beta1 API来指定HPA。
以下是我对这个HPA的规格:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: my-worker-hpa
  namespace: work
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: my-worker-deployment
  metrics:
  - type: Object
    object:
      target:
        kind: Deployment
        name: my-controller-deployment
      metricName: my_controller_metric
      targetValue: 1

当配置应用于kubectl apply -f my-worker-hpa.yml时,我得到的消息是:

horizontalpodautoscaler "my-worker-hpa" configured

虽然这条消息看起来没问题,但HPA不起作用。该规范是否畸形?
正如我所说的,该指标在自定义指标服务器中可用,该指标具有kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep my_controller_metric
这是HPA发出的错误消息:

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 my_controller_metric: Deployment on work my-controller-deployment/unable to fetch metrics from custom metrics API: the server could not find the metric my_controller_metric for deployments

谢谢!

a0zr77ik

a0zr77ik1#

在您的案例中,问题是HPA配置:spec.metrics.object.target还应指定API版本。将apiVersion: extensions/v1beta1放在spec.metrics.object.target下面应该可以解决这个问题。
此外,还有一个关于HPA中更好的配置验证的未决问题:https://github.com/kubernetes/kubernetes/issues/60511

mwkjh3gx

mwkjh3gx2#

我有一个完全相同的用户案例,我有一个worker pod,需要在master pod上进行扩展。我无法理解这个问题,因为type: Pods从它正在扩展的pod(又名worker pod)获取指标。我很高兴偶然地发现这个问题,却发现它是开放的。
然而,它确实激励我找到了解决方案。
长话短说,基于名称空间度量的扩展。我用的是Helm Charts,但想法是一样的:
Prometheus适配器:

prometheus-adapter:
  rules:
    default: false
    custom:
      - seriesQuery: ' foobar'
        resources:
          overrides:
            pod:
              resource: pod
            namespace:
              resource: namespace
        name:
          matches: ^(.*)
          as: "metricName"
        metricsQuery: barfoo

HPA:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: name-hpa
  labels:
      {{- include "raw-ds.labels" $global | nindent 4 }}
    app: {{ $deployName }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ $deployName }}
  minReplicas: {{ $.Values.autoscaling.minReplicas }}
  maxReplicas: {{ $.Values.autoscaling.maxReplicas }}
  metrics:
    - type: Object
      object:
        describedObject:
          kind: Namespace
          name: default
          apiVersion: v1
        metric:
          name:  metricName
        target:
          type: Value
          value: 2k

相关问题