<p>What is the difference between replicas and HPA?</p>
<p>For sample, below deployment is configured with 3 replicas</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  **replicas: 3**
</code></pre>
<p>and the below HPA with 2-20 replicas</p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: hello
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hello
  **minReplicas: 2
  maxReplicas: 20
metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80**
</code></pre>
<p>does it mean that the above HPA will control the overall number of replicas irrespective of what is defined in the &quot;deployment.yaml&quot;? When HPA scales up would it add one more &quot;deployment&quot; replica or three more &quot;deployment&quot; replicas?</p>

One Developer
<p>It is important to add to previous answer that deployment <code>spec.replicas</code> and HPA <code>spec.minReplicas</code> might be conflicting. When both configured some unexpected behaviour might arise.</p>
<p>If there is an HPA, it manages the amount of replicas according to it's settings. But while deployment is under control of an HPA, if you apply deployment config with set amount of replicas, it would override current desired amount of replicas and might scale your deployment unexpectedly.</p>
<p>For example if you have deployment <code>spec.replicas</code> set to 1, and HPA currently scaled your deployment to 5 replicas, when you apply deployment config it would set amount of desired replicas to 1 and immediately scale down your deployment. Then HPA takes back control, changes desired amount of replicas back to 5 and scales it up again.
<a href="https://i.stack.imgur.com/PQsZf.png" rel="nofollow noreferrer">Here is how this issue looks on my Grafana dashboard that tracks amount of running replicas</a></p>
<p>More on the topic:</p>
<ul>
<li><a href="https://technologyconversations.com/2018/10/10/to-replicas-or-not-to-replicas-in-kubernetes-deployments-and-statefulsets/" rel="nofollow noreferrer">Blog post about the problem.</a></li>
<li><a href="https://github.com/kubernetes/kubernetes/issues/25238#issuecomment-917230268" rel="nofollow noreferrer">Problem explained in Kubernetes GitHub issue.</a></li>
</ul>
