> Markdown version of https://opspane.dev/kubernetes/hpa-calculator/ for AI agents and readers: the same text, without navigation or the interactive tool.
>
> The tool computes its result with JavaScript in the browser, so this file holds the reference text, worked examples and FAQ. Open the page to run it on your own input.
>
> Link the Kubernetes HPA calculator: `r` current replicas, `min`, `max`, and `m0` (up to `m3`) as kind_target_value_current_request, where kind is cpu, memory, container-cpu, container-memory, pods, object or external; target is u (Utilization, a percentage of the request), av (AverageValue) or v (Value); value and current are numbers or quantities; request (per pod) is optional. Load over time: `lp` as seconds_demand steps joined by *, `rd` readiness delay in seconds, `sp` sync period in seconds. E.g. https://opspane.dev/kubernetes/hpa-calculator/?r=4&min=2&max=10&m0=cpu_u_50_85_ (CPU at 85% of request, target 50%), or with a load spike https://opspane.dev/kubernetes/hpa-calculator/?r=4&min=2&max=20&m0=cpu_u_50_85_500m&lp=0_1700m*60_4250m*300_1000m&rd=30
>
> Answer endpoint for agents: the same URL parameters on https://opspane.dev/kubernetes/hpa-calculator/answer.md return the computed result as Markdown, without JavaScript, e.g. https://opspane.dev/kubernetes/hpa-calculator/answer.md?r=4&min=2&max=10&m0=cpu_u_50_85_

# Kubernetes HPA Calculator

How many replicas will the HPA ask for, and why? The controller's exact math: tolerance, missing and unready pods, several metrics and behavior.

## Quick answer

- `desiredReplicas = ceil(currentReplicas × currentMetric ÷ target)`. For example, 4 pods at 85% CPU against a 50% target give ceil(1.7 × 4) = **7**.
- Nothing changes while the ratio is within the tolerance, 0.1 by default (0.9 to 1.1).
- Scale-down waits for the stabilization window: the controller keeps the highest recommendation of the last 300 seconds.
- With several metrics, each proposes a count and the largest wins, kept within `minReplicas` and `maxReplicas`.

## How to calculate HPA desired replicas

1. Enter the current replicas, minReplicas and maxReplicas, or paste your HPA from `kubectl get hpa NAME -o yaml`, which fills in the spec and, from its status, the current values.
2. For each metric pick the source and target type, then the target and the current value as `kubectl get hpa` shows them (`cpu: 85%/50%` is current 85, target 50).
3. Switch to **Per pod** to enter each pod's usage, mark pods that are not ready or pending, and leave a value empty for a pod with no sample; the controller treats those specially.
4. Open **Load over time** to describe how the load changes: pick a preset (spike, ramp, a one-hour daily wave, flapping around the target) or edit the steps, each a time and Metric 1's total demand. Set how long a new pod takes to become ready and the controller's sync period.
5. Open **Behavior and tolerance** if your HPA sets `spec.behavior` or your cluster runs non-default flags. The result shows the desired count, each metric's math and how the count moves sync by sync.

## The formula, with an example

For each metric the controller computes a ratio and multiplies it by the number of pods it counted:

```
desiredReplicas = ceil(currentReplicas × currentMetricValue ÷ desiredMetricValue)
```

With 4 pods using 85% of their CPU request and a target of 50%: the ratio is 85 ÷ 50 = 1.7, outside the tolerance band, so the HPA asks for ceil(1.7 × 4) = ceil(6.8) = **7** replicas.

Two details matter at the edges. Utilization is a whole percentage computed before any division (Σusage × 100 ÷ Σrequests, rounded down), so 3 pods using 333m of 1 CPU each are at 33%, not 33.3%. And the product goes through a float64 `math.Ceil`, so a ratio that is a hair above a whole number adds a replica; the tool shows the exact product when that happens.

## Tolerance

Nothing changes while the ratio is inside the band `[1 − tolerance, 1 + tolerance]`, both edges included. The default tolerance of 0.1 is a kube-controller-manager flag, --horizontal-pod-autoscaler-tolerance. At a 50% target with 10 pods, 55% gives a ratio of 1.1 and keeps 10; 56% gives 1.12 and scales to 12.

Since Kubernetes 1.33 an HPA can set its own tolerance per direction, `behavior.scaleUp.tolerance` and `behavior.scaleDown.tolerance`, as a quantity such as `0.05`. The `HPAConfigurableTolerance` feature gate was alpha in 1.33, beta and on by default in 1.35, and GA in 1.37. A direction you leave unset uses the flag.

## Pods without metrics and unready pods

The first pass uses only ready pods that have a sample. Pending pods are always set aside; for CPU, so are pods that are not Ready yet or still inside their CPU initialization period. Then, if any pod had no sample, or the ratio says scale up while pods were set aside, the controller re-checks with those pods filled in:

- **Scale-up:** pods without a sample count as using 0, and unready pods count as 0. With 3 pods at 300m, 500m and 700m of 1 CPU against a 30% target, all ready gives 5; with the first pod not ready, the ready two average 60% and the re-check counts the third as 0 → 40% → **4**.
- **Scale-down:** pods without a sample count as using 100% of their request, or the target when it is above 100%. With 4 pods where only two report (400m and 95m against a 100% target), utilization is 24%, the re-check gives 62% and the HPA goes to **3**.
- **Cancelled:** if the re-check lands inside the tolerance, points the other way, or the new count would move against the ratio, replicas stay as they are. Unready 100m, ready 450m and one pod without a sample against 50% first reads below target (45%), but the re-check comes out above it, so it stays at 3.

## Several metrics

Each metric proposes a count and the **largest** wins: CPU at 48% against 60% keeps 4, a Pods metric at 90 per pod against 100 keeps 4, and an External queue of 900 against a Value target of 100 asks for 36, so the HPA goes to 36, or maxReplicas if lower. If a metric cannot be read, the HPA still scales up on the others but does not scale down on partial data.

Object and External metrics with an `AverageValue` target divide the metric's total by the current replicas: a total of 1,000 against 30 per pod with 4 replicas is ceil(1000 ÷ 30) = 34. With a `Value` target the ratio is multiplied by the number of ready pods. Container resource metrics work like resource metrics but use one container's usage and request.

## Behavior: how fast the count moves

The proposal is then kept within `minReplicas` and `maxReplicas` and rate-limited. What limits it depends on whether `spec.behavior` is set at all:

|  | spec.behavior not set | spec.behavior set (API defaults) |
| --- | --- | --- |
| Scale-up limit | max(2 × current, 4) per sync | larger of 4 pods and 100% per 15 s |
| Scale-up window | none | 0 s |
| Scale-down limit | none | 100% per 15 s |
| Scale-down window | 300 s (flag) | 300 s (flag), or `stabilizationWindowSeconds` |
| selectPolicy | — | Max |

The two columns are not the same: from 1 replica to a proposal of 40, without behavior the count goes 1 → 4 → 8 → 16 → 32 → 40; with behavior set to its defaults it goes 1 → 5 → 10 → 20 → 40, one sync every 15 seconds. A field you set replaces that field's default; the rest of the direction keeps its defaults.

**Stabilization window:** the controller remembers its recent recommendations. On the way down it uses the highest one inside the scaleDown window (its `stabilizationWindowSeconds`), so a drop from 10 to 3 replicas happens 300 seconds after the metric falls. On the way up it uses the lowest one inside the scaleUp window.

**Policies:** each policy allows a change of `value` pods or `value`% within `periodSeconds`, counted from where the count was at the start of the period. `selectPolicy: Max` takes the policy that allows the bigger change, `Min` the smaller, `Disabled` stops scaling in that direction.

When the workload has 0 replicas because someone scaled it to zero, the HPA computes nothing (ScalingActive=False). `minReplicas: 0` needs an Object or External metric and the `HPAScaleToZero` feature gate (alpha since 1.16, beta and on by default in 1.37).

## Load over time, sync by sync

The timeline under the result runs the same controller code once per sync period (--horizontal-pod-autoscaler-sync-period, 15 s by default). Each step of the load profile sets Metric 1's **total** demand across all pods (CPU in cores for a CPU metric) and holds until the next step. At every sync the ready pods share it, so the metric is demand ÷ ready pods, and the controller compares that with the target through the tolerance band, the stabilization windows, the rate limits and min/max.

With the Spike preset on the default form (4 pods at 85% of a 500m request, target 50%), the HPA goes 4 → 7 at 0:00 and to maxReplicas 10 when the spike lands at 1:00. When the load falls back at 5:00 the metrics ask for 7, but the 300 s window still holds the higher recommendations from the spike: the first scale-down is at 9:45, to 7, and the count settles at 3. The shaded band on the chart is the window holding a scale-down.

**Pods that start slowly.** A pod the HPA adds counts toward replicas at once, because the controller divides by the pods that exist, but serves no load until it is ready. Set **Pod ready after** to model that: in the spike above, ready pods run above target for 4:15 with pods ready at once and for 5:00 with a 60 s start-up. For CPU, the controller sets pods that are not ready aside and, when it would scale up, re-checks counting them as using 0, which keeps it from adding pods while the new ones start (replica_calculator.go, `groupPods`). For other metrics a pod that is not ready is counted like any other; the model has it report 0, since it receives no traffic.

**Flapping.** With the load alternating between 15% above and 15% below target every minute (the Flapping preset), the HPA makes 2 scale events: it scales up at once, and the window absorbs every dip until the load stops changing. With the downscale window set to 0 the same load makes 8.

## Verification

The engine is a line-by-line port of kube-controller-manager at `release-1.37`:

- [replica_calculator.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/controller/podautoscaler/replica_calculator.go)
- [horizontal.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/controller/podautoscaler/horizontal.go)
- [metrics/utilization.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/controller/podautoscaler/metrics/utilization.go)
- [autoscaling/v2/defaults.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/apis/autoscaling/v2/defaults.go)
- [replica_calculator_test.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/controller/podautoscaler/replica_calculator_test.go)
- [horizontal_test.go](https://github.com/kubernetes/kubernetes/blob/release-1.37/pkg/controller/podautoscaler/horizontal_test.go)

- Floats are IEEE-754 doubles like Go's float64 and every operation keeps upstream's order, so the tolerance comparisons and `math.Ceil` round the same way; sums of samples are exact integers like the controller's `big.Int`.
- The unit tests run the vectors of `replica_calculator_test.go` (resource, tolerance, missing metrics, Pods, Object and External metrics) and of `horizontal_test.go` (scaling rules, stabilization, conversion with and without behavior). The load timeline is tested against controller steps computed by hand and against `TestScaleUpHotCpuNoScale` and `TestStabilizeDownscale` replayed as a timeline. A few vectors, recomputed at build time:

| Upstream case | Input | Replicas |
| --- | --- | --- |
| scale up | 3 pods at 300m, 500m, 700m of 1 CPU; target 30% | `5` |
| unready pod scales less | same, first pod not ready | `4` |
| scale down | 5 pods at 100m–500m, target 50% | `3` |
| missing pods scale down | 4 pods, 2 without samples, 400m and 95m, target 100% | `3` |
| re-check flips direction | 3 pods: unready 100m, 450m, one without a sample; target 50% | `3` |
| Pods metric, unready ignored | 50k, 10k, unready 30k; target 15k | `6` |
| External per pod | total 8.6 against 2.15 per pod, 3 replicas | `4` |
| Object, 1% scale-up tolerance | 20.666 against 20, 3 replicas | `4` |

- What the timeline assumes: the ready pods share the demand evenly and see it at once, while real metrics are averaged over a window (metrics-server's is typically 15–60 s) and arrive a little late. Only Metric 1 follows the profile; other metrics keep their current totals. Syncs are exactly one sync period apart plus processing time, so an entry exactly one window old has expired, as in a running cluster.
- Pod start-up: every new pod becomes ready after the same delay. After a pod turns Ready, the controller ignores its CPU sample until one full metric window has been collected (inside the 5-minute --horizontal-pod-autoscaler-cpu-initialization-period), so for CPU include that window in the delay. Pods that never become ready, and the --horizontal-pod-autoscaler-initial-readiness-delay rule for them, are not modelled.
- A scale-down removes pods that are not ready first, as the ReplicaSet controller does; removed pods leave at once (the controller ignores terminating pods, so the math is the same, but real pods drain). The per-pod mode has no timeline and uses one request for every pod.
- The engine runs in this tab; nothing you type or paste is sent anywhere.

## FAQ

### How does the HPA calculate desired replicas?

desiredReplicas = ceil(currentReplicas × currentMetric ÷ target). With 4 pods at 85% CPU and a 50% target the ratio is 1.7, so ceil(1.7 × 4) = 7. Nothing changes while the ratio is within the tolerance (0.1 by default), and the result is kept within minReplicas and maxReplicas.

### What is the HPA tolerance?

A dead band around the target: by default the HPA does not scale while the ratio is between 0.9 and 1.1. At a 50% target, 55% (ratio 1.1) changes nothing and 56% (ratio 1.12) scales 10 pods to 12. The default is a kube-controller-manager flag (horizontal-pod-autoscaler-tolerance); since Kubernetes 1.33 an HPA can set its own under behavior.scaleUp.tolerance and behavior.scaleDown.tolerance (HPAConfigurableTolerance: alpha in 1.33, beta and on by default in 1.35, GA in 1.37).

### Why does my HPA not scale down right away?

Scale-down waits for the stabilization window: the controller takes the highest recommendation of the last 300 seconds. If the metric drops so that 10 replicas should become 3, the first scale-down happens 300 seconds later. Without spec.behavior the window is a kube-controller-manager flag (the downscale stabilization window); with it, stabilizationWindowSeconds under behavior.scaleDown.

### How fast can the HPA scale up?

Without spec.behavior, to at most max(2 × current, 4) replicas per sync: 1 → 40 goes 1 → 4 → 8 → 16 → 32 → 40. Once behavior is set, the defaults are the larger of 100% and 4 pods per 15 seconds: 1 → 5 → 10 → 20 → 40. Each sync is 15 seconds apart by default.

### What happens with several metrics?

Each metric proposes a replica count and the largest wins. CPU at 48% against 60% keeps 4, a Pods metric within tolerance keeps 4, and an External queue at 9 times its target asks for 36, so the HPA goes to 36 (or maxReplicas). If a metric cannot be read, the HPA still scales up on the others but never scales down.

### Can I simulate a traffic spike or a daily load curve?

Yes: under Load over time, describe Metric 1's total demand as steps (time → demand) or pick a preset. Each sync divides the demand by the ready pods and runs the controller's math. The Spike preset on the default form goes 4 → 7 → 10, and when the load drops at 5:00 the 300 s stabilization window holds 10 until 9:45. A readiness delay shows how long the ready pods run hot while new ones start.

### How are pods without metrics and unready pods counted?

The ratio is first computed from ready pods with samples. If it says scale up, pods without a sample count as using 0 and, for CPU, unready pods as 0 too; if it says scale down, pods without a sample count as using 100% of their request (or the target, if higher). If that re-check lands inside the tolerance or points the other way, nothing changes. Example: 3 pods at 300m, 500m and 700m of 1 CPU against 30% would go to 5, but with the first pod not ready it goes to 4.

## Related tools

- [Kubernetes Resource Units Converter](https://opspane.dev/kubernetes/resource-units/): Convert Kubernetes CPU and memory quantities: millicores, Mi vs M, the form kubectl shows, and the mistakes that break a limit.
- [EKS Max Pods Calculator](https://opspane.dev/kubernetes/eks-max-pods/): Calculate max pods per node for every EC2 instance type, with prefix delegation and the IP addresses your pod CIDR must hold.
- [Kubernetes CronJob Generator](https://opspane.dev/cron/kubernetes/): Generate a complete CronJob manifest with schedule, timeZone, concurrencyPolicy and history limits, explained field by field.
- [Kubernetes QoS Class & Requests Checker](https://opspane.dev/kubernetes/requests-limits/): Paste a Pod or Deployment: get its QoS class and why, the Pod request with init and sidecar containers, oom_score_adj and how many Pods fit a node.
