Prompt
How do I auto scale Kubernetes workloads based on traffic?
Latest observation
To auto-scale Kubernetes workloads based on traffic, the usual approach is to scale on a metric that represents demand, such as:
- HTTP requests per second
- concurrent connections
- queue length
- CPU/memory as a proxy, if traffic metrics aren’t available
Kubernetes’ built-in HPA (Horizontal Pod Autoscaler) can scale pods automatically, but to scale based on traffic you typically need custom/external metrics.
Common ways to do it
1. Use HPA with CPU/memory
Simplest option, but it does not directly measure traffic.
Example:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
2. Use HPA with custom metrics
Best if you want to scale on app-specific traffic metrics like:
- requests/sec
- active sessions
- latency
- queue depth
You’ll need:
- metrics-server for resource metrics
- a custom metrics pipeline such as:
- Prometheus + Prometheus Adapter
- Datadog
- New Relic
- Cloud provider managed metrics
Then define HPA like:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 20
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: "100"
3. Use KEDA for event-driven autoscaling
If traffic is coming from a queue, stream, or external system, KEDA is often easier than raw HPA.
KEDA can scale based on things like:
- RabbitMQ queue length
- Kafka lag
- SQS queue depth
- Prometheus queries
- Azure Service Bus, etc.
Example:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: my-app
spec:
scaleTargetRef:
name: my-app
minReplicaCount: 1
maxReplicaCount: 20
triggers:
- type: prometheus
metadata:
serverAddress: http://prometheus:9090
metricName: http_requests_per_second
query: sum(rate(http_requests_total[1m]))
threshold: "100"
If you mean ingress/web traffic specifically
If traffic reaches your app through an Ingress controller, you can measure:
- request rate from the application itself
- ingress controller metrics
- load balancer metrics
Typical pattern:
- Expose request metrics from app (
/metrics) - Scrape with Prometheus
- Use Prometheus Adapter or KEDA
- HPA scales replicas based on those metrics
Important note
Scaling on traffic is often more effective than CPU because:
- CPU can lag behind traffic spikes
- app throughput may depend on request type, not just CPU
- queue-based or request-based scaling is more predictive
Recommended setup
For most production systems:
- HPA + Prometheus Adapter for HTTP request-based scaling
- KEDA for queue/event-driven workloads
- Keep CPU-based HPA as a fallback if needed
If you want, I can give you:
- a step-by-step setup using Prometheus + HPA, or
- a KEDA example for your specific traffic source.