<p>I have the below Horizontal Pod Autoscaller configuration on Google Kubernetes Engine to scale a deployment by a custom metric - <code>RabbitMQ messages ready count</code> for a specific queue: <code>foo-queue</code>.</p>
<p>It picks up the metric value correctly.</p>
<p>When inserting 2 messages it scales the deployment to the maximum 10 replicas.
I expect it to scale to 2 replicas since the targetValue is 1 and there are 2 messages ready.</p>
<p>Why does it scale so aggressively?</p>
<p>HPA configuration:</p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: foo-hpa
  namespace: development
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: foo
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: External
    external:
      metricName: &quot;custom.googleapis.com|rabbitmq_queue_messages_ready&quot;
      metricSelector:
        matchLabels:
          metric.labels.queue: foo-queue
      targetValue: 1
</code></pre>

<p>According to <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/</a></p>

<p>From the most basic perspective, the Horizontal Pod Autoscaler controller operates on the ratio between desired metric value and current metric value:</p>

<pre><code>desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
</code></pre>

<p>From the above I understand that as long as the queue has messages the k8 HPA will continue to scale up since <code>currentReplicas</code> is part of the <code>desiredReplicas</code> calculation.</p>

<p>For example if:</p>

<p><code>currentReplicas</code> = 1</p>

<p><code>currentMetricValue</code> / <code>desiredMetricValue</code> = 2/1</p>

<p>then:</p>

<p><code>desiredReplicas</code> = 2</p>

<p>If the metric stay the same in the next hpa cycle <code>currentReplicas</code> will become 2 and <code>desiredReplicas</code> will be raised to 4</p>
