<p>we are using EKS cluster and Helm chart for deployment. Helm charts contains the template YANL files for CPU and Memory.
here is the YANL file</p>
<pre><code>apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-{{ .Release.Name }}-memory
  namespace: {{ .Release.Namespace }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Release.Name }}
  minReplicas: 2
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 200
  behavior:
    scaleDown:
      policies:
      - periodSeconds: 60
        type: Pods
        value: 1
      - periodSeconds: 30
        type: Percent
        value: 30
      selectPolicy: Min
      stabilizationWindowSeconds: 120
    scaleUp:
      policies:
      - periodSeconds: 30
        type: Pods
        value: 1
      - periodSeconds: 30
        type: Percent
        value: 200
      selectPolicy: Max
      stabilizationWindowSeconds: 60
</code></pre>
<p>when we run the <code>kubectl get hpa</code> command</p>
<pre><code>root@SX-In:~# kubectl get hpa -A
NAMESPACE         NAME                                        REFERENCE                                   TARGETS     MINPODS   MAXPODS   REPLICAS   AGE
test1   hpa-abc-cpu      Deployment/abc   2%/200%     2         4         2          23d
test2   hpa-abc-memory   Deployment/abc   183%/200%   2         4    
</code></pre>
<p><code>kubectl top pods</code> shows below</p>
<pre><code>root@SX-In:~# kubectl top pods -n test1
NAME                          CPU(CORES)    MEMORY(BYTES)
abcp1-78bb67d47f-lkwwr         5m           1148Mi
abcp1-78bb67d47f-vd26b         5m           917Mi
</code></pre>
<p>I read the docs but did not understand how this <strong>183</strong>% in memory gets calculated . can someone please explain.</p>
<p>I tried searching formula for same but did not get</p>
<p>The machine size where pods are deployed is t2.xlarge(4 CPU , 16 GB RAM)</p>
<p>Please suggest</p>

user2315104
<p>The percentage is the ratio of the Pod's actual memory usage to its resource requests.  From the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#how-does-a-horizontalpodautoscaler-work" rel="nofollow noreferrer">Kubernetes HPA documentation</a>:</p>
<blockquote>
<p>For per-pod resource metrics (like CPU), the controller fetches the metrics from the resource metrics API for each Pod targeted by the HorizontalPodAutoscaler. Then, if a target utilization value is set, the controller calculates the utilization value as a percentage of the equivalent resource request on the containers in each Pod.</p>
</blockquote>
<p>I can't exactly reproduce the 183% number, but I can come close.  Let's say your Deployment specifies that your Pods request 512 MiB of memory, with a hard limit of 2048 MiB.  The actual memory (1148, 917 MiB) gets divided by the resource request to get a percentage (224%, 179%), and then those percentages get averaged across the Pods (202% in this specific calculation).</p>
<p>That percentage then gets fed into the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details" rel="nofollow noreferrer">HPA formula</a> to compute the new target <code>replicas:</code> value.</p>
<p>Note that it looks like you have two HPAs trying to manage the same Deployment.  This could be problematic in an example like what you show, where you have relatively high memory but low CPU: the memory autoscaler could want to scale up, but the CPU autoscaler would want to scale down to its minimum.  You can <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-multiple-metrics-and-custom-metrics" rel="nofollow noreferrer">attach multiple metrics to a single autoscaler</a> and that would be a better setup.</p>
