<p>I have two deployments running a v1 and v2 of the same service in istio. I have set up a custom-metric 'istio-total-requests' to be collected through prometheus adapater.</p>
<p>I have set up an HPA to scale the v2 deployment and can see the target value increasing when I send requests, but what is not happening is the HPA is not scaling the number of pods/replicas.</p>
<p>I am running kubernetes v1.19 on minikube v1.13.1, and cannot understand why its not scaling.</p>
<pre><code>prometheus:
  url: http://prometheus.istio-system.svc.cluster.local
rules:
  default: false
  custom:
# this rule matches cumulative cAdvisor metrics measured in seconds
  - seriesQuery: 'istio_requests_total{kubernetes_namespace!=&quot;&quot;,kubernetes_pod_name!=&quot;&quot;}'
    seriesFilters: []      
    resources:
      # template: &lt;&lt;.Resource&gt;&gt;
      # skip specifying generic resource&lt;-&gt;label mappings, and just
      # attach only pod and namespace resources by mapping label names to group-resources
      overrides:
        kubernetes_namespace: {resource: &quot;namespace&quot;}
        kubernetes_pod_name: {resource: &quot;pod&quot;}
    # specify that the `container_` and `_seconds_total` suffixes should be removed.
    # this also introduces an implicit filter on metric family names
    name:
      # we use the value of the capture group implicitly as the API name
      # we could also explicitly write `as: &quot;$1&quot;`
      matches: &quot;^(.*)_total&quot;
      as: &quot;${1}_per_second&quot;
      # matches: &quot;&quot;
      # as: &quot;&quot;
    # specify how to construct a query to fetch samples for a given series
    # This is a Go template where the `.Series` and `.LabelMatchers` string values
    # are available, and the delimiters are `&lt;&lt;` and `&gt;&gt;` to avoid conflicts with
    # the prometheus query language
    metricsQuery: &quot;sum(rate(&lt;&lt;.Series&gt;&gt;{&lt;&lt;.LabelMatchers&gt;&gt;}[1m])) by (&lt;&lt;.GroupBy&gt;&gt;)&quot;
</code></pre>
<p>HPA YAML</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: translate-deployment-v2-hpa
spec:
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Pods
    pods:
      metric:
        name: istio_requests_per_second
        # selector: {matchLabels: {destination_version: 0.0.2}}
      target:
        type: AverageValue
        averageValue: 10
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: translate-deployment-v2      
</code></pre>
<p>See the HPA pulling and measuring the metric but not scaling, the window below it shows the prometheus-adapter getting queried for the metric successfully.</p>
<p><a href="https://i.stack.imgur.com/8PlF3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8PlF3.png" alt="enter image description here" /></a></p>
<p>HPA Description</p>
<p><a href="https://i.stack.imgur.com/fk0a4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fk0a4.png" alt="enter image description here" /></a></p>
<p>One final item which I'm not clear on, is what is the purpose of the selector in the hpa definition above ? Is it to select specific values from the data range which is queried by prometheus ?</p>
<p>For example I know the field I'm querying is output by default by envoy as follows:</p>
<pre><code>istio_requests_total{app=&quot;istio-ingressgateway&quot;,chart=&quot;gateways&quot;,connection_security_policy=&quot;unknown&quot;,destination_app=&quot;translate-pod&quot;,destination_canonical_revision=&quot;0.0.1&quot;,destination_canonical_service=&quot;translate-pod&quot;,destination_principal=&quot;spiffe://cluster.local/ns/default/sa/default&quot;,destination_service=&quot;translate-service.default.svc.cluster.local&quot;,destination_service_name=&quot;translate-service&quot;,destination_service_namespace=&quot;default&quot;,destination_version=&quot;0.0.1&quot;,destination_workload=&quot;translate-deployment&quot;,destination_workload_namespace=&quot;default&quot;,heritage=&quot;Tiller&quot;,install_operator_istio_io_owning_resource=&quot;unknown&quot;,instance=&quot;172.17.0.5:15020&quot;,istio=&quot;ingressgateway&quot;,istio_io_rev=&quot;default&quot;,job=&quot;kubernetes-pods&quot;,kubernetes_namespace=&quot;istio-system&quot;,kubernetes_pod_name=&quot;istio-ingressgateway-6cfd75fc57-flmhp&quot;,operator_istio_io_component=&quot;IngressGateways&quot;,pod_template_hash=&quot;6cfd75fc57&quot;,release=&quot;istio&quot;,reporter=&quot;source&quot;,request_protocol=&quot;http&quot;,response_code=&quot;200&quot;,response_flags=&quot;-&quot;,service_istio_io_canonical_name=&quot;istio-ingressgateway&quot;,service_istio_io_canonical_revision=&quot;latest&quot;,source_app=&quot;istio-ingressgateway&quot;,source_canonical_revision=&quot;latest&quot;,source_canonical_service=&quot;istio-ingressgateway&quot;,source_principal=&quot;spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account&quot;,source_version=&quot;unknown&quot;,source_workload=&quot;istio-ingressgateway&quot;,source_workload_namespace=&quot;istio-system&quot;}
</code></pre>
<p>Does the selector allow you to further filter the series data, if not what is the purpose and how do you use it ?</p>

<p>According to your screenshot, HPA works as expected because your metric value is lower than the threshold. If the value does not go over your threshold value, HPA will not trigger the scale up. Instead, it may trigger the scale down in your case.</p>
<p>The metric you are using right now is <code>istio_requests_per_second</code>. That is calculated by the total request per second. The first screenshot show the average value is <code>200m</code>, which will be <code>0.2</code>. Your threshold is <code>10</code> so HPA definitely would not scale up in this case.</p>
<p>For the selector, it gives you the ability to select your target label under the metrics. For example, if you want to scale the instances against the <code>GET</code> method only. You can define the selector matching the <code>GET</code> method label in this case.</p>
