LAB09a: The 3-Piece Orchestra

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:

  1. Application exposes metrics → Prometheus scrapes them

  2. Prometheus Adapter → Translates Prometheus metrics into Kubernetes Custom Metrics API

  3. HPA using custom metric → References the metric name via metrics.custom.metrics.k8s.io


image

Setup

Custom metric autoscaling with:

  • Your Node.js queue app

  • 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:

  • kubectl

  • helm

  • k3s/k8s cluster running

  • 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


1.3 — Service

queue-app-service.yaml

Apply:



For HPA custom metric scaling, you MUST have:

  • Prometheus running

  • 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:

  • CPU usage

  • Memory usage

That’s it. No queue length, no RPS, no latency, no business metrics. It’s intentionally minimal and lightweight.

Setting up . . .

  1. Add Helm Repo

  1. Install Prometheus Stack

This gives you:

  • Prometheus server

  • Alertmanager

  • Grafana

  • Node exporter, kube-state-metrics, etc.

By default, this Prometheus will scrape pods with prometheus.io/scrape: "true".

Check pods:

  1. Install Prometheus Adapter This exposes custom metrics to Kubernetes.

  1. Create Adapter Rules file adapter-values.yaml

  1. Apply the custom rules

  1. Create PodMonitor to scrape your queue app It only scrapes:

  • ServiceMonitor

  • PodMonitor

  • Node exporter

  • Kube-state-metrics

  • Control-plane components

  • etc. Annotations are ignored.

podmonitor-queue.yaml

  1. Verify adapter exposes metric

Once PodMonitor is active, the chain unlocks:

  1. Prometheus scrapes myapp_queue_length

  2. Prometheus Adapter exposes it via custom.metrics.k8s.io

  3. HPA can finally read it

  4. Autoscaling begins

Verify Prometheus is Scraping App

  1. Port forward:

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.

References:

Debugging Steps

  1. Confirm Prometheus Adapter is actually

  1. Get logs from the Prometheus Adapter

  1. Test adaptor endpoint

Some time we need to Remove ALL default adapter container rules

Last updated