Skip to content

fix(k8s): the prometheus server scrapes only itself and the otel collector, and k8s-monitoring discovers every other target #1130

Closed

Description

@dan-petty

Problem

The cluster has two scrapers with two conventions. Alloy, run by k8s-monitoring, scrapes kubelet, cAdvisor, kube-state-metrics, node-exporter, DCGM and the llm gateway. The prometheus-community server (chart 29.35.0) runs its own Kubernetes service discovery beside it, mostly with chart-default jobs. No target is scraped twice today, but the server's half is unfiltered, partly dead, and hard-codes the cluster name five times. The k8s-monitoring chart already has features that cover every target the server's discovery finds. Checked through /api/v1/targets?state=any and /api/v1/status/config on 2026-10-03.

The API-server job holds three quarters of all series, and nothing reads them. scrapeConfigs.kubernetes-api-servers (k8s/monitoring/prometheus-values.yaml:112-116) is on by chart default, with only a cluster relabel.

  • count({job="kubernetes-api-servers"}) = 52,175, while prometheus_tsdb_head_series = 68,852 (75.8%).
  • The largest families: apiserver_request_duration_seconds_bucket 11,568, etcd_request_duration_seconds_bucket 8,016, apiserver_request_sli_duration_seconds_bucket 7,964, apiserver_request_body_size_bytes_bucket 4,576.
  • git grep -E 'apiserver_|etcd_|rest_client_|workqueue_' -- k8s/ finds nothing: no dashboard, rule or provisioned dashboard (1860, 12239) uses them. The only consumer is TargetDown (up == 0).

Four jobs match nothing. These are chart defaults left on while prometheus-pushgateway.enabled is false:

  • kubernetes-services (blackbox probes; 85 dropped targets, 0 active);
  • prometheus-pushgateway (85 dropped, 0 active);
  • kubernetes-pods-slow (111 dropped, 0 active);
  • kubernetes-service-endpoints-slow (138 dropped, 0 active).

The remaining targets are found through prometheus.io/* annotations.

  • kubernetes-pods finds 2 cloudflared pods, traefik, the otel collector, loki-0 and prometheus-alertmanager-0.
  • kubernetes-service-endpoints finds one target, kube-system/kube-dns:9153. That answers feat(k8s): coredns metrics and a coredns dashboard #822's open question: CoreDNS is already scraped. Turning on clusterMetrics.kubeDNS without removing this job would scrape it twice.
  • Loki is scraped with no allow list (3,303 loki_* series), through the podAnnotations in k8s/logging/loki-values.yaml:48-52.
  • DCGM is scraped through a hand-relabelled ServiceMonitor (k8s/monitoring/dcgm-exporter-values.yaml:42-58; the relabel copies the pod's node name to node and instance). The chart's integrations.dcgm-exporter scrapes it natively (job integrations/dcgm-exporter).

The cluster label is hard-coded six times and missing once. cluster.name is set in k8s-monitoring-values.yaml, and five post_relabel_configs blocks in prometheus-values.yaml repeat the cluster name as a literal replacement (the same value as cluster.name). The server's self-scrape (job="prometheus") has no cluster label, and neither does prometheus_tsdb_head_series.

Two constraints the move must keep:

  • The collector's scrape stays on the server, and not through Alloy, "so the collector's export failures stay visible when Alloy is what fails" (k8s/README.md:181, k8s/otel/values.yaml:116-118).
  • The node-maintenance silence relies on the kubernetes-pods job setting node from __meta_kubernetes_pod_node_name (k8s/README.md:275). annotationAutodiscovery sets no node label by default; the DCGM rules (max by (node, gpu)) and dashboard 12239 also need node.

No rule or dashboard selects job="kubernetes-pods", "kubernetes-api-servers" or "kubernetes-service-endpoints". Rules filter only job="prometheus", and no rule or dashboard filters job="dcgm-exporter". So the job renames need no query rewrite.

No document says which monitoring components stay outside k8s-monitoring and why. The k8s/README.md describes k8s-monitoring-values.yaml only as "Alloy, kube-state-metrics, node-exporter, node journals, and gateway monitors".

Acceptance criteria

  • In k8s/monitoring/prometheus-values.yaml, scrapeConfigs sets enabled: false on six jobs: kubernetes-api-servers, kubernetes-service-endpoints, kubernetes-service-endpoints-slow, kubernetes-pods-slow, kubernetes-services and prometheus-pushgateway. Their relabel blocks are deleted.

  • kubernetes-pods stays enabled. Its pre_relabel_configs keep only namespace otel, and a comment cites the README's independence reason.

  • prometheus and kubernetes-pods take their cluster relabel from one YAML anchor in the same file. The self-scrape carries cluster.

  • In k8s/monitoring/k8s-monitoring-values.yaml, annotationAutodiscovery is set as follows:

    • enabled: true, collector: alloy-metrics;
    • annotations.scrape, annotations.metricsPortNumber and annotations.metricsPath set to prometheus.io/scrape, prometheus.io/port and prometheus.io/path;
    • excludeNamespaces: [otel] and services.enabled: false;
    • extraDiscoveryRules that copy __meta_kubernetes_pod_node_name to node.
  • clusterMetrics.kubeDNS.enabled: true is set.

  • integrations.collector: alloy-metrics is set, with two instances:

    • loki: labelSelectors {app.kubernetes.io/name: loki, app.kubernetes.io/component: single-binary}, with the chart's default allow list;
    • dcgm-exporter: labelSelectors {app.kubernetes.io/name: dcgm-exporter}, metrics.scrapeInterval: 15s, and extraDiscoveryRules that set node and instance from __meta_kubernetes_pod_node_name.
  • k8s/logging/loki-values.yaml no longer sets the prometheus.io/* podAnnotations. k8s/monitoring/dcgm-exporter-values.yaml sets serviceMonitor.enabled: false and drops its relabelings.

  • A test in tests/test_k8s_monitoring_integration.py asserts one structural tuple containing:

    • the server's enabled jobs, ("kubernetes-pods", "prometheus");
    • the namespace kubernetes-pods keeps, otel;
    • the autodiscovery annotation keys, excludeNamespaces and services.enabled;
    • that the node rule is present in autodiscovery and in the dcgm-exporter integration;
    • clusterMetrics.kubeDNS.enabled;
    • the Loki and DCGM integration selectors;
    • the absence of Loki's scrape annotations and of the DCGM ServiceMonitor.
  • A second test asserts that each annotated component in the repo's values has exactly one scrape owner. Verified by mutations in review, each of which must make it fail:

    • Alertmanager's prometheus.io/scrape (prometheus-values.yaml:51-53): autodiscovery only;
    • the collector's annotations (otel/values.yaml): the server job only;
    • removing otel from excludeNamespaces;
    • re-adding Loki's annotations.
  • tests/test_stack_dashboards.py (_dcgm_monitored, _collector_annotated) reads the new owners. The DCGM capture fixture's job label changes to integrations/dcgm-exporter if a test compares it.

  • k8s/README.md gains a component-ownership table, one row per component, giving the owner (a k8s-monitoring feature or a separate release) and the reason. Rows:

    README:181 and README:275 describe the new scrape owners and where node comes from.

  • Every scrape that moves to the Alloy collectors stays reachable under the monitoring perimeter: k8s/monitoring/networkpolicy.yaml has exactly one egress rule per scrape port the collectors now use (for example kube-system 9153 for clusterMetrics.kubeDNS, which the Prometheus server reached before). A test reads the policy and the scrape owners from the repo and fails on a scrape port with no egress rule, or with two.

  • No test reaches the network or runs helm. Each new test's call phase is under 1 s, and uv run devops ci stays within budget. A changelog.d/<n>.md fragment exists; CHANGELOG.md and docs/ROADMAP.md are untouched.

  • Pending a person, after the infra stack is redeployed, in Grafana Explore against Prometheus 15 minutes later:

    • count by (job) (up) shows prometheus, kubernetes-pods (one target, the collector), the autodiscovery jobs, which take each pod's app.kubernetes.io/name (cloudflared, traefik, alertmanager), integrations/kubernetes/kube-dns, integrations/loki and integrations/dcgm-exporter, and none of the six disabled jobs;
    • count by (job, node) (up{job!~"integrations/kubernetes/.*"}) has a node on every pod target;
    • count by (node, gpu) (DCGM_FI_DEV_GPU_TEMP) equals the GPU count;
    • count({cluster=""}) is empty;
    • three hours later, prometheus_tsdb_head_series is about 50k lower than before.

Key questions

  • Why not let Alloy scrape everything, the collector included? Proposed: the documented design keeps the collector on the server so that an Alloy failure can't hide the collector's export failures (README:181). The narrowed kubernetes-pods job is the only server-side discovery left, and the ownership table records why.
  • Use the chart's k8s.grafana.com/* annotation keys or the existing prometheus.io/*? Proposed: prometheus.io/*, configured through annotationAutodiscovery.annotations. Traefik is k3s-managed and already carries prometheus.io/* (path, port 9100, scrape), and so do cloudflared and Alertmanager. Re-annotating would need a k3s HelmChartConfig and a cloudflared manifest edit for no gain.
  • Why services.enabled: false? Proposed: the only annotated Service is kube-dns, and clusterMetrics.kubeDNS scrapes it with the chart's CoreDNS allow list. Leaving service discovery on would scrape CoreDNS twice.
  • Should API-server health be scraped through clusterMetrics.apiServer? Proposed: no, not in this item. No dashboard or rule reads it. If a consumer appears, enable it with metricsTuning.includeMetrics limited to what that consumer reads (for example apiserver_request_total, apiserver_request_duration_seconds_sum/_count). kube-state-metrics and kubelet scrapes fail visibly when the API server is down.
  • Does moving DCGM to the integration keep prometheusOperatorObjects and the prometheus-operator-crds release? Proposed: yes. The llm-gateway ServiceMonitor (moved under fix(k8s): deploy-stack applies the llm gateway policy and narrows its egress #795) and feat(k8s): qdrant metrics with an authenticated scrape, and its dashboard row restored #823's authenticated Qdrant monitor still need both.
  • Can the network perimeters carry the move? Proposed: yes. Every target namespace admits the monitoring namespace by namespaceSelector (cloudflared on 2000, logging on 3100), and alloy-metrics runs in monitoring like the server. When the planned follow-up narrows the monitoring peer on Loki to named pods, it must keep alloy-metrics.
  • Is the cluster relabel on the server still needed after the move? Proposed: yes, on the two jobs that remain. Prometheus applies external_labels only to remote-write, federation and alerts, not to local series, so server.global.external_labels is not a substitute.

Strategic alignment

Activity

  1. added
    scope/k8s
    scope/telemetry
    priority/p1-high
    on Oct 3, 2026
  2. added this to the v0.2.33 milestone on Oct 9, 2026
  3. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    Moved to v0.2.33: v0.2.33 started with fewer than its 12 items and was topped up to 12 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  4. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    What changed

    #1527, merged into release/v0.2.33 as 5845924.

    Summary

    • The Prometheus server keeps two scrape jobs: prometheus and kubernetes-pods, which now keeps only namespace otel, so the collector's export failures stay visible when Alloy is what fails. Both take the cluster relabel from one &cluster anchor, so the self-scrape now carries cluster. The six other chart-default jobs are off, among them the unread API-server scrape (about 60% of head series, 59.7k of 99.6k on 2026-10-09).
    • k8s-monitoring takes over every other target with chart features:
      • annotationAutodiscovery: prometheus.io/* keys, otel excluded, Service discovery off, no ServiceAccount token;
      • clusterMetrics.kubeDNS;
      • the Loki integration (single-binary selector, metrics only) and the dcgm-exporter integration (15s).
    • Autodiscovery, kube-dns and DCGM copy the pod's node to node, so the node silence and the NodeNotReady inhibition keep covering their TargetDown.
    • Loki loses its scrape annotations. The DCGM chart's ServiceMonitor is off, and its Argo CD Application drops SkipDryRunOnMissingResource, which nothing needs any more. Alertmanager names its container, so the config-reloader sidecar's target does not scrape it twice.
    • k8s/README.md gains a Scrape Ownership table of the 8 components that run outside k8s-monitoring, with the owner and the reason. The stale scrape, node, Loki-retention and alloy-metrics-0 text is rewritten, along with the comments in prometheus-operator-crds, stack_lifecycle.py and the prometheus-se…

    24 file(s) changed.

    How it was verified

    Check runs at b4f48f2:

    • Prune PR DevContainer Images: pass
    • CodeQL: pass
    • Static Analysis: pass
    • Analyze (actions): pass
    • Analyze (python): pass
    • Tests & Coverage: pass
    • Changed-Test Independence: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1130-prometheus-scrapes-itself-and-the-collector.md:

    • k8s/monitoring/prometheus-values.yaml sets enabled: false on kubernetes-api-servers, kubernetes-service-endpoints, kubernetes-service-endpoints-slow, kubernetes-pods-slow, kubernetes-services and prometheus-pushgateway, with their relabel blocks deleted. kubernetes-nodes and kubernetes-nodes-cadvisor stay off.

    • kubernetes-pods stays enabled, and its pre_relabel_configs keep only namespace otel. Its comment cites the README's independence reason.

    • prometheus and kubernetes-pods take their cluster relabel from one anchor (&cluster). The self-scrape carries cluster.

    • annotationAutodiscovery is set as follows:

      • enabled: true, collector: alloy-metrics;
      • the prometheus.io/scrape, prometheus.io/port and prometheus.io/path annotation keys;
      • excludeNamespaces: [otel], services.enabled: false and bearerToken.enabled: false;
      • the node rule in extraDiscoveryRules.
    • clusterMetrics.kubeDNS.enabled: true, with the same node rule.

    • integrations.collector: alloy-metrics, with two instances:

      • loki: labelSelectors {app.kubernetes.io/name: loki, app.kubernetes.io/component: single-binary}, the chart's default allow list, and logs.enabled: false;
      • dcgm-exporter: labelSelectors {app.kubernetes.io/name: dcgm-exporter}, metrics.scrapeInterval: 15s, and discovery rules that set node and instance from __meta_kubernetes_pod_node_name.
    • k8s/logging/loki-values.yaml sets no podAnnotations. k8s/monitoring/dcgm-exporter-values.yaml sets serviceMonitor: {enabled: false} and nothing else under it.

    • test_scrape_ownership_moves_to_k8s_monitoring asserts all of the above as one tuple, including Alertmanager's three annotations. The enabled server jobs are computed over the 10 default jobs in prometheus 29.35.0's values.yaml (PROMETHEUS_DEFAULT_JOBS), so a default job that the values leave out counts as on.

    • test_each_annotated_component_has_one_scrape_owner asserts exactly one owner for each of cloudflared, roadmap-service, Alertmanager, the collector, Loki and DCGM. The owners are the server's kubernetes-pods job, autodiscovery, each integration instance, and the DCGM chart's own ServiceMonitor. test_a_component_with_no_scrape_owner_or_two_fails runs the four review mutations, and each one fails the check:

      • Alertmanager without its scrape annotation;
      • the collector without its annotations;
      • otel removed from excludeNamespaces;
      • Loki annotated again.
    • test_every_scrape_port_has_one_egress_rule counts the egress rules in k8s/monitoring/networkpolicy.yaml for each scrape port, using one counting helper, _egress_rules_to. Each port has exactly one rule: cloudflared 2000, devops 8000, kube-system 9100 (Traefik) and 9153 (CoreDNS), logging 3100, monitoring 9093 and 9400, and otel 8888. The policy needed no change.

    • tests/test_stack_dashboards.py reads the new owners:

      • _dcgm_monitored reads the k8s-monitoring dcgm-exporter integration;
      • _collector_annotated also requires the server job to keep namespace otel.

      The DCGM and Prometheus server capture fixtures name their new scrapes and target labels. No test compares the DCGM job label.

    • k8s/README.md has a Scrape Ownership section. Its table has one row for each of the eight components, with the owner and the reason, and the lines after it name the autodiscovered pods and what devops prometheus targets lists. The dashboard table, Log and Metric Retention, LokiRetentionNotRunning, Planned maintenance (where node comes from, and what stops with alloy-metrics-0) and the directory listing describe the new owners.

    • No test reaches the network or runs helm. Each new test's call phase is at most 0.02 s (uv run pytest tests/test_k8s_monitoring_integration.py --durations=0). changelog.d/1130.md exists; CHANGELOG.md and docs/ROADMAP.md are untouched.

    • Review render, outside the gate: helm template of the cached k8s-monitoring 4.5.2 and prometheus 29.35.0 charts with the new values.

      • The k8s-monitoring render only adds Alloy configuration. The autodiscovery scrape has no bearer_token_file.
      • The node rule renders in annotation_autodiscovery_pods, kube_dns and the DCGM instance. DCGM's scrape_interval is "15s", and no Loki log rule renders.
      • prometheus.yml has only the kubernetes-pods job, whose first rule keeps otel, and the prometheus job. Both end with the cluster replace rule.
    • Pending a person, after the v0.2.33 release PR merges into main and Argo CD syncs the prometheus, k8s-monitoring, dcgm-exporter and loki Applications. They track main, so do not apply early with devops k8s deploy-stack from the release branch: Argo CD would revert it.

      1. kubectl get application -n argocd prometheus k8s-monitoring dcgm-exporter loki -o custom-columns=NAME:.metadata.name,SYNC:.status.sync.status,HEALTH:.status.health.status,REV:.status.sync.revisions shows each one Synced and Healthy at the merge commit.

        • kubectl get servicemonitor -A lists only llm/llm-gateway.
        • kubectl -n logging get pod loki-0 and kubectl -n monitoring get pod prometheus-alertmanager-0 show pods restarted since the sync, because their pod template annotations changed.
        • In Grafana Explore against Prometheus, prometheus_config_last_reload_successful is 1.
      2. Fifteen minutes later, count by (job) (up) returns these jobs and no others besides the unchanged Alloy jobs (kubelet, cAdvisor, resources, kube-state-metrics, node_exporter, llm-gateway):

        • prometheus 1 and kubernetes-pods 1;
        • cloudflared 2, traefik 1, alertmanager 1 and roadmap-service 1;
        • integrations/kubernetes/kube-dns, equal to the CoreDNS pods in kubectl -n kube-system get pods -l k8s-app=kube-dns;
        • logging/single-binary 1;
        • integrations/dcgm-exporter, equal to the GPU nodes in kubectl get nodes -l nvidia.com/gpu.present=true.

        No kubernetes-api-servers, kubernetes-service-endpoints or dcgm-exporter job is left.

      3. count(alertmanager_build_info) is 1, so Alertmanager is scraped once.

      4. count by (job) (up{node=""}) lists only prometheus, llm-gateway, integrations/kubernetes/kube-state-metrics, integrations/node_exporter and logging/single-binary.

      5. count by (node, gpu) (DCGM_FI_DEV_GPU_TEMP) equals the number of GPUs, and the instance variable of dashboard 12239 lists node names.

      6. loki_compactor_apply_retention_last_successful_run_timestamp_seconds and coredns_dns_requests_total each return series.

      7. count({__name__=~".+", __name__!~"ALERTS.*", cluster=""}) is empty.

      8. ALERTS{alertname="TargetDown"} holds no alert for the new jobs.

      9. Three hours later, prometheus_tsdb_head_series is about 60k lower than before the sync (about 99.6k on 2026-10-09).

  5. added a commit that references this issue on Oct 10, 2026
  6. added
    type/bug
    and removed on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/k8sscope/telemetrytype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions