LAB21: Scale your Deployment based on custom application metrics already exposed in Prometheus.
The 3-Piece Orchestra
To make HPA understand Prometheus metrics, you need:
Application exposes metrics → Prometheus scrapes them
Prometheus Adapter → Translates Prometheus metrics into Kubernetes Custom Metrics API
HPA using custom metric → References the metric name via metrics.custom.metrics.k8s.io
Custom metric autoscaling with:
Prometheus (scraping the pod)
Prometheus Adapter (exposing metric to Kubernetes)
HPA that scales when myapp_queue_length > 10
This is the full, end-to-end pipeline for K3s, K8s, or RKE2. If you follow these steps in order, autoscaling will work.
PART 0 — Prerequisites
You need:
internet access for images
PART 1 — Deploy Your Dummy Queue App
Your app exposes one metric:
Let's deploy it properly with annotations so Prometheus scrapes it.
1.1 — Namespace
namespace.yaml
Apply:
1.2 — Deployment (with scrape annotations)
queue-app-deployment.yaml
queue-app-service.yaml
Apply:
For HPA custom metric scaling, you MUST have:
Prometheus scraping your dummy queue app
Prometheus Adapter running
Adapter rules mapping your metric
No Prometheus → HPA custom metrics will never appear.
Why Metrics Server Cannot Do Custom Metrics
The Metrics Server is a tiny gardener in the Kubernetes garden. It only harvests two crops:
That’s it. No queue length, no RPS, no latency, no business metrics. It’s intentionally minimal and lightweight.
Setting up . . .
This gives you:
Node exporter, kube-state-metrics, etc.
By default, this Prometheus will scrape pods with prometheus.io/scrape: "true".
Check pods:
Install Prometheus Adapter This exposes custom metrics to Kubernetes.
Create Adapter Rules file adapter-values.yaml
Create PodMonitor to scrape your queue app It only scrapes:
etc. Annotations are ignored.
podmonitor-queue.yaml
Verify adapter exposes metric
Once PodMonitor is active, the chain unlocks:
Prometheus scrapes myapp_queue_length
Prometheus Adapter exposes it via custom.metrics.k8s.io
Verify Prometheus is Scraping App
Open browser
Add HPA with Custom Metric
queue-app-hpa.yaml
Apply
Check
If success, Your Prometheus adapter is now:
correctly connected to Prometheus
successfully discovering your metric
mapping it to Kubernetes pod identities
exposing it via custom.metrics.k8s.io API
Test Auto-scaling
YOU ARE DONE. Your custom-metric autoscaler is fully working.
Your app → Prometheus → Adapter → HPA → Autoscaling pipeline is now operational.
Debugging Steps
Confirm Prometheus Adapter is actually
Get logs from the Prometheus Adapter
Some time we need to Remove ALL default adapter container rules