Skip to main content
A workload that declares a replica range instead of a fixed count lets the cluster add pods when the service is busy and remove them when it is not. You write the range on the workload, and forge renders a Kubernetes HorizontalPodAutoscaler (HPA) beside its Deployment. This page is about workloads that run on a cluster you operate. Reliant hosting does not apply an autoscaled workload yet, and forge env deploy says so before it changes anything. Autoscaling needs forge v1.12.0 or later.

Declare a range

Add autoscale to the workload where it is bound to your cluster. In a scaffolded project that is the _api workload in deploy/kcl/prod/main.k, once its line in _workloads uses _on_cluster instead of _hosted (see Environments for the binders):
The scaffold’s cluster binder sets a capacity floor of three replicas through _cluster_replicas, which collides with autoscale: render fails with replicas = 3 is set beside autoscale. Set _cluster_replicas = 1 in the same file so the autoscaler owns the count. Render the environment and look at what forge will apply:
terminal
The output is an autoscaling/v2 HPA named after the workload (api), with minReplicas: 2, maxReplicas: 6 and a CPU target of 70 percent. The Deployment it scales no longer states spec.replicas, because the HPA writes that field and a second writer would undo it on every apply.

What the fields mean

The metric is CPU utilization across the whole pod: the sum of every container’s use against the sum of every container’s request, so a sidecar counts against the request that covers it. The target stops at 95 because a CPU limit equal to the request, which is the default, caps utilization near 100 percent. A higher target is one the pods can never exceed, and the workload would never scale up. Because the target is a percent of the request, the CPU request is the real dial. A pod that requests 250m and is held at 70 percent scales out when it averages 175m. If a workload is always idle against a generous request, no amount of traffic will move it. Set resources on the workload to match what one pod can comfortably do, then let the range handle the rest. The default entry shape requests 250m of CPU and 1 GiB of memory.

How it scales

forge states the scaling behavior in the HPA instead of leaving it to the cluster’s defaults, so a later change to Kubernetes’ defaults cannot change how a running workload scales while nothing in your declaration moved. Scaling up has no waiting period. Every 60 seconds the autoscaler may double the pod count or add four pods, whichever is more, so a workload at one or two pods can still grow quickly. Scaling down waits: the autoscaler looks back 300 seconds and only scales down to the highest count any recommendation in that window asked for, so a short dip in load does not remove pods that the next burst needs again. Once the window allows it, it may remove every excess pod at once. The PodDisruptionBudget and the pod spread that forge renders above one replica are keyed on maxReplicas, so they stay put while the count moves. A workload whose maximum is 1 renders neither.

What forge refuses

Each rule below fails at render, naming the workload and the fix.

Prerequisites on the cluster

The HPA reads CPU from the cluster’s metrics API, so the cluster needs metrics-server. k3d, GKE, EKS and AKS ship it. On a cluster without it the HPA exists but reports unknown metrics and never scales; kubectl describe hpa api says so.

On Reliant hosting

A workload bound to forge.OnHosted can declare autoscale, and it passes forge’s hosted checks, but Reliant hosting does not apply it yet. forge env deploy asks the control plane whether it advertises the autoscales-workloads capability before building. A control plane that does not would run the workload at one replica and never scale it, so forge refuses the deploy and names each workload:
Until then, set replicas for a hosted environment, or bind the workload to a cluster you run and keep the rest hosted (Deploy to your own Kubernetes cluster walks through the binding). The same declaration then works on hosting without edits once the capability ships.