For most of its life, jithendar.com has run on exactly two pods. Two is plenty on an ordinary day, and two is what the Deployment said, so two is what ran. That holds until a post gets shared somewhere busy, and then it's two pods against whatever turns up.
Since 26 November that number belongs to KEDA (opens in a new tab). The site still never drops below two pods. When the requests reaching them go past what two should handle, more start, up to eight. This post walks through how that works on mk4, the small k3s cluster on Hetzner behind this site:
- what KEDA measures and what it builds from it;
- how I picked the numbers from a week of real traffic;
- why the cluster can't place a third pod yet;
- the morning this change broke every deploy.
The short version:
- It scales on requests per second, not CPU. Each pod's Istio sidecar counts the requests it receives, and Prometheus scrapes those counts. KEDA asks Prometheus for the site's total, and the HPA divides it by the number of pods. One pod should take 5 requests per second.
- CPU is only a backstop, at 200 % of the container's request. In a week of data, nearly all of the CPU spikes (129 of 133 minutes) came from pods starting up, not from visitors. A low CPU target would have added pods on every deploy.
- Two to eight pods. At least two, because the pods spread over nodes and a node drain has to leave one serving. At most eight, because that's where the database connections run out.
- A third pod has nowhere to go yet. The spread rule sends it to worker3, which is 23Mi of memory short. The autoscaler works; the cluster needs room.
- The ScaledObject ships through the same pipeline as every other manifest: a test, an allow-list and a permission in CI's Role. Merging it with placeholder values stopped every deploy for almost half an hour. Since then, a half-configured ScaledObject is left out with a warning instead of breaking the deploy.
Why not an HPA on CPU
That was my first idea too. CPU is the default example in every autoscaling guide, and Kubernetes can do it without installing anything. Before picking a number, I pulled a week of data out of Prometheus: CPU of the site's web container from cAdvisor, matched to each pod's start time, plus the request counts from Istio.
The pattern was hard to miss:
- The CPU spikes are startups. A pod used more than 0.4 cores in 133 minutes of the week, and 129 of those minutes were in the pod's first ten. A fresh pod warms its page cache before anything else. In the home page 520 postmortem (opens in a new tab), that took about 52 seconds for 91 pages, and it shows up as a burst of up to 0.9 cores.
- Pods start a lot. 88 different
jithendar-compods ran that week: deploys restart them, moks-cms releases restart them, and node maintenance moves them. - Visitors barely move the needle. At the busiest minute of the week, 4.8 requests per second for the whole site, the busiest pod used 0.26 cores.
- A textbook target would flap. The site's average CPU was above 80 % of its request for 79 minutes, almost all of them during restarts. An HPA with the usual 70 to 80 % target would have added pods on nearly every deploy, then removed them five minutes later.
Requests per second don't care that a pod has just started. They measure what I actually want to scale for: people asking for pages. So requests lead, and CPU stays as a backstop for the day requests get unusually heavy.
Counting the right requests
The site runs in the Istio mesh, so every pod has an istio-proxy sidecar that counts what passes through it in istio_requests_total. Prometheus scrapes those counters every 30 seconds. The query KEDA runs:
sum(rate(istio_requests_total{
reporter="destination",
destination_workload_namespace="jithendar",
destination_workload="jithendar-com",
destination_service_name="jithendar-com"
}[2m]))Every part of it is there for a reason:
reporter="destination"keeps only what the receiving sidecar reported. Istio reports a request on both sides, once from the caller's proxy (here, the gateway) and once from the receiver's, so counting both would double every number.destination_service_name="jithendar-com"keeps only requests through the public Service. The seo-agent calls the site through a second Service,jithendar-com-internal, and its callbacks shouldn't add pods for readers.rate(...[2m])turns a counter into requests per second, averaged over two minutes. With a 30-second scrape, that's about four samples: enough to ignore a single burst, short enough to react within a couple of minutes.sum(...)gives the total for the whole site. KEDA hands that total to the HPA, and withmetricType: AverageValuethe HPA divides it by the number of pods. So the threshold you write is per pod.
There's one kind of request the query never sees: pages Cloudflare serves from its cache. They never reach mk4, which is exactly right, because a cached page costs the pods nothing.
The ScaledObject
KEDA itself was already on the cluster: version 2.21.1, installed as a Helm release by Terraform in k8s-deployments (opens in a new tab) (minikube-infrastructure/keda.tf). The site's part is one file in this site's repository, kubernetes/35-autoscaling.yaml, applied with the rest of the site. Without its comments:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: jithendar-com
namespace: jithendar
spec:
scaleTargetRef:
name: jithendar-com # the Deployment (KEDA's default kind)
minReplicaCount: 2
maxReplicaCount: 8
triggers:
- type: prometheus
metricType: AverageValue
metadata:
serverAddress: http://prometheus.monitoring:9090/prometheus
query: sum(rate(istio_requests_total{reporter="destination",destination_workload_namespace="jithendar",destination_workload="jithendar-com",destination_service_name="jithendar-com"}[2m]))
threshold: "5" # requests per second, per pod
- type: cpu
metricType: Utilization
metadata:
containerName: web # the app only, not the istio-proxy sidecar
value: "200" # percent of the web container's CPU request (250m)A few things that aren't obvious from the YAML:
- The Deployment has no
replicasfield anymore. If it did, everykubectl applywould set the count back to that number, over whatever KEDA had chosen. - No credentials anywhere. Prometheus is inside the mesh, so there's no TriggerAuthentication: KEDA's operator has its own Istio sidecar and calls Prometheus over mTLS as
keda/keda-operator. Namespacekedais on the list of callers the monitoring namespace accepts. containerName: webmakes the CPU trigger compare only the app container against its own request. Without it, KEDA uses the whole pod, sidecar included.minReplicaCount: 2means no scale to zero. KEDA can take a workload down to nothing and back, but a website that has to warm up for a minute on the first request isn't a good candidate. Because of this, KEDA'spollingInterval, which drives the jump from zero to one, barely matters here.
Two easy mistakes with the values
The trigger values are strings, and the counts are integers. KEDA's CRD types triggers[].metadata as a map of strings, so threshold: 5 without quotes is refused by the API server. maxReplicaCount: "8" with quotes is refused for the opposite reason.
The values also carry no units:
thresholdis a plain number of requests per second, so150smeans nothing to it.- A
Utilizationvalue is a percent of the container's request, not millicores. If you mean "0.5 cores" on a 250m request, that's"200".500monly works withmetricType: AverageValue.
What KEDA builds from it
KEDA doesn't scale anything itself. Its operator reads the ScaledObject and creates an ordinary HorizontalPodAutoscaler named keda-hpa-jithendar-com, which it owns and keeps in step. If you already have an HPA for the same Deployment, KEDA's admission webhook refuses the ScaledObject, so delete yours first. Here is what it made, from the live cluster:
spec:
minReplicas: 2
maxReplicas: 8
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: jithendar-com}
metrics:
- type: ContainerResource
containerResource:
name: cpu
container: web
target: {type: Utilization, averageUtilization: 200}
- type: External
external:
metric:
name: s0-prometheus
selector:
matchLabels: {scaledobject.keda.sh/name: jithendar-com}
target: {type: AverageValue, averageValue: "5"}The CPU trigger became a plain ContainerResource metric. The HPA reads it from Metrics Server, and KEDA isn't involved at all. The Prometheus trigger became an External metric called s0-prometheus, which is where KEDA does its real work.
Every 15 seconds the HPA controller asks the API server for s0-prometheus. The API server doesn't know what that is, but an APIService, v1beta1.external.metrics.k8s.io, says who does: keda-operator-metrics-apiserver. That pod asks the operator over gRPC, the operator runs the query against Prometheus, and the answer travels back the same way.
KEDA's install on mk4 splits the mesh around that path:
- The operator has an Istio sidecar. Prometheus only accepts mTLS, so the operator's queries have to go through one.
- The metrics API server and the admission webhooks don't. The Kubernetes API server calls them directly, and a proxy that isn't ready yet would fail those calls.
- One port is relaxed. The metrics API server talks to the operator on port 9666 with KEDA's own TLS, so a PeerAuthentication makes just that port PERMISSIVE and leaves the rest of the operator on STRICT.
You can make the same call the HPA makes:
$ kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/namespaces/jithendar/s0-prometheus?labelSelector=scaledobject.keda.sh%2Fname%3Djithendar-com"
{"kind":"ExternalMetricValueList","apiVersion":"external.metrics.k8s.io/v1beta1","metadata":{},
"items":[{"metricName":"s0-prometheus","metricLabels":null,"timestamp":"2026-11-26T09:24:45Z","value":"1.400"}]}That 1.400 is the whole site's requests per second, not one pod's. The HPA does the division, which is why kubectl get hpa shows something like 700m/5 (avg) with two pods running.
The arithmetic
The HPA's rule is the same for both metrics: desired = ceil(current pods × current value ÷ target). With an average value that works out to:
- Requests: the current value is the total divided by the pods, so the pods cancel out:
ceil(total ÷ 5). - CPU:
ceil(pods × utilization ÷ 200).
It takes the larger of the two, then clamps it between 2 and 8.
| Requests per second, whole site | Pods |
|---|---|
| up to 10 (today: about 1; the week's peak: 4.8) | 2 |
| 10 to 15 | 3 |
| 15 to 20 | 4 |
| 20 to 25 | 5 |
| 25 to 30 | 6 |
| 30 to 35 | 7 |
| 35 and more | 8 |
Three HPA defaults shape how it moves, and none of them is set in the ScaledObject:
- A 10 % tolerance. The HPA doesn't act when the ratio to the target is within 10 %. Going from 2 pods to 3 therefore happens at about 11 requests per second, not at 10.01.
- Scaling up is immediate. There's no stabilization window going up, and in one 15-second step the HPA may add 4 pods or double the count, whichever is more. From 2 pods it can go straight to 6.
- Scaling down waits five minutes. The HPA uses the highest recommendation of the last 300 seconds. A spike that lasts one minute holds its extra pods for five, so the count doesn't bounce.
Choosing 5, 200 and 8
Picking these numbers is the part most guides skip. Here's the reasoning, from the same week of data.
5 requests per second per pod. The first question was what a request costs. Across the 9,238 minutes of the week in which no pod had started in the previous quarter of an hour, a straight-line fit gives:
- about 0.053 core-seconds of CPU per request;
- about 0.013 cores for both pods sitting idle.
So a pod taking 5 requests per second uses about 0.27 cores, a little over its 250m request. At that load:
- Today's traffic stays on two pods. The week's peak was 4.8 requests per second for the whole site, 2.4 per pod.
- A third pod comes at about 11 requests per second, more than twice the busiest minute of the week.
- There's headroom while it starts. A new pod needs a minute or two to start and warm up. Each pod may use up to 2 cores (its limit), so one sitting at 0.27 has plenty of room in that time.
CPU at 200 %. 200 % of a 250m request is 0.5 cores. At 0.053 core-seconds per request, that's about 9 typical requests per second on one pod, well past the point where the requests trigger has already added pods. So CPU only decides when requests are heavier than usual: pages that miss every cache, admin work, search. It isn't completely clear of restart noise. The site's average was above 200 % for 28 minutes of the week, almost all during restarts. The HPA sets aside CPU from pods that aren't ready yet, which takes some of the edge off. If extra pods do start turning up after deploys, the fix is a short scale-up stabilization window under advanced.horizontalPodAutoscalerConfig, not a lower target.
8 pods at most. This one isn't about CPU or memory. Each pod opens up to 5 database connections (DATABASE_POOL_MAX). PgBouncer runs in session mode, so every client connection holds a server connection, and the two pooler pods have 20 server connections each. Eight pods use all 40. Past that, requests would queue for a connection, and more pods wouldn't help.
Two of the queries behind those numbers, if you want to try the same on your own cluster:
# The whole site's requests per second: the busiest 2-minute rate of the last 7 days
max_over_time(sum(rate(istio_requests_total{reporter="destination",destination_workload_namespace="jithendar",destination_workload="jithendar-com",destination_service_name="jithendar-com"}[2m]))[7d:1m])
# The busiest pod's CPU: 99th percentile over the last 7 days, in cores
quantile_over_time(0.99, max(rate(container_cpu_usage_seconds_total{namespace="jithendar",pod=~"jithendar-com-.*",container="web"}[2m]))[7d:1m])The rest are in the repository with their results, in docs/knowledge-sharing/keda-autoscaling/evidence/prometheus.txt (opens in a new tab).
Where a third pod would go
An autoscaler can only ask for pods. The scheduler still has to find room for them, and on 26 November it couldn't.
The Deployment has a topology spread rule:
maxSkew: 1acrosskubernetes.io/hostname, withwhenUnsatisfiable: DoNotSchedule;nodeTaintsPolicy: Honor, so the tainted control-plane node doesn't count.
In plain words, no worker may have more than one pod more than another. That's what gives two pods on two different nodes, and it's why a node drain never takes the whole site down. But it also decides where the third pod goes:
- worker1 and worker2 already have one pod each. A second pod on either would make it 2, 1, 0, a skew of 2.
- So the third pod may only start on worker3.
- worker3 has 489Mi of memory requests left. A site pod asks for 512Mi.
So if traffic went past 11 requests per second today, the HPA would ask for three pods, and the third would sit Pending. The site would keep serving from two. That's no worse than before KEDA, but it isn't the point of having it either. The fixes are all trade-offs:
- free about 25Mi of requests on worker3, where the
ragnamespace holds the largest share (3 GiB); - relax the rule to
ScheduleAnyway, giving up the even spread to gain scheduling; - add a fourth worker.
None of them is done yet.
Shipping it through the pipeline
This site's manifests are applied by a GitHub Actions workflow, not by hand. On a merged pull request, the workflow does a server-side dry run, a diff, an apply, a rolling restart, a smoke test, and then restores the CrewAI configuration. Adding a new kind of object to that pipeline took three things:
- A permission. The workflow deploys as the ServiceAccount
jithendar/github-ci-applier. Its Role gainedcreate,patchandupdateonscaledobjects.keda.sh, and read-only access to HorizontalPodAutoscalers, so the deploy can show the HPA but never edit it. - A place on the allow-list. A script,
scripts/ci-app-manifests.py, picks the objects CI may apply from the rendered manifests. A kind on neither of its lists stops the run, so every new kind is a deliberate decision. ScaledObject is now on the list. TriggerAuthentication deliberately isn't, because it reads Secrets, so a manifest that adds one stops the deploy until a person decides. - A test.
scripts/tests/test_autoscaling.pychecks the wiring:- the Deployment sets no
replicas; - the ScaledObject targets it and keeps at least two pods, more than the PodDisruptionBudget's
minAvailable: 1; - the query selects the public Service;
- the CPU trigger names a container that exists;
- no trigger needs credentials.
- the Deployment sets no
The replicas trap
Removing replicas: 2 from the Deployment looks harmless. It isn't, if the Deployment was ever applied with kubectl apply. Client-side apply keeps the last manifest it applied in an annotation, kubectl.kubernetes.io/last-applied-configuration, and compares three things: that annotation, the live object and your new file. A field that's in the annotation but missing from the new file is treated as "you deleted this", so kubectl removes it from the live object. A Deployment without replicas defaults to one pod. The site would have dropped to one pod on the first deploy and stayed there until the HPA noticed and brought it back.
The fix is to rewrite the annotation from the new manifest before that deploy. It changes nothing else on the object:
kubectl -n jithendar apply set-last-applied -f jithendar-com-deployment.yaml
kubectl -n jithendar get deployment jithendar-com \
-o jsonpath='{.metadata.annotations.kubectl\.kubernetes\.io/last-applied-configuration}' \
| python3 -c 'import json, sys; print("replicas" in json.load(sys.stdin)["spec"])' # FalseAfter that, kubectl diff of the new Deployment against the cluster showed no change at all, which is what you want to see before letting KEDA take over.
The morning it broke every deploy
The numbers in the ScaledObject were meant to be chosen last, from the data above, so the pull request that added it had SET-ME in their place. Then it was merged on 26 November at 08 UTC, still with the placeholders.
The deploy started a minute later and stopped at the first step, the server-side dry run:
* spec.maxReplicaCount: Invalid value: "string": spec.maxReplicaCount in body must be of type integer: "string"
##[error]Server dry run of group site failed.Nothing was applied, and the site kept running its two pods. But the site group is all or nothing, so every deploy after that failed the same way, including the restarts that pick up a new moks-cms release. Two quick fixes followed:
- The plan step now names the problem on the pull request. The deploy's
planjob runs on every pull request, and now it reports each placeholder by its path, for examplespec.triggers[0].metadata.threshold is SET-ME. That turns a confusing dry-run error after the merge into a clear failure before it. - A ScaledObject with any
SET-MEis now left out with a warning per value, and everything else deploys. That's safe here because leaving it out changes nothing that runs: the Deployment has noreplicas, so it keeps the pods it has. ASET-MEin any other kind of object still stops the run, and the autoscaling tests skip a number while it's unset and check it once it's set.
The fix was merged at 08, and its deploy went green at 09, less than half an hour after the first failure. The real numbers went live at 09. The lesson I'm keeping: a placeholder that the API server can't parse is a time bomb on the main branch, and a draft label won't stop anyone from merging it. If a value has to be filled in later, the unfilled state should still deploy.
Checking it
Everything here is read-only:
$ kubectl -n jithendar get scaledobject jithendar-com
NAME SCALETARGETKIND SCALETARGETNAME MIN MAX READY ACTIVE FALLBACK PAUSED TRIGGERS
jithendar-com apps/v1.Deployment jithendar-com 2 8 True True False False prometheus,cpu
$ kubectl -n jithendar get hpa keda-hpa-jithendar-com
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS
keda-hpa-jithendar-com Deployment/jithendar-com cpu: 2%/200%, 306m/5 (avg) 2 8 2How to read them:
READY Truemeans KEDA accepted the object and can reach its scalers.ACTIVE Trueonly says a trigger is above its activation threshold. That's 0 by default, so any traffic at all makes it active, and it doesn't mean "scaling up".- The HPA's
TARGETScolumn shows both metrics against their targets: CPU at 2 % of the web container's request, and 0.306 requests per second per pod against 5.
When something's wrong, look here first:
kubectl -n jithendar describe scaledobject jithendar-com # conditions and events; a Prometheus error shows here
kubectl -n jithendar describe hpa keda-hpa-jithendar-com # every scaling decision, with the metric that drove it
kubectl -n keda logs deploy/keda-operator -c keda-operator --tail=50
kubectl get apiservice v1beta1.external.metrics.k8s.io # Available True, or no HPA can read s0-prometheusIf Prometheus can't be reached, the External metric fails. The HPA then still scales up on CPU but won't scale down, and the minimum of two always holds. KEDA can also run a fixed fallback number of pods after a set number of failed reads. The file has that commented out for now.
What's next
- Room for the third pod, as above. Until then, the ceiling in practice is two.
- Watching it from Grafana. KEDA's operator exports its own metrics (scaler values, errors, active state), but this Prometheus doesn't scrape it yet. kube-state-metrics, which would give the HPA's desired and current replicas, isn't running either. With both, an autoscaling panel could go on the public Grafana (opens in a new tab), next to the request rates it already shows.
- A load test somewhere safe. I'd rather see the first scale-up in a copy of the namespace than wait for a busy day in production. When it does happen for real, this post gets the HPA's events.
The pieces are all public if you want to copy them:
- the ScaledObject, tests and deploy pipeline in jithendar.com (opens in a new tab) (
kubernetes/35-autoscaling.yaml,scripts/tests/test_autoscaling.py,scripts/ci-app-manifests.py); - KEDA's install in k8s-deployments (opens in a new tab) (
minikube-infrastructure/keda.tf,deploy/minikube/keda/).
The numbers are the part you can't copy. Take a week of your own traffic first.



Discussion comments
No comments yet. Start the discussion.