<p>I am trying to determine a reliable setup to use with K8S to scale one of my deployments using an HPA and an autoscaler.  I want to minimize the amount of resources overcommitted but allow it to scale up as needed.</p>
<p>I have a deployment that is managing a REST API service.  Most of the time the service will have very low usage (0m-5m cpu).  But periodically through the day or week it will spike to much higher usage on the order of 5-10 CPUs (5000m-10000m).</p>
<p>My initial pass as configuring this is:</p>
<ul>
<li>Deployment: 1 replica</li>
</ul>
<pre><code>&quot;resources&quot;: {
   &quot;requests&quot;: {
     &quot;cpu&quot;: 0.05
   },
   &quot;limits&quot;: {
      &quot;cpu&quot;: 1.0
   }
}
</code></pre>
<ul>
<li>HPA:</li>
</ul>
<pre><code>&quot;spec&quot;: {
   &quot;maxReplicas&quot;: 25,
   &quot;metrics&quot;: [
      {
         &quot;resource&quot;: {
         &quot;name&quot;: &quot;cpu&quot;,
         &quot;target&quot;: {
            &quot;averageValue&quot;: 0.75,
            &quot;type&quot;: &quot;AverageValue&quot;
         }
         },
         &quot;type&quot;: &quot;Resource&quot;
      }
   ],
   &quot;minReplicas&quot;: 1,
   ...
}
</code></pre>
<p>This is running on an AWS EKS cluster with autoscaler running.  All instances have 2 CPUs. The goal is that as the CPU usage goes up the HPA will allocate a new pod that will be unschedulable and then the autoscaler will allocate a new node.  As I add load on the service, the CPU usage for the first pod spikes up to approximately 90-95% at max.</p>
<p>I am running into two related problems:</p>
<ol>
<li>Small request size</li>
</ol>
<p>By using such a small request value (cpu: 0.05), the newly requested pods can be easily scheduled on the current node even when it is under high load.  Thus the autoscaler never find a pod that can't be scheduled and doesn't allocate a new node.  I could increase the small request size and overcommit, but this then means that for the vast majority of the time when there is no load I will be wasting resources I don't need.</p>
<ol start="2">
<li>Average CPU reduces as more pods are allocated</li>
</ol>
<p>Because the pods all get allocated on the same node, once a new pod is allocated it starts sharing the node's available 2 CPUs.  This in turn reduces the amount of CPU used by the pod and thus keeps the average value below the 75% target.</p>
<p>(ex: 3 pods, 2 CPUs ==&gt; max 66% Average CPU usage per pod)</p>
<p>I am looking for guidance here on how I should be thinking about this problem.  I think I am missing something simple.</p>
<p>My current thought is that what I am looking for is a way for the Pod resource request value to increase under heavier load and then decrease back down when the system doesn't need it.  That would point me toward using something like a VPA, but everything I have read says that using HPA and VPA at the same time leads to <em>very bad things</em>.</p>
<p>I think increasing the request from 0.05 to something like 0.20 would probably let me handle the case of scaling up.  But this will in turn waste a lot of resources and could suffer issues if the scheduler find space on an existing pod.  My example is about one service but there are many more services in the production deployment.  I don't want to have nodes sitting empty with committed resources but no usage.</p>
<p>What is the best path forward here?</p>

<p>Sounds like you need a Scheduler that take actual CPU utilization into account. This is not supported yet.</p>
<p>There seem to be work on a this feature: <strong><a href="https://github.com/kubernetes-sigs/scheduler-plugins/tree/6b7e77af527d8db82afb5060e5474ed524bdc0d6/kep/61-Trimaran-real-load-aware-scheduling" rel="nofollow noreferrer">KEP - Trimaran: Real Load Aware Scheduling</a></strong> using <a href="https://github.com/kubernetes-sigs/scheduler-plugins/pull/115" rel="nofollow noreferrer">TargetLoadPackin plugin</a>. Also see <a href="https://github.com/kubernetes/kubernetes/issues/73269" rel="nofollow noreferrer">New scheduler priority for real load average and free memory</a>.</p>
<p>In the meanwhile, if the CPU limit is 1 Core, and the Nodes autoscale under high CPU utilization, it sounds like it should work <em>if the nodes is substantially bigger than the CPU limits for the pods</em>. E.g. try with nodes that has 4 Cores or more and possibly slightly larger <em>CPU request</em> for the Pod?</p>
