<p>I am trying to implement autoscaling of pods in my cluster. I have tried with a "dummy" deployment and hpa, and I didn't have problem. Now, I am trying to integrate it into our "real" microservices and it keeps returning </p>

<pre><code>Conditions:
  Type           Status  Reason                   Message
  ----           ------  ------                   -------
  AbleToScale    True    SucceededGetScale        the HPA controller was able to get the target's current scale
  ScalingActive  False   FailedGetResourceMetric  the HPA was unable to compute the replica count: missing request for memory
Events:
  Type     Reason                        Age                   From                       Message
  ----     ------                        ----                  ----                       -------
  Warning  FailedGetResourceMetric       18m (x5 over 19m)     horizontal-pod-autoscaler  unable to get metrics for resource memory: no metrics returned from resource metrics API
  Warning  FailedComputeMetricsReplicas  18m (x5 over 19m)     horizontal-pod-autoscaler  failed to get memory utilization: unable to get metrics for resource memory: no metrics returned from resource metrics API
  Warning  FailedComputeMetricsReplicas  16m (x7 over 18m)     horizontal-pod-autoscaler  failed to get memory utilization: missing request for memory
  Warning  FailedGetResourceMetric       4m38s (x56 over 18m)  horizontal-pod-autoscaler  missing request for memory
</code></pre>

<p>Here is my hpa:</p>

<pre><code>
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
 name: #{Name}
 namespace: #{Namespace}
spec:
 scaleTargetRef:
   apiVersion: apps/v1beta1
   kind: Deployment
   name: #{Name}
 minReplicas: 2
 maxReplicas: 5
 metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 80
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
</code></pre>

<p>The deployment</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: #{Name}
  namespace: #{Namespace}
spec:
  replicas: 2
  selector:
    matchLabels:
      app: #{Name}
  template:
    metadata:
      annotations:
        linkerd.io/inject: enabled
      labels:
        app: #{Name}
    spec:
      containers:
      - name: #{Name}
        image: #{image}
        resources:
          limits:
            cpu: 500m
            memory: "300Mi"
          requests:
            cpu: 100m
            memory: "200Mi"
        ports:
        - containerPort: 80
          name: #{ContainerPort}
</code></pre>

<p>I can see both memory and cpu when I do <code>kubectl top pods</code>.  I can see the requests and limits as well when I do <code>kubectl describe pod</code>. </p>

<pre><code>    Limits:
      cpu:     500m
      memory:  300Mi
    Requests:
      cpu:     100m
      memory:  200Mi
</code></pre>

<p>The only difference I can think of is that my dummy service didn't have linkerd sidecar. </p>

<p>For the HPA to work with resource metrics, <em>every</em> container of the Pod needs to have a request for the given resource (CPU or memory).</p>

<p>It seems that the Linkerd sidecar container in your Pod does not define a memory request (it might have a CPU request). That's why the HPA complains about <code>missing request for memory</code>.</p>

<p>However, you can configure the memory and CPU requests for the Linkerd container with the <code>--proxy-cpu-request</code> and <code>--proxy-memory-request</code> <a href="https://linkerd.io/2/reference/cli/inject/" rel="noreferrer">injection flags</a>.</p>

<p>Another possibility is to use <a href="https://linkerd.io/2/reference/proxy-configuration/" rel="noreferrer">these annotations</a> to configure the CPU and memory requests:</p>

<ul>
<li><code>config.linkerd.io/proxy-cpu-request</code></li>
<li><code>config.linkerd.io/proxy-memory-request</code></li>
</ul>

<p>Defining a memory request with in either of these ways should make the HPA work.</p>

<p><strong>References:</strong></p>

<ul>
<li>Linkerd issue initially reported <a href="https://github.com/linkerd/linkerd2/issues/1480" rel="noreferrer">in this issue</a> and fixed in <a href="https://github.com/linkerd/linkerd2/pull/1731" rel="noreferrer">this pull request</a></li>
<li>In <a href="https://github.com/linkerd/linkerd2/blob/master/CHANGES.md#stable-230" rel="noreferrer">Linkerd 2.3.0</a>, <code>--proxy-cpu-request</code> and <code>--proxy-memory-request</code> replaced <code>--proxy-cpu</code> and <code>--proxy-memory</code></li>
<li>There was a similar issue with the <a href="https://github.com/istio/istio/issues/13977" rel="noreferrer">Istio sidecar</a>, but it seems to have been fixed too</li>
</ul>
