GKE HPA metricSelector:可以按命名空间过滤吗?

问题描述

我最近在同一个 GKE 集群的不同命名空间中部署了我的应用程序的测试版本。我的应用程序使用 GPU 资源,我的 HPA 应该根据 GPU 负载自动扩展。

我注意到,尽管负载为零,但 HPA 正在扩展到最大数量的副本。

原因似乎是 kubernetes.io|container|accelerator|duty_cycle 指标并非特定于命名空间。我的 matchLabels 过滤器针对 api 部署,它存在于两个命名空间(defaultstaging)中。

如何按命名空间另外过滤?我试过 Metadata.namespace 但这不起作用。

这是 HPA:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
Metadata:
  name: api-hpa
  namespace: staging
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api
  minReplicas: 1
  maxReplicas: 3
  metrics:
    - type: External
      external:
        metricName: kubernetes.io|container|accelerator|duty_cycle
        metricSelector:
          matchLabels:
            Metadata.system_labels.top_level_controller_name: api
            Metadata.system_labels.top_level_controller_type: Deployment
        targetAverageValue: "75"a
$ kubectl describe hpa api-hpa
Name:                                                                       api-hpa
Namespace:                                                                  staging
Labels:                                                                     <none>
Annotations:                                                                <none>
CreationTimestamp:                                                          Sun,27 Jun 2021 23:53:33 +0200
Reference:                                                                  Deployment/api
Metrics:                                                                    ( current / target )
  "kubernetes.io|container|accelerator|duty_cycle" (target average value):  65 / 75
Min replicas:                                                               1
Max replicas:                                                               3
Deployment pods:                                                            3 current / 3 desired
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbletoScale     True    ReadyForNewScale    recommended size matches current size
  ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from external metric kubernetes.io|container|accelerator|duty_cycle(&LabelSelector{MatchLabels:map[string]string{Metadata.system_labels.top_level_controller_name:api,Metadata.system_labels.top_level_controller_type: Deployment,},MatchExpressions:[]LabelSelectorRequirement{},})
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range

解决方法

Resourcetype Horizo​​ntalPodAutoscaler 无法直接过滤命名空间。 但是,您始终可以将命名空间作为标签添加到要过滤的资源中。

可以在 the official api documentation 上找到有关指标过滤器的更多信息,它将 metricSelector 指定为 labelSelector