App
Scale to zero with KEDA
Queue length and external metrics
A worker Deployment takes jobs off a RabbitMQ queue called jobs. Most of the night the queue is empty, and a Horizontal Pod Autoscaler can't go below one replica, so one worker would sit idle. KEDA can take it to zero. It watches the queue itself, brings the Deployment from zero to one when work arrives, and hands the rest to an ordinary HPA fed with the queue's length through the external metrics API. Follow 30 jobs from arrival to an empty queue and back to zero.
- KEDA
- v2.21 · keda-operator, keda-metrics-apiserver
- Trigger
- rabbitmq · queue jobs · 10 messages per worker
- Range
- 0 to 10 worker Pods
- 0 ↔ 1
- KEDA · 1 ↔ N: the HPA
Read the whole lesson as text
1. The ScaledObject
You apply a ScaledObject for the worker Deployment: between 0 and 10 replicas, one RabbitMQ trigger on queue jobs, a target of 10 messages per worker. KEDA's operator, running on node-1, sees it and creates an HPA named keda-hpa-worker. That HPA asks for an external metric, the queue's length, with minReplicas 1. Then the operator checks the queue, finds it empty, and scales the Deployment to zero itself.
- You create a ScaledObject that points at the worker Deployment.
- The operator, on node-1, creates an HPA for the Deployment, fed by an external metric.
- The operator asks RabbitMQ how many messages are in jobs: none.
- The queue is empty, so the operator scales the Deployment from one to zero.
2. Polling at zero
With no worker running, nothing would notice new jobs, so KEDA does. Every pollingInterval, 30 seconds by default, the operator asks RabbitMQ for the length of jobs. While it's 0 the scaler stays inactive and the Deployment stays at zero. The HPA does nothing either: it won't scale a target that has zero replicas. All night, the cost of the worker is one small HTTP request every 30 seconds.
- Thirty seconds later: still nothing in the queue.
3. Activation
At 06:00 the checkout service publishes 30 jobs. On its next poll the operator sees 30 messages. That is above the activation value of 0, so the scaler is active, and the operator itself scales the Deployment from zero to one. Only KEDA crosses between 0 and 1. The scheduler places the Pod on node-2, it starts, connects to RabbitMQ and takes its first job.
- The checkout service publishes 30 jobs to the queue.
- On its next poll, the operator sees 30 messages waiting.
- The operator scales the Deployment from zero to one.
- The worker starts on node-2 and is Ready.
4. The HPA takes over
Every 15 seconds the HPA controller asks the API server for the metric s0-rabbitmq-jobs. external.metrics.k8s.io isn't served by the API server itself: an APIService registers KEDA's metrics server for it, and the API server forwards the request there. KEDA's metrics server gets the queue's length, 28 now, and returns it. The target is an average of 10 per Pod, so the HPA wants ceil(28 / 10) = 3 workers and scales the Deployment to three.
- The HPA controller asks for the queue metric, like any other API request.
- The API server forwards it to KEDA's metrics server, which registered for that API group.
- KEDA reads the queue's length: 28, now that the first worker has taken two.
- The answer goes back to the HPA through the API server.
- 28 messages at 10 per Pod is 3 Pods. The HPA scales the Deployment.
5. Draining
Three workers take jobs off the queue. The metric falls: 18, 7, then 0. The HPA's answer falls with it, down to its floor of one, but it doesn't scale down at once. By default it waits until 300 seconds of recommendations all agree, so a short gap between bursts doesn't throw Pods away. Five minutes after the queue emptied, it scales the Deployment to one.
- All three workers take jobs and acknowledge each one when it's done.
- The queue is empty. The HPA's next look says one worker is enough.
- Five minutes of low recommendations later, the HPA scales the Deployment to one.
6. Back to zero
The operator has seen an empty queue on every poll since the jobs ran out. Once cooldownPeriod, 300 seconds by default, has passed since the scaler was last active, it scales the Deployment from one to zero, and the HPA goes quiet again. That's the trade: nothing runs while there's nothing to do, and the first job after a quiet spell waits for a poll and a Pod start. For a model server, that start also includes loading the model.
- Cooldown is over and the queue is still empty. The operator scales the worker to zero.
Summary
KEDA splits scaling in two. Between zero and one, its operator polls the queue and scales the Deployment itself, because nothing else would notice work while no Pod runs. Above one, it hands over to an ordinary HPA, which reads the queue's length through the external metrics API that KEDA's metrics server answers, and divides by the target per Pod. Cooldown takes it back to zero, and the price is a cold start for the first job after a quiet spell.
Next lesson: Starting a model server