DEV Community

Cover image for KEDA Kubernetes Tutorial: Scale to Zero with HPA & SQS
Naveed Ahmed
Naveed Ahmed

Posted on Originally published at blog.naveedkumbhar.com

KEDA Kubernetes Tutorial: Scale to Zero with HPA & SQS

Originally published on Naveed Ahmed's Engineering Blog.

Imagine a Kubernetes Deployment running queue consumers.

CPU usage is low, but the queue contains thousands of messages.

If the HPA relies only on CPU, it may not scale the application when you actually need more workers.

KEDA solves a different part of the autoscaling problem: it can use external signals such as Kafka consumer lag, RabbitMQ messages, AWS SQS queue depth and Prometheus queries.

1. How KEDA Works

For a Deployment managed by a ScaledObject, KEDA has two main responsibilities:

  • The KEDA operator evaluates event-source activation and handles scaling between zero and one replica.
  • The KEDA metrics API server exposes external metrics so the Kubernetes HPA can scale active workloads.

The basic sequence is:

Message queue / external metric
              |
              v
         KEDA scaler
              |
       +------+------+
       |             |
       v             v
   Activation     External metric
     0 → 1             |
                       v
                  Kubernetes HPA
                       |
                       v
                   More Pods
                       |
                       v
                 Node autoscaler
                (if capacity needed)
Enter fullscreen mode Exit fullscreen mode

2. Install KEDA With Helm

Add the official chart repository:

helm repo add kedacore https://kedacore.github.io/charts
helm repo update
Enter fullscreen mode Exit fullscreen mode

Review and pin a compatible chart version before production installation.

A basic example:

helm upgrade --install keda kedacore/keda \
  --namespace keda \
  --create-namespace
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

kubectl get pods -n keda
kubectl get apiservices | grep external.metrics
Enter fullscreen mode Exit fullscreen mode

Review cluster compatibility and the Helm chart's current values before enabling additional settings.

3. Create an SQS ScaledObject

Suppose an orders-worker Deployment reads from Amazon SQS.

A basic ScaledObject might look like this:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: orders-sqs-scaler
  namespace: workers
spec:
  scaleTargetRef:
    name: orders-worker
  pollingInterval: 15
  cooldownPeriod: 300
  minReplicaCount: 0
  maxReplicaCount: 50
  triggers:
    - type: aws-sqs-queue
      metadata:
        queueURL: https://sqs.us-east-1.amazonaws.com/123456789012/orders-prod
        awsRegion: us-east-1
        queueLength: "5"
      authenticationRef:
        name: orders-sqs-auth
Enter fullscreen mode Exit fullscreen mode

This example assumes a compatible KEDA version and an existing TriggerAuthentication named orders-sqs-auth.

Use supported AWS workload identity or another approved authentication method rather than embedding access keys into the manifest.

The scaling threshold is a starting value, not a universally correct production setting.

4. KEDA vs HPA vs Karpenter

Component Main responsibility
KEDA Trigger activation and event-driven autoscaling
HPA Scale active application Pods based on metrics
Karpenter Provision and manage EC2 node capacity

KEDA can request more application replicas without automatically ensuring that the cluster has enough room to schedule them.

If Pods remain Pending, investigate scheduling limits, node pools, instance availability and node provisioning.

5. Why Workers Stay at Zero

A ScaledObject that never activates may have an authentication, trigger or metrics issue.

Start with:

kubectl get scaledobjects -A
kubectl describe scaledobject orders-sqs-scaler -n workers
kubectl get hpa -n workers
Enter fullscreen mode Exit fullscreen mode

Then inspect the operator logs:

kubectl logs -n keda \
  -l app=keda-operator \
  --tail=100
Enter fullscreen mode Exit fullscreen mode

The exact labels and namespace depend on your installation.

Check whether KEDA can access the external event source and whether the trigger actually reports activation.

6. Scale-to-Zero Has Trade-Offs

Scale-to-zero can reduce idle resource usage.

But a newly activated workload still has to start.

That may include scheduling, pulling an image, establishing connections, and initializing the application.

If latency is sensitive, consider keeping a minimum replica count or pre-warming before predictable peaks.

7. Avoid Conflicting Controllers

Do not configure an independent HPA to control the same Deployment as a KEDA-managed ScaledObject without an explicitly supported ownership model.

Competing controllers can create confusing scaling behavior.


Read the Complete KEDA Guide

My full tutorial also explains AKS integration, TriggerAuthentication, Prometheus scaling, predictive autoscaling, Kubernetes compatibility and production troubleshooting.

Read KEDA Kubernetes: Event-Driven Autoscaling Guide

Official reference: KEDA Documentation.


What external signal would give you the most useful autoscaling behavior in your cluster?

Written by Naveed Ahmed — DevOps & Platform Engineering

Top comments (0)