Skip to main content
In this lesson we deploy a production-ready monitoring stack (Prometheus + Grafana) using the community kube-prometheus-stack Helm chart, then explore real Kubernetes metrics (kubelet, kube-apiserver, kube-state-metrics) and how to query them in Grafana and Prometheus. This walkthrough covers how to fetch node and control-plane metrics, deploy the stack with Helm, and build useful PromQL queries for dashboards and alerts.

1) Inspect kubelet metrics (via kubectl proxy)

Each kubelet exposes Prometheus-format (OpenMetrics) metrics on the node, but the endpoint is authenticated. Use kubectl to proxy through the API server and save a node’s kubelet metrics locally:
Kubelet metrics use Prometheus exposition format. Example histogram fragment:
Gauges are also present. For example, the kubelet exposes a gauge showing how many pods it is running:
Example output:
Note: gauges represent instantaneous values (they can increase or decrease), while counters are cumulative.

2) Inspect kube-apiserver metrics

The kube-apiserver exposes a high-cardinality, comprehensive metrics set. Fetch it using kubectl:
Typical metrics include workqueue durations, request latency histograms, and request counters:
The kube-apiserver metrics file can be large (example sample: ~21600 lines), so filter and sample when exploring.

3) Prometheus metric patterns and histograms

Prometheus histogram exposition generates multiple series per logical histogram:
  • <metric>_bucket{le="..."} ... — counts up to each bucket bound
  • <metric>_sum — total sum of all observations
  • <metric>_count — total observation count
Together these describe a distribution. To produce percentiles (P50, P95, P99) from histogram buckets in PromQL, use histogram_quantile() over an aggregated sum by (le) ( rate(..._bucket[range]) ).
Use the PromQL rate() (or increase()) function for counter metrics (cumulative counters). For latency percentiles from histograms use histogram_quantile() with an aggregated *_bucket time rate.

4) Deploy kube-prometheus-stack with Helm

The community kube-prometheus-stack Helm chart packages Prometheus Operator, Prometheus, Alertmanager, Grafana, kube-state-metrics, and default ServiceMonitors. Install it to get a production-oriented stack quickly. Add the Helm repo and update:
Edit a values.yaml to control retention, scrape intervals, resources, and which ServiceMonitors to enable. Example values.yaml excerpt:
Install the chart:
Helm prints NOTES with commands to retrieve the Grafana admin password and port-forward:
Verify pods are running:
Example output (trimmed):
kube-prometheus-stack uses the Prometheus Operator and ServiceMonitors to dynamically discover and scrape targets, and includes many pre-built Grafana dashboards and a pre-configured Prometheus data source.

5) Query metrics in Grafana Explore / Prometheus

Open Grafana (via port-forward or your configured ingress) and use Explore (compass icon) for ad-hoc PromQL queries. Useful queries:
  • Rate of API server requests (per second over 5 minutes):
  • Filter for successful HTTP 2xx responses (regex on code label):
  • Aggregate rate by HTTP verb:
Why rate()? Counters are cumulative; rate() returns a per-second average over the window and is appropriate for dashboards and alerts.

6) Histograms and percentiles (P50, P95, P99)

Calculate percentiles from histogram buckets with histogram_quantile(): Template:
Example — 99th percentile of API server request latency:
Interpretation:
  • A value of 0.05 indicates 99% of requests completed within 0.05 seconds (50 ms).
  • Use histogram_quantile(0.5, ...) for P50 (median).
  • Always aggregate buckets (sum by (le)) across series before applying histogram_quantile().

7) Counters vs. Histograms vs. Gauges

Use this quick reference for metric types and how to query them:

8) Exercise: Top containers by memory in the monitoring namespace

List the top 5 containers by working set memory (GB) in the monitoring namespace:
This returns the top namespace/pod/container combinations by memory (in GB). Typical top consumers in a monitoring namespace include:
  • Grafana (main container)
  • Grafana sidecar (dashboards/datasources)
  • Prometheus server
  • kube-state-metrics
Why these consume memory:
  • Prometheus keeps in-memory indexes and recent time-series; more targets → higher memory.
  • Grafana and sidecars load dashboards and datasources into memory.
  • Sidecars may maintain caches and also consume memory.

Final notes and tuning guidance

  • Tune Prometheus retention, scrape intervals, and target selection to balance observability needs and resource/cost constraints.
  • Use rate() for counters, histogram_quantile() for histogram percentiles, and raw values/aggregations for gauges.
  • The kube-prometheus-stack Helm chart simplifies deployment and includes many pre-built dashboards and ServiceMonitors that mirror the metrics you inspected manually.

Watch Video