How do you configure Prometheus Adapter to expose custom application metrics to the HPA?
❓ Interview Prompt: Explain the Custom Metrics API pipeline: Prometheus → Prometheus Adapter → Custom Metrics API → HPA, and PromQL query configuration.
Standard HPA scales on CPU/RAM (from Metrics Server). Prometheus Adapter bridges Prometheus metrics to the Kubernetes Custom Metrics API — enabling HPA to scale on any business metric collected by Prometheus.
Full Pipeline:
App exposes /metrics → Prometheus scrapes → Prometheus Adapter queries PromQL → Custom Metrics API → HPA reads → Scales Deployment
Prometheus Adapter Configuration:
Adapter config maps PromQL queries to Custom Metrics API paths. The seriesQuery identifies which Prometheus series to expose. The metricsQuery computes the value per pod/namespace.
Common Custom Metrics for HPA:
rate(http_requests_total[2m])rabbitmq_queue_messages_readywebsocket_connections_activehistogram_quantile(0.95, ...)rate(cache_misses_total[5m]) / rate(cache_requests_total[5m])Why Not Just Use CPU for Scaling:
HTTP request count is a leading indicator — scale up BEFORE CPU spikes. CPU is a lagging indicator — by the time CPU is high, users are already experiencing latency.
Code Implementation Example
# Prometheus Adapter ConfigMap — maps PromQL to Custom Metrics API
apiVersion: v1
kind: ConfigMap
metadata:
name: adapter-config
namespace: monitoring
data:
config.yaml: |
rules:
# Expose http_requests_per_second as a custom metric
- seriesQuery: 'http_requests_total{namespace!="",pod!=""}'
resources:
overrides:
namespace:
resource: namespace
pod:
resource: pod
name:
matches: "^(.*)_total$"
as: "${1}_per_second"
metricsQuery: 'rate(<<.Series>>{<<.LabelMatchers>>}[2m])'
---
# HPA using the custom metric
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa-custom
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 50
metrics:
# Scale when average HTTP RPS > 100 per pod
- type: Pods
pods:
metric:
name: http_requests_per_second # from Prometheus Adapter
target:
type: AverageValue
averageValue: "100" # 1 replica per 100 req/sPrometheus Adapter translates PromQL queries to Custom Metrics API — HPA can scale on any application metric. HTTP RPS is a leading indicator; always prefer business metrics over lagging CPU for autoscaling.