avatar🌌
TangentTangent Blog

技术博客 | Go & Kubernetes & Linux

技术博客 | Go & Kubernetes & Linux

KEDA Scale to Zero in Practice

Overview ​

KEDA (Kubernetes Event Driven Autoscaling) is an event-driven autoscaler that supports scaling workloads down to 0 replicas—an important feature that traditional HPA cannot achieve.

Core Features ​

1. Zero Replica Scaling ​

  • Full Resource Release: When no event is triggered, the number of Pods can scale down to 0, consuming no compute resources.
  • Fast Response: When a trigger is detected, scale up from 0 to the required number of replicas immediately.
  • Cost Optimization: Completely stop workloads during idle periods, significantly reducing cloud resource costs.

2. Event-Driven Mechanism ​

KEDA supports multiple trigger types:

  • Cron Triggers: Time-based scaling
  • Message Queues: Redis, RabbitMQ, Kafka, etc.
  • HTTP Requests: Based on HTTP request count
  • Databases: MySQL, PostgreSQL query results
  • Cloud Services: AWS SQS, Azure Service Bus, etc.

Configuration Example Analysis ​

Deployment Configuration ​

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: keda-test
  namespace: default
spec:
  replicas: 0  # Initial replicas set to 0, controlled by KEDA
  selector:
    matchLabels:
      app: keda-test
  template:
    metadata:
      labels:
        app: keda-test
    spec:
      containers:
      - name: keda-test
        image: busybox
        command: ["sh", "-c", "echo Hello KEDA; sleep 30"]

Key Points:

  • replicas: 0: Initial state is 0 replicas
  • Actual replica count is controlled by KEDA’s ScaledObject

ScaledObject Configuration ​

yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: keda-cron-scaledobject
  namespace: default
spec:
  scaleTargetRef:
    name: keda-test              # Target Deployment name
  pollingInterval: 15            # Check trigger every 15 seconds
  cooldownPeriod: 30             # Scale down 30 seconds after trigger ends
  minReplicaCount: 0             # Minimum replicas is 0 (key setting)
  maxReplicaCount: 2             # Maximum replicas is 2
  triggers:
  - type: cron
    metadata:
      timezone: "Asia/Shanghai"
      start: "0 * * * *"         # Scale up at minute 0 of every hour
      end: "30 * * * *"          # Scale down at minute 30 of every hour
      desiredReplicas: "1"       # Target replicas when scaling up

Key Parameter Explanations ​

minReplicaCount: 0 ​

  • This is the key setting for enabling scale to zero
  • When no trigger is satisfied, replicas will scale down to 0
  • Traditional HPA usually has a minimum of 1

pollingInterval ​

  • How often KEDA checks trigger conditions
  • Too short increases API server load
  • Too long affects response speed

cooldownPeriod ​

  • How long to wait before scaling down after triggers are not satisfied
  • Prevents frequent scaling actions
  • Gives workloads enough time to finish current tasks

Workflow ​

  1. Initial State: Deployment replicas is 0, no Pods running
  2. Trigger Detection: KEDA checks cron trigger every 15 seconds
  3. Scale Up Phase: When time matches start, scale up to 1 replica
  4. Running Phase: Pod runs tasks, KEDA keeps monitoring
  5. Scale Down Phase: After time passes end, wait 30 seconds then scale down to 0

Observation Process ​

Initially, KEDA keeps the Deployment at 0 replicas until the trigger is satisfied. Initial deploy

After applying the ScaledObject for keda-test, Pods start to be created.

Create podRunning pod

After completion, Pods are terminated. Terminating pod

The whole workflow: Workflow

Use Cases ​

1. Scheduled Jobs ​

  • Data backup jobs
  • Report generation
  • System maintenance tasks

2. Event-Driven Processing ​

  • Message queue processing
  • File upload handling
  • Burst API request handling

3. Cost Optimization ​

  • Resource saving for dev/test environments
  • Resource release during off-peak hours
  • Pay-as-you-go cloud optimization

Advantage Comparison ​

FeatureTraditional HPAKEDA
Min Replicas10
Trigger TypesCPU/Memory60+ event sources
Resource UsageAlways occupiedFully releasable
Response SpeedMinutesSeconds
Cost BenefitAverageVery high

Notes ​

  1. Cold Start Delay: Scaling from 0 requires extra Pod startup time
  2. State Persistence: Ensure your app is stateless or handles state recovery properly
  3. Monitoring & Alerts: Monitor the health of KEDA components
  4. Permission Config: KEDA needs proper RBAC permissions to manage workloads

Best Practices ​

  1. Set Intervals Properly: Adjust pollingInterval and cooldownPeriod as needed
  2. Resource Limits: Set appropriate CPU and memory limits
  3. Health Checks: Configure readiness and liveness probes
  4. Log Monitoring: Monitor logs of KEDA and your app for troubleshooting
  5. Test & Validate: Fully test scaling behavior before deploying to production

Summary ​

KEDA’s scale-to-zero feature is one of its most valuable capabilities, especially for event-driven and intermittent workloads. With proper configuration, you can significantly reduce resource costs while ensuring service availability.

Go-Zero Concurrency Tool: Detailed Analysis of `mr` Package Functions and Task Concurrency Execution Principles
Kubernetes StorageClass Explained and Lab Tutorial
由 Valaxy v1.2.2 驱动|主题-Yunv1.2.2