<p>I am trying to horizontally autoscale a workload not only by custom metrics but also by algorithm that differs from algorithm described <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details" rel="nofollow noreferrer">here</a></p>

<p>1/ is that possible?</p>

<p>2/ if it is not, and assuming i don't mind creating a container that does the autoscaling for me instead of HPA, what API should i call to do the equivalent of <code>kubectl scale deployments/&lt;name&gt; --replicas=&lt;newDesired&gt;</code> ?</p>

<p>here's the use-case:</p>

<p>1/ the workload consumes a single request from a queue, handles them, when done removes the item it handled, and consumes the next message.</p>

<p>2/ when there's more than 0 messages ready - I'd like to scale up to the number of messages ready (or max scale if it is larger).
when there's 0 messages being processed - i'd like to scale down to 0.</p>

<p>getting the messages ready/ messages being processed to metrics server is not an issue.</p>

<p>getting HPA to scale by "messages ready" is not an issue either.</p>

<p>but...</p>

<p>HPA algorithm scales gradually... 
when i place 10 items in queue - it first to 4, then to 8 then to 10.</p>

<p>it also scales down gradually, and when it scales down it can terminate a pod that was processing - thus increasing the "ready" and causing a scale-up.</p>

<p>a node.js code that i would have run had i known the api to call (intead of HPA):</p>

<pre><code>    let desiredToSet = 0;
    if (!readyMessages &amp;&amp; !processingMessages) {
      //if we have nothing in queue and all workers completed their work - we can scale down to minimum
      //we like it better than reducing slowly as this way we are not risking killing a worker that's working
      desiredToSet = config.minDesired;
    }
    else {
      //messages ready in the queue, increase number of workers up to max allowed
      desiredToSet = Math.max(Math.min(readyMessages + processingMessages, config.maxDesired), currentDeploymentReplicas);
    }
    //no point in sending a request to change, if nothing changed
    if (desiredToSet !== currentDeploymentReplicas) {
      &lt;api to set desiredToSet of deployment to come here&gt;;
    }
</code></pre>

<p>1) I don't think it's possible. The HPA controller is built-into Kubernetes and I don't think its algorithm can be extended/replaced.</p>

<p>2) Yes, you can create a custom controller that does the job of the HPA with your own algorithm. To scale the Deployment up and down through the Kubernetes API, you <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#replace-scale-deployment-v1-apps" rel="nofollow noreferrer">manipulate the Scale sub-resource</a> of the Deployment.</p>

<p>Concretely, to scale the Deployment to a new number of replicas, you make the following request:</p>

<pre><code>PUT /apis/apps/v1/namespaces/{namespace}/deployments/{name}/scale
</code></pre>

<p>With a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#scale-v1-autoscaling" rel="nofollow noreferrer">Scale</a> resource (containing the desired replica count) as a body argument, as described in the API reference.</p>
