The Horizontal Pod Autoscaler scales on CPU and memory, but a queue worker is busy when its queue is long, not when its CPU is hot, and an idle worker should cost nothing. KEDA, a graduated CNCF project in kedacore/keda, adds event-driven scaling to Kubernetes: scale on queue length, consumer lag, a Prometheus query or a schedule, all the way down to zero and back up. This lesson covers how it works, the two resources you write, and the settings that decide whether scaling is smooth or flappy.
- Explain how KEDA's operator and metrics server cooperate with the HPA
- Write a ScaledObject that scales a Deployment on queue lag or a Prometheus query
- Choose between a ScaledObject and a ScaledJob for long-running work
- Set activation thresholds, cooldown and fallback so scaling is predictable
- Authenticate scalers with TriggerAuthentication instead of inline secrets
How KEDA works: it feeds the HPA, it does not replace it
The cmd/ folder of the repository builds three binaries. The operator watches your KEDA resources, creates a normal HPA for each one, and handles the special case the HPA cannot: scaling between zero and one replica. The metrics adapter (the KEDA metrics server) registers as a Kubernetes external metrics API, so when the HPA asks "what is the value of this metric?" the answer comes from KEDA. The admission webhooks reject invalid objects, such as two ScaledObjects targeting the same Deployment.
The knowledge of each event source lives in pkg/scalers/: one Go file per scaler, about seventy of them covering Kafka, RabbitMQ, SQS, Azure Service Bus, Redis streams, PostgreSQL queries, Prometheus, Cron and many more. A scaler answers two questions: is there any work at all (activation), and how much (the metric value).
- Activation phase (0 to 1, 1 to 0): the operator decides, using the scaler's activation check.
- Scaling phase (1 to N): the HPA decides, using the metric KEDA exposes and the target you set.
- Because the HPA does the 1-to-N maths, everything you know about HPA behaviour, stabilization windows and policies still applies.
Install with Helm: helm repo add kedacore https://kedacore.github.io/charts, then helm install keda kedacore/keda --namespace keda --create-namespace. You get the CRDs, the operator, the metrics server and the webhooks.
ScaledObject: scale a Deployment on events
A ScaledObject points at a Deployment, StatefulSet, or any custom resource with a /scale subresource, and lists one or more triggers. Here an order processor scales on Kafka consumer lag: one replica per 50 pending messages, up to 30, and zero when the topic is drained.
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: orders-worker
spec:
scaleTargetRef:
name: orders-worker
minReplicaCount: 0
maxReplicaCount: 30
pollingInterval: 15
cooldownPeriod: 300
triggers:
- type: kafka
metadata:
bootstrapServers: kafka.data.svc:9092
consumerGroup: orders
topic: orders
lagThreshold: "50"
activationLagThreshold: "5"The defaults matter: pollingInterval is 30 seconds (how often KEDA checks the source while idle), cooldownPeriod is 300 seconds (how long after the last activity before scaling back to zero), minReplicaCount is 0 and maxReplicaCount is 100. cooldownPeriod only governs the step to zero; scale-in between N and 1 follows the HPA's own stabilization window.
The Prometheus scaler makes KEDA useful far beyond queues. For LLM serving, the right signal is usually requests waiting in the engine's queue, not GPU utilisation, which is high whenever a GPU is in use at all.
triggers:
- type: prometheus
metadata:
serverAddress: http://prometheus.monitoring.svc:9090
query: sum(vllm:num_requests_waiting{model_name="chat"})
threshold: "4"
activationThreshold: "1"ScaledJob: one Job per piece of work
Scaling a Deployment down kills pods, possibly in the middle of a message. For long-running tasks such as video transcoding, batch inference or a nightly training run, that is unacceptable. A ScaledJob creates Kubernetes Jobs instead: each Job takes one or a few items, runs to completion and exits, and nothing is killed to scale in.
apiVersion: keda.sh/v1alpha1
kind: ScaledJob
metadata:
name: transcode
spec:
jobTargetRef:
backoffLimit: 2
template:
spec:
restartPolicy: Never
containers:
- name: worker
image: ghcr.io/example/transcode:2.3.1
pollingInterval: 20
maxReplicaCount: 50
successfulJobsHistoryLimit: 5
failedJobsHistoryLimit: 5
triggers:
- type: aws-sqs-queue
authenticationRef:
name: sqs-auth
metadata:
queueURL: https://sqs.eu-west-1.amazonaws.com/123456789012/videos
queueLength: "1"
awsRegion: eu-west-1Set the history limits: both default to 100, and a busy queue otherwise leaves hundreds of finished Job objects behind. The scalingStrategy block (default, custom, accurate, eager) controls how pending messages and already-running Jobs are counted, which matters when your Jobs take items off the queue slowly.
Credentials, thresholds and failure modes
Scalers need to read the queue, so they need credentials. Never put them in trigger metadata. A TriggerAuthentication (namespaced) or ClusterTriggerAuthentication maps scaler parameters to Kubernetes Secrets, or better, to workload identity on your cloud so no long-lived key exists.
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
name: rabbitmq-auth
spec:
secretTargetRef:
- parameter: host
name: rabbitmq-conn
key: url- Flapping between 0 and 1: set an activation threshold above zero so a single stray message does not wake the workload, and lengthen
cooldownPeriod. - Source unreachable: without a
fallback, an erroring scaler leaves you at whatever count you had. Withfallback: {failureThreshold: 3, replicas: 6}, KEDA holds a safe replica count after three failed checks. - Maintenance: the
autoscaling.keda.sh/paused: "true"annotation freezes scaling;paused-replicaspins a fixed count. - Two autoscalers: do not also create your own HPA for the same target. KEDA owns the HPA it generates, named
keda-hpa-plus the ScaledObject name. - Cold start: scaling from zero takes pod scheduling plus image pull plus app start. If the first request must be fast, keep
minReplicaCount: 1.