<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: backend-hpa
spec:
  maxReplicas: 10
  minReplicas: 3
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: backend
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 1
        periodSeconds: 100
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Pods
        value: 2
        periodSeconds: 35
</code></pre>
<h3>I'm confused about how memory utilization is calculated? The 70% is based of what threshold value?</h3>
<pre><code>➜ kd hpa       
Name:                                                     prod-backend-hpa-v1
Namespace:                                                prod
Labels:                                                   argocd.argoproj.io/instance=ccl-backend-prod
Annotations:                                              &lt;none&gt;
CreationTimestamp:                                        Mon, 13 Sep 2021 17:39:44 -0700
Reference:                                                Deployment/prod-backend-v1
Metrics:                                                  ( current / target )
  resource memory on pods  (as a percentage of request):  31% (85408426666m) / 70%
  resource cpu on pods  (as a percentage of request):     0% (1m) / 70%
Min replicas:                                             3
Max replicas:                                             10
Behavior:
  Scale Up:
    Stabilization Window: 60 seconds
    Select Policy: Max
    Policies:
      - Type: Pods  Value: 2  Period: 35 seconds
  Scale Down:
    Stabilization Window: 300 seconds
    Select Policy: Max
    Policies:
      - Type: Pods  Value: 1  Period: 100 seconds
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 memory resource utilization (percentage of request)
  ScalingLimited  True    TooFewReplicas    the desired replica count is less than the minimum replica count
Events:           &lt;none&gt;

</code></pre>
<p>so how is this</p>
<pre><code>resource memory on pods  (as a percentage of request):  31% (85408426666m) / 70%
</code></pre>
<p>calculated?</p>
<p>My deployment has the following requests/limits set</p>
<pre><code>  Containers:
   backend:
    Port:       8080/TCP
    Host Port:  0/TCP
    Command:
      web
    Limits:
      cpu:     1
      memory:  1Gi
    Requests:
      cpu:      200m
      memory:   256Mi

</code></pre>
<p>my current pods</p>
<pre><code>➜ k top po
W0914 13:38:13.793286 3006942 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
NAME                              CPU(cores)   MEMORY(bytes)   
prod-backend-v1-7858bddc4-5t8r5   1m           80Mi            
prod-backend-v1-7858bddc4-67llh   1m           81Mi            
prod-backend-v1-7858bddc4-98wj2   1m           82Mi 
</code></pre>
<p>so it seems that 31% is calculated as <code>81/256 ~31%.</code></p>
<h3>But is this correct to do?</h3>
<p>I would think it would be logically correct to expect <code>81/LIMIT = 81/1024 = ~8%.</code></p>

DmitrySemenov
<h3>type=Utilization &amp;&amp; <code>averageUtilization:70</code></h3>
<p>is the target value of the average of the resource metric across all relevant pods, represented as a <strong>percentage of the requested value</strong> of the resource for the pods. Currently only valid for Resource metric source type</p>
<h3>type=AverageValue &amp;&amp; <code>averageValue: 500Mi</code></h3>
<p>averageValue is the target value of the average of the metric across all relevant pods (as a quantity)</p>
<p>so my memory metric for HPA turned out to become:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: backend-hpa
spec:
  maxReplicas: 10
  minReplicas: 3
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: backend
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: AverageValue
        averageValue: 500Mi # averageValue is the target value of the average of the metric across all relevant pods (as a quantity), https://www.pulumi.com/docs/reference/pkg/kubernetes/autoscaling/v2beta2/horizontalpodautoscalerlist/#metrictarget
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70 # represented as a percentage of the requested value of the resource for the pods.
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 1
        periodSeconds: 100
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Pods
        value: 2
        periodSeconds: 35
</code></pre>
<p>More info:
<a href="https://www.pulumi.com/docs/reference/pkg/kubernetes/autoscaling/v2beta2/horizontalpodautoscalerlist/#metrictarget" rel="nofollow noreferrer">https://www.pulumi.com/docs/reference/pkg/kubernetes/autoscaling/v2beta2/horizontalpodautoscalerlist/#metrictarget</a></p>
