<p>I have a cluster in the Google Kubernetes Engine and want to make one of the deployments auto scalable by memory.</p>
<p>After doing a deployment, I check the horizontal scalation with the following command</p>
<p><code>kubectl describe hpa -n my-namespace</code></p>
<p>With this result:</p>
<pre><code>Name:                                                     myapi-api-deployment
Namespace:                                                my-namespace
Labels:                                                   &lt;none&gt;
Annotations:                                              &lt;none&gt;
CreationTimestamp:                                        Tue, 15 Feb 2022 12:21:44 +0100
Reference:                                                Deployment/myapi-api-deployment
Metrics:                                                  ( current / target )
  resource memory on pods  (as a percentage of request):  &lt;unknown&gt; / 50%
Min replicas:                                             1
Max replicas:                                             5
Deployment pods:                                          1 current / 1 desired
Conditions:
  Type            Status  Reason                   Message
  ----            ------  ------                   -------
  AbleToScale     True    ReadyForNewScale         recommended size matches current size
  ScalingActive   False   FailedGetResourceMetric  the HPA was unable to compute the replica count: failed to get memory utilization: missing request for memory
  ScalingLimited  False   DesiredWithinRange       the desired count is within the acceptable range
Events:
  Type     Reason                   Age                    From                       Message
  ----     ------                   ----                   ----                       -------
  Warning  FailedGetResourceMetric  2m22s (x314 over 88m)  horizontal-pod-autoscaler  failed to get memory utilization: missing request for memory
</code></pre>
<p>When I use the <code>kubectl top</code> command I can see the memory and cpu usage. Here is my deployment including the autoscale:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-api-deployment
  namespace: my-namespace
  annotations:
    reloader.stakater.com/auto: &quot;true&quot;
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-api
      version: v1
  template:
    metadata:
      labels:
        app: my-api
        version: v1
      annotations:
        sidecar.istio.io/rewriteAppHTTPProbers: &quot;true&quot;
    spec:
      serviceAccountName: my-api-sa
      containers:
        - name: esp
          image: gcr.io/endpoints-release/endpoints-runtime:2
          imagePullPolicy: Always
          args: [
              &quot;--listener_port=9000&quot;,
              &quot;--backend=127.0.0.1:8080&quot;,              
              &quot;--service=myproject.company.ai&quot;            
          ]
          ports:
            - containerPort: 9000
        - name: my-api
          image: gcr.io/myproject/my-api:24
          ports:
            - containerPort: 8080
          livenessProbe:
            httpGet:
              path: &quot;/healthcheck&quot;
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: &quot;/healthcheck&quot;
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10         
          resources:
            limits:
              cpu: 500m
              memory: 2048Mi
            requests:
              cpu: 300m
              memory: 1024Mi
---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: my-api-deployment
  namespace: my-namespace
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-api-deployment
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: memory
        target:
          type: &quot;Utilization&quot;
          averageUtilization: 50
---
</code></pre>
<p>Using the autoscaling/v2beta2 recommended by the GKE documentation</p>

seldon851
<p>When using the HPA with memory or CPU, you need to set resource requests for whichever metric(s) your HPA is using. See <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#how-does-a-horizontalpodautoscaler-work" rel="nofollow noreferrer">How does a HorizontalPodAutoscaler work</a>, specifically</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. If a target
raw value is set, the raw metric values are used directly.</p>
</blockquote>
<p>Your HPA is set to match the <code>my-api-deployment</code> which has two containers.  You have resource requests set for <code>my-api</code> but not for <code>esp</code>.  So you just need to add a memory resource request to <code>esp</code>.</p>
