← All posts

Cutting Prometheus series by 45 % on MicroK8s

On MicroK8s every kubelet endpoint also serves API server, etcd and scheduler metrics. How I found the duplicated histograms with the TSDB status API and removed them with a few metric relabelings, without breaking a single alert or dashboard.

4 min read

On this page

My small homelab Prometheus kept growing: more memory, a larger TSDB, slower queries. There were not many more workloads, so something had to be producing far more series than it should. The answer turned out to be specific to MicroK8s, and the fix was a few lines of relabeling config.

Result: samples per scrape round went from roughly 308k to 168k (−45 %), and every alert rule and dashboard kept working.

Step 1: Find out what is big

Don't guess, measure. Prometheus has a built-in TSDB status API:

bash
curl -s http://prometheus:9090/api/v1/status/tsdb | jq '.data.seriesCountByMetricName[:15]'

It lists the metrics with the most series. To see which scrape job produces the most samples:

promql
topk(10, sum by (job) (scrape_samples_post_metric_relabeling))

For me, the top of both lists was surprising: a huge number of apiserver_* and etcd_* histogram buckets, and they came from the kubelet job, not the API server job.

Step 2: Why MicroK8s is different

On a standard cluster, the API server, scheduler and kubelet are separate processes, each with its own /metrics endpoint. MicroK8s bundles all control-plane components into a single process called kubelite.

As a side effect, every node's kubelet /metrics endpoint also serves the API server, etcd, scheduler and workqueue metrics from that process. kube-prometheus-stack scrapes the kubelet on every node and the API server separately, so the same expensive histograms end up in Prometheus several times.

Histograms are the expensive part. A single apiserver_request_duration_seconds histogram with ~40 buckets, multiplied by verbs, resources and status codes, quickly turns into tens of thousands of series.

Step 3: Drop the duplicates at the kubelet

kube-prometheus-stack exposes relabeling hooks for each ServiceMonitor. In the chart values, I drop the control-plane metrics from the kubelet job, because the API server job already collects them:

yaml
kubelet:
  serviceMonitor:
    metricRelabelings:
      # MicroK8s/kubelite: control-plane metrics are duplicated on every kubelet endpoint
      - sourceLabels: [__name__]
        action: drop
        regex: apiserver_.+_(bucket|sum|count)|(etcd|workqueue|scheduler|informer|apiextensions|aggregator|authentication|authorization|field_validation|watch)_.+

Be careful with the regex. It must not match kubelet metrics you still use, such as kubelet_*, volume_manager_* or kubelet_volume_stats_*, because the default alerts and dashboards depend on those.

Step 4: Keep only the cAdvisor metrics you use

cAdvisor (container metrics, also scraped from the kubelet) is the other big producer. Instead of a drop list, I use an allowlist of the container metrics that rules and dashboards actually query:

yaml
kubelet:
  serviceMonitor:
    cAdvisorMetricRelabelings:
      - sourceLabels: [__name__]
        action: keep
        regex: container_(cpu_usage_seconds_total|cpu_cfs_periods_total|cpu_cfs_throttled_periods_total|memory_working_set_bytes|memory_rss|memory_cache|memory_swap|oom_events_total|start_time_seconds|network_(receive|transmit)_(bytes|packets|packets_dropped)_total|fs_(reads|writes)(_bytes)?_total|spec_memory_limit_bytes|spec_cpu_quota|spec_cpu_period)|machine_.+
      # Network series for virtual CNI interfaces are noise
      - sourceLabels: [__name__, interface]
        action: drop
        regex: container_network_.*;(cali|cni|tunl|vxlan).*

The second rule removes per-interface network series for the CNI's virtual interfaces (Calico in my case). With a few dozen pods they add up fast and say nothing useful.

Step 5: Trim API server histograms nobody reads

The API server job itself also exposes histograms that no default rule or dashboard uses:

yaml
kubeApiServer:
  serviceMonitor:
    metricRelabelings:
      - sourceLabels: [__name__]
        action: drop
        regex: apiserver_(request_body_size_bytes|response_sizes|watch_events_sizes|watch_cache_read_wait_seconds|watch_list_duration_seconds)_bucket|etcd_request_duration_seconds_bucket

Note that metricRelabelings replaces the chart's default list. If the chart ships default rules for this ServiceMonitor, copy them into your values, or you lose them silently.

Step 6: Prove nothing broke

Before and after the change, check three things:

  1. All rules still evaluate:
    bash
    curl -s http://prometheus:9090/api/v1/rules | jq '[.data.groups[].rules[] | select(.health != "ok")] | length'
    

    It should print 0.
  2. No rule or dashboard queries a dropped metric. Grep the expressions of all rules (from /api/v1/rules) and all dashboard JSON (in kube-prometheus-stack, the ConfigMaps labelled grafana_dashboard=1) for the metric names your regexes match.
  3. Sample count really went down:
    promql
    sum(scrape_samples_post_metric_relabeling)
    

    Compare a value from before the change with one taken after.

Bonus: application histograms

The same approach works for chatty applications. One workflow engine in my cluster exported about 22k histogram bucket series that nobody looked at. A single rule on its PodMonitor removed them:

yaml
metricRelabelings:
  - sourceLabels: [__name__]
    action: drop
    regex: .+_bucket

_sum and _count are kept, so averages still work. Only percentiles go away.

Also worth checking

  • Dead config keys. After a chart upgrade, an option I relied on no longer existed and was silently ignored. Render the chart (helm template) and confirm that your settings actually end up in the generated ServiceMonitors.
  • Scrape interval and retention. In a homelab, a 60 s scrape interval and a 7-day retention are usually plenty and halve storage compared to 30 s.

Takeaway

On MicroK8s, check your kubelet job first: it probably contains a full copy of the control-plane metrics. Measure with the TSDB status API, drop what is duplicated, keep an allowlist for cAdvisor, and verify the result against your rules and dashboards. It is a short change with a big effect.