External Metrics and HPA
What you'll learn: How to use real-world signals from outside your cluster—like queue depth or cloud service metrics—to automatically scale your applications.
Beyond Built-In Metrics
So far, you've seen HPA scale based on CPU/memory (resource metrics) and custom metrics from within your cluster. But what if your scaling decision should be based on something external—like the number of messages in an AWS SQS queue, Azure Service Bus depth, or a Datadog metric tracking API response times from a CDN?
External metrics let HPA pull data from any monitoring system or cloud service that exposes metrics, making scaling decisions based on real business and operational signals rather than just pod-level resource usage.
How It Works
The External Metrics API connects your cluster to external data sources through adapters. Popular adapters include:
- KEDA (Kubernetes Event-Driven Autoscaling)
- Cloud provider adapters (AWS CloudWatch, Azure Monitor, GCP Stackdriver)
- Custom external metrics adapters for Datadog, Prometheus, etc.
When you reference an external metric in your HPA spec, Kubernetes queries the adapter, which fetches the current value from the external system. HPA then applies its scaling logic just as it would with any other metric type.
# Example: Scale based on SQS queue depth
metrics:
- type: External
external:
metric:
name: sqs_queue_messages
selector:
matchLabels:
queue: order-processing
target:
type: AverageValue
averageValue: "30"
This HPA would scale your pods to maintain roughly 30 messages per pod in the queue.
Key Takeaway: External metrics allow HPA to scale based on signals from cloud services and monitoring systems outside your cluster, enabling truly event-driven and business-metric-driven autoscaling.