<p>I'm studying kubernetes and testing some example.</p>
<p>I got a problem with applying external metrics to hpa.</p>
<p>i made a external metrics with prometheus adapter.</p>
<p>so I can get external metrics using</p>
<pre><code>kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/
</code></pre>
<p>command.</p>
<p>result is below.</p>
<pre><code>{&quot;kind&quot;:&quot;APIResourceList&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;groupVersion&quot;:&quot;external.metrics.k8s.io/v1beta1&quot;,&quot;resources&quot;:[{&quot;name&quot;:&quot;redis_keys&quot;,&quot;singularName&quot;:&quot;&quot;,&quot;namespaced&quot;:true,&quot;kind&quot;:&quot;ExternalMetricValueList&quot;,&quot;verbs&quot;:[&quot;get&quot;]}]}
</code></pre>
<p>and i can get metrics value using</p>
<pre><code>kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/default/redis_keys
</code></pre>
<p>command.</p>
<p>result is below.</p>
<pre><code>{&quot;kind&quot;:&quot;ExternalMetricValueList&quot;,&quot;apiVersion&quot;:&quot;external.metrics.k8s.io/v1beta1&quot;,&quot;metadata&quot;:{&quot;selfLink&quot;:&quot;/apis/external.metrics.k8s.io/v1beta1/namespaces/default/redis_keys&quot;},&quot;items&quot;:[{&quot;metricName&quot;:&quot;redis_keys&quot;,&quot;metricLabels&quot;:{},&quot;timestamp&quot;:&quot;2020-10-28T08:39:09Z&quot;,&quot;value&quot;:&quot;23&quot;}]}
</code></pre>
<p>and i applied the metric to hpa.</p>
<p>below is hpa configuration.</p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: taskqueue-autoscaler
spec:
  scaleTargetRef:
          #apiVersion: extensions/v1beta1
    apiVersion: apps/v1
    kind: Deployment
    name: taskqueue-consumer
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: External
    external:
      metricName: redis_keys
      targetAverageValue: 20
</code></pre>
<p>after mading hpa,</p>
<p>i tested this command.</p>
<pre><code>kubectl get hpa
</code></pre>
<p>and result is weird.</p>
<pre><code>NAME                   REFERENCE                       TARGETS           MINPODS   MAXPODS   REPLICAS   AGE
taskqueue-autoscaler   Deployment/taskqueue-consumer   11500m/20 (avg)   1         10        2          63m
</code></pre>
<p>i think it's value(11500m) is wrong because result of query value is 23.</p>
<p>Where should i see in this case?</p>

choogwang
<p>Actually it's right, but its also complicated because it gets into a couple of different things with the HPA resource that are not immediately obvious. So I will tackle explaining this one thing at a time.</p>
<p>First, the units of measurement. The Metrics API will try to return whole units when possible, but does return milli-units as well, which can result in the above. Really, what you are seeing is 11.5 converted to 11500m, but the two are the same. Check out this link on the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#appendix-quantities" rel="noreferrer">&quot;Quantities&quot; of HPA metrics</a> that covers it a bit more.</p>
<p>Next, you are seeing two replicas at the moment with a value from the Metrics API of 23. Since you have set the metric to be the AverageValue of the External Metric, it is dividing the value of the metric by the number of replicas in the cluster to result in 11.5 or 11500m when you view the HPA resource. This explains why you are seeing only 2 replicas, while the value of the metric is &quot;above&quot; your threshold. Check out this link on <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-multiple-metrics-and-custom-metrics" rel="noreferrer">Autoscaling with Multiple &amp; Custom metrics, specifically the section about &quot;Object&quot; metrics</a>. And lower down the page they slip in this line regarding External Metrics, confirming why you are seeing the above.</p>
<blockquote>
<p>External metrics support both the Value and AverageValue target types,
which function exactly the same as when you use the Object type.</p>
</blockquote>
<p>Hope this helps and a few tweaks should make it line up better with your expectations. Good luck out there!</p>
