0
0
Kafkadevops~7 mins

Auto-scaling strategies in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Auto-scaling helps Kafka handle more or fewer messages by automatically adjusting resources. It solves the problem of sudden traffic changes without manual intervention.
When your Kafka cluster faces unpredictable spikes in message traffic and needs more brokers to keep up.
When you want to save costs by reducing the number of brokers during low traffic periods.
When you want to maintain stable performance by automatically balancing load across brokers.
When you deploy Kafka on cloud platforms that support dynamic resource allocation.
When you want to avoid manual scaling errors and improve system reliability.
Config File - kafka-autoscaler.yaml
kafka-autoscaler.yaml
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: kafka-broker-autoscaler
  namespace: kafka
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: StatefulSet
    name: kafka-broker
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 75

This file defines a Kubernetes Horizontal Pod Autoscaler for Kafka brokers.

scaleTargetRef points to the Kafka broker StatefulSet to scale.

minReplicas and maxReplicas set the scaling limits.

metrics define CPU and memory usage thresholds to trigger scaling.

Commands
This command creates the autoscaler in the Kubernetes cluster to manage Kafka broker scaling automatically.
Terminal
kubectl apply -f kafka-autoscaler.yaml
Expected OutputExpected
horizontalpodautoscaler.autoscaling/kafka-broker-autoscaler created
This command checks the status of the autoscaler to see current replicas and metrics.
Terminal
kubectl get hpa kafka-broker-autoscaler -n kafka
Expected OutputExpected
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE kafka-broker-autoscaler StatefulSet/kafka-broker 65%/70% CPU 3 10 4 2m
-n kafka - Specifies the Kafka namespace where the autoscaler is deployed
This command lists Kafka broker pods to verify how many are running after autoscaling.
Terminal
kubectl get pods -n kafka -l app=kafka-broker
Expected OutputExpected
NAME READY STATUS RESTARTS AGE kafka-broker-0 1/1 Running 0 5m kafka-broker-1 1/1 Running 0 5m kafka-broker-2 1/1 Running 0 5m kafka-broker-3 1/1 Running 0 1m
-l app=kafka-broker - Filters pods by label to show only Kafka broker pods
-n kafka - Specifies the Kafka namespace
Key Concept

If you remember nothing else from this pattern, remember: auto-scaling adjusts Kafka brokers automatically based on resource use to keep performance steady and costs low.

Common Mistakes
Setting minReplicas higher than needed causing unnecessary resource use.
It wastes resources and increases costs when traffic is low.
Set minReplicas to the lowest safe number of brokers for your workload.
Not specifying resource metrics like CPU or memory in the autoscaler config.
Without metrics, autoscaler cannot decide when to scale, so it won't work.
Always define clear resource usage targets to trigger scaling.
Applying autoscaler in the wrong Kubernetes namespace.
The autoscaler won't find the Kafka brokers to manage and will fail silently.
Use the correct namespace flag (-n kafka) when applying and checking autoscaler.
Summary
Create a Horizontal Pod Autoscaler YAML file to define scaling rules for Kafka brokers.
Apply the autoscaler configuration to Kubernetes to enable automatic scaling.
Check autoscaler status and Kafka broker pods to monitor scaling behavior.