How to "OR"-match a metricSelector in a HorizontalPodAutoscaler based on external metric

9/27/2019

First off to avoid x-y questions: I'd like to achieve HPA based on loadbalancer metrics coming from a specific ingress.

I have a HPA based on a request_count external metric coming from stackdriver. I want to match the label in the following way, in order to get only the requests coming to a specific ingress.

...HPAmanifest...
- type: External
    external:
      metricName: loadbalancing.googleapis.com|https|request_count
      metricSelector:
        matchLabels:
          resource.labels.target_proxy_name: k8s-tps-appname-appname-ingress--64658eaf6b9dce83
      targetAverageValue: 50

The problem is that by doing that, I neglect HTTP traffic (which does happen and it's beyond our power to prevent that), which means that my app won't scale if there's a huge number of HTTP requests.

If I am not mistaken, expressing something similar makes the HPA catch metrics containing BOTH labels, which means that no metric will be matched at all. (note, the only difference between them is the "s" after "tp", which distinguishes traffic coming from http/s)

...HPAmanifest...
- type: External
    external:
      metricName: loadbalancing.googleapis.com|https|request_count
      metricSelector:
        matchLabels:
          resource.labels.target_proxy_name: k8s-tps-appname-appname-ingress--64658eaf6b9dce83
          resource.labels.target_proxy_name: k8s-tp-appname-appname-ingress--64658eaf6b9dce83
      targetAverageValue: 50

Is it possible to use a "or" match, (or a regex alternatively) in order to achieve HPA based on loadbalancer metrics coming from a specific ingress?

NB: I do not filter on label url-map because on the stackdriver web UI I see that the graphs are not matching for some reason (I'll give better looks in the meanwhile)

Thanks

-- linuxbandit
google-kubernetes-engine
kubernetes

2 Answers

10/10/2019

The public doc says the following.

Autoscaling based on multiple metrics

You can use multiple metrics with a single HorizontalPodAutoscaler,combining External metrics with other metric types described in Autoscaling Deployments with Custom Metrics. To specify each metric you want to use as a separate entry in metrics list in your HPA object specification. HPA will calculate the number of replicas based on each metric and pick the highest one.

For More options please refer to links[2],[3]

[2]https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/ [3]https://stupefied-goodall-e282f7.netlify.com/contributors/design-proposals/autoscaling/hpa-external-metrics/

-- Ali Reza Izadi
Source: StackOverflow

2/13/2020

I am not sure exactly what went wrong back then, though the answer is exactly what I have ruled out (as per my original question): in order to filter the requests arriving to a specific ingress, one must filter on label url-map

So my working HPA is now having in the matchLabels:

...
      metricSelector:
        matchLabels:
          resource.labels.url_map_name: k8s-tps-appname-appname-ingress--345afb364

and it doesn't cause problems of using an "OR" selector as per my question here. Notice that the ingress annotation is url-map with hyphens, which is "transformed" in url_map_name (with underscores); this happens with all others annotations too (in my original question, target-proxy as ingress annotation becomes target_proxy_name in the label

For future readers: you can't OR the conditions in a matchLabel currently (1.15) in k8s.

-- linuxbandit
Source: StackOverflow