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

GKE HPA metricSelector: possible to filter by namespace?

我最近在同一 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 上找到有关 metrics-filter 的更多信息,它将 metricSelector 指定为 labelSelector