Files
home-network-k3s-impl/runbooks/cnpg-exporter.md
T
jakman85andClaude Opus 5 44983095e1 feat(runbooks): make the 42 alert runbook_url annotations resolve
Every platform-health alert rule carries
`runbook_url: "https://${BASE_DOMAIN}/runbooks/<name>"` — 42 annotations over 34
distinct names, across platform/monitoring/alerts.yaml,
platform/monitoring/pvc-capacity-alerts.yaml, platform/cnpg/alerts.yaml and
platform/kubevirt/alerts.yaml. There was no runbooks/ directory and no HTTPRoute
serving that path: an in-cluster probe of
https://home.keithjackson.dev/runbooks/node-down returned HTTP 404. Every alert that
reached a human handed them a dead link at the exact moment the documentation is read,
and the annotation made it look solved — the same green-surface-over-absent-mechanism
class as the import Job that reported success while storing placeholder secrets.

No alerts.yaml is touched and no runbook_url value changes. The existing URL scheme is
made to resolve instead.

WHAT LANDS
- runbooks/<34>.md — one per name, each grounded in this cluster's real topology and,
  where one exists, its real incident history: the 2026-09-07 k3s-prod-1 etcd I/O
  starvation (measured PSI 11.36 vs 0.75/1.29, the exact journal lines, the four
  mechanisms proven inert), the Longhorn reboot tax and the freeze/thaw of
  concurrent-replica-rebuild-per-node-limit, Cilium's once-resolved egress interface
  cache (Ifindex 3 against a live 1780) and the absence of OSS failover, the CNPG
  38-hour ContinuousArchiving=False with its measured 6.8 MiB/h disk-full clock and the
  2026-09-12 aggregation-scope fix, the 2026-09-06 Keycloak lockout in which all 12
  clients held literal placeholder secrets behind an entirely green cluster, the Velero
  R2 x-amz-tagging NotImplemented incompatibility and its v1.14.3-rc.2 pin, and swap
  re-enabling itself across reboots via systemd-gpt-auto-generator.
  Each page states what the alert means HERE, the first diagnostic commands, likely
  causes ordered by probability with a remediation each, and when to escalate or stop.
- Every page uses FULLY-QUALIFIED resource names and says why: three CRDs register the
  kind `Backup` here, the bare short name resolves to Longhorn's (zero objects), so
  `kubectl get backup -A` prints "No resources found" on a cluster holding 6 Velero and
  27 CNPG backups. Acting on that false negative is how a CLI quirk becomes data loss.
- Every page states that kubectl only works via
  `infra/devcontainer/dev.sh run bash -c '...'` with an explicit `--context k3s-prod`,
  because dev.sh defaults its current-context to k3s-dev.
- Pages also record which alert groups are INERT today (measured 2026-09-13: no
  Longhorn, cert-manager, Flux-controller or Hubble scrape job exists, so 13 rules
  evaluate against nothing) so nobody reads their silence as health.

SERVING — runbooks/serving/ plus clusters/base/runbooks.yaml
A two-replica nginx-unprivileged serving a ConfigMap generated from runbooks/*.md, at
the apex `/runbooks` prefix, in its own non-gating post-platform Flux tier
(dependsOn platform-config, wait:false) — the platform/homepage pattern, file for file.
- The service catalog cannot express this route: ingressRoutes[].host is pattern-bound
  to a single DNS label plus the apex, so the bare apex is unrepresentable, and there is
  no path field.
- Rejected: redirecting to the Gitea web UI. A runbook is read when the platform is
  broken, and Gitea is an app-tier workload whose own dependencies several of these
  runbooks document; Gateway API's RequestRedirect also has no "append .md" modifier,
  so it would need 34 hand-maintained ReplaceFullPath rules.
- configMapGenerator maps each file to an EXTENSIONLESS key (node-down=node-down.md),
  so /runbooks/node-down is a plain static lookup with no rewrite rule. Served as
  text/plain: the namespace has DNS-only egress, so a CDN markdown renderer could not
  load anyway.
- The tier declares NO postBuild.substituteFrom, deliberately: envsubst would run over
  the prose and silently empty every ${...} shell example (it fails open — the 2026-09-01
  emptied Cilium k8sServiceHost). The HTTPRoute omits `hostnames:` and inherits the
  https-apex listener's, so nothing needs substituting.

AUTH — PUBLIC, deliberately
auth-mode=none. Gating these pages behind oauth2-proxy/Keycloak would make
runbooks/keycloak-down unreadable when Keycloak is down and
runbooks/keycloak-client-secrets unreadable during the exact 2026-09-06 failure mode.
An operator who cannot log in is this route's primary reader. Content is operational
procedure only, no secrets. The rationale is recorded in an annotation on the HTTPRoute
so the route is left OUT of the Gateway ext-authz attachment when forward-auth
enforcement lands.

PROVEN — in-cluster probe through the Gateway on prod
  /runbooks/node-down        HTTP 200   8613 bytes  text/plain; charset=utf-8
  /runbooks/keycloak-down    HTTP 200   9378 bytes  text/plain; charset=utf-8
  /runbooks/cnpg-archiving   HTTP 200  11729 bytes  text/plain; charset=utf-8
  /runbooks/longhorn-faulted HTTP 200   9135 bytes  text/plain; charset=utf-8
  /runbooks/vpn-down         HTTP 200  11092 bytes  text/plain; charset=utf-8
  /runbooks/backup-failure   HTTP 200   9604 bytes  text/plain; charset=utf-8
HTTPRoute Accepted by io.cilium/gateway-controller on sectionName https-apex; the
`/runbooks` prefix wins over homepage's apex `/` by Gateway API longest-path precedence.

GUARD — conformance/tests/l0-static/alert-runbooks-resolvable (R-OBS-04)
363 offline assertions. It checks BOTH halves, because the regression is silent in both
directions: an alert with a new runbook_url and no runbook links to a 404, AND a runbook
committed without its configMapGenerator entry is in git, reviewed, merged, and still
404s in the cluster. Also asserts a quality floor so a stub cannot make the link look
fixed, and that route/listener/nginx prefix agree with the annotations.
Proven to fail, then reverted: a bogus runbook_url (2 FAIL), a runbook removed from the
generator (1 FAIL), and a 31-byte stub (7 FAIL).

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-12 23:49:58 -05:00

9.5 KiB

cnpg-exporter — The CNPG metrics collector is erroring (a database is invisible)

Alert: CNPGMetricsCollectorErroring (warning, 6h) File: platform/cnpg/alerts.yaml, group platform-health.cnpg Expression: max by (namespace, pod) (cnpg_last_error) == 1

What this means here

This is the monitor's own blind spot, for databases — a failure that looks exactly like health.

CloudNativePG publishes two independent metric families and only one of them depends on SQL:

  • cnpg_collector_* comes from the instance manager and is always present.
  • cnpg_pg_stat_* (archiver, database, replication) comes from the exporter logging in to the application database as the cnpg_metrics_exporter role.

When that login fails, cnpg_last_error goes to 1 and the entire cnpg_pg_stat_* family blanks for that instance — while up stays 1 and cnpg_collector_up stays 1. Nothing looks broken. The scrape target is green. The dashboards have gaps that read as "no data" rather than "broken".

This is not hypothetical, and it shaped the whole alert file. Measured live on 2026-09-06 on both clusters, cnpg_errors_total carried:

pg_stat_archiver on db nextcloud: failed to connect to `user=cnpg_metrics_exporter database=nextcloud`:
FATAL: permission denied for database "nextcloud" (SQLSTATE 42501)

nextcloud-db had been publishing no pg_stat_archiver / pg_stat_database metrics at all. The consequence recorded verbatim: an archiving alert built on pg_stat_archiver — the obvious choice — would have been silently blind to one of three prod databases. That is why every DR rule in platform/cnpg/alerts.yaml keys on cnpg_collector_* instead, and why this rule exists to alert on the blind spot itself.

Scope: pod is kept here, deliberately and unchanged. cnpg_last_error is genuinely per-instance — each instance's own exporter reports its own SQL-collection health — and prod proved the metric discriminates rather than tracking role: all three nextcloud-db pods (including primary -2) reported 1 while all six keycloak/semaphore pods reported 0. There is no primary-only defect to fix, and per-pod is the right granularity because the remedy is a GRANT applied per database and you want to see it clear on every instance.

for: 6h keeps a pod restart or a brief connection blip quiet. Severity stays warning so it never becomes the permanently-firing critical this repo has been burned by.

Live state, measured 2026-09-13: max by (namespace,pod) (cnpg_last_error) == 1 returned empty across the 9 instances that publish cnpg_last_error — so the nextcloud grant appears to have been addressed. Re-measure rather than assuming either way.

Running the commands in this runbook

kubectl does not work on the operator workstation. The toolchain lives only in the pinned dev container, and the host's k3s-oidc context hangs on a browser prompt once its token expires — that hang is not the cluster being down. Every command below runs as:

infra/devcontainer/dev.sh run bash -c 'kubectl --context k3s-prod get nodes'

Always pass --context k3s-prod explicitly. dev.sh merges both clusters into one kubeconfig and defaults its current-context to k3s-dev, so a command without --context silently answers about the wrong cluster. (CLUSTER=prod infra/devcontainer/dev.sh run kubectl ... is equivalent.) Do not run two dev.sh invocations at once — they share one container and interfere.

USE FULLY-QUALIFIED RESOURCE NAMES, ALWAYS. Three CRDs on this cluster register the kind Backup: longhorn.io/v1beta2, postgresql.cnpg.io/v1 and velero.io/v1. The bare short name resolves to Longhorn's, which holds zero objects — so kubectl get backup -A prints "No resources found" on a cluster holding 6 Velero backups and 27 CNPG backups. Verified on prod 2026-09-13. Reading that as "the backups are gone" and acting on it turns a false negative into real data loss. Write backups.velero.io, backups.postgresql.cnpg.io, clusters.postgresql.cnpg.io, settings.longhorn.io, volumes.longhorn.io, nodes.longhorn.io.

Mutating kubectl (apply, patch, annotate, rollout) and flux reconcile are blocked for agents here: every durable change goes through git (git push origin main is the deploy). Where this runbook gives a mutating command it is for an operator, and it says so.

First commands

Which instances are erroring:

infra/devcontainer/dev.sh run bash -c \
  'kubectl --context k3s-prod -n monitoring port-forward svc/kube-prometheus-stack-prometheus 19090:9090 & sleep 5; \
   curl -s -G --data-urlencode "query=max by (namespace,pod) (cnpg_last_error)" localhost:19090/api/v1/query | python3 -m json.tool | grep -E "pod|\"1\""'

The message itself — cnpg_errors_total carries it in an errorUserQueries label. This is the one command that tells you the cause outright:

infra/devcontainer/dev.sh run bash -c \
  'kubectl --context k3s-prod -n monitoring port-forward svc/kube-prometheus-stack-prometheus 19090:9090 & sleep 5; \
   curl -s -G --data-urlencode "query=cnpg_errors_total" localhost:19090/api/v1/query | python3 -m json.tool | head -60'

Or straight from the instance's metrics endpoint:

infra/devcontainer/dev.sh run bash -c \
  'kubectl --context k3s-prod -n nextcloud-db exec nextcloud-db-2 -c postgres -- \
     curl -s localhost:9187/metrics | grep -E "^cnpg_last_error|^cnpg_errors_total|^cnpg_collector_up" | head -20'

Confirm which family is actually missing — this is what makes the blind spot visible:

infra/devcontainer/dev.sh run bash -c \
  'kubectl --context k3s-prod -n monitoring port-forward svc/kube-prometheus-stack-prometheus 19090:9090 & sleep 5; \
   for q in "count(cnpg_collector_up)" "count(cnpg_last_error)" "count(cnpg_pg_stat_archiver_archived_count)" "count(cnpg_pg_stat_database_xact_commit)"; do \
     printf "%-50s " "$q"; curl -s -G --data-urlencode "query=$q" localhost:19090/api/v1/query \
       | python3 -c "import json,sys;print([x[\"value\"][1] for x in json.load(sys.stdin)[\"data\"][\"result\"]] or \"ABSENT\")"; done'

The exporter role's actual grants:

infra/devcontainer/dev.sh run bash -c \
  'kubectl --context k3s-prod -n nextcloud-db exec nextcloud-db-2 -c postgres -- \
     psql -tAc "select rolname, rolcanlogin from pg_roles where rolname = \"cnpg_metrics_exporter\";" \
     -c "select datname, has_database_privilege(\"cnpg_metrics_exporter\", datname, \"CONNECT\") from pg_database where datistemplate = false;"'

Likely causes, most likely first

1. The exporter role lacks CONNECT on the application database

The recorded cause. Signature: SQLSTATE 42501, permission denied for database "<name>" in cnpg_errors_total. Remedy: grant the cnpg_metrics_exporter role CONNECT on the application database. Do it declaratively — CNPG can manage roles and the managed.roles / bootstrap surface in the Cluster spec is where this belongs, so the grant survives a re-seed. A psql GRANT by hand fixes the symptom on one instance and vanishes on the next bootstrap.

2. Custom monitoring queries reference something that does not exist

CNPG lets a Cluster attach extra collector queries via a ConfigMap. A query against a missing table, extension or column fails the whole collection pass for that instance. cnpg_errors_total's label names the failing query.

3. The database is not accepting connections

A read-only/recovery state, max_connections exhausted, or the instance mid-restart. Cross-read /runbooks/keycloak-db if the instance is keycloak-db-*. Note the deliberate setting dataDurability: preferred (never required) exists so a degraded standby cannot block writes here.

4. An upgrade changed the exporter's expectations

CNPG 1.30.0 is the verified operator version on both clusters. A major operator or Postgres bump can change the collector's queries; if this started at an upgrade, read /runbooks/update-rollback.

5. The instance is fine and the whole scrape is gone

Then up for that job would be 0 and this is the wrong runbook — /runbooks/targets-down. The precise value of this alert is the case where up is 1 and the data is still missing.

When to escalate or stop

  • Understand what you are currently blind to. While this fires, cnpg_pg_stat_archiver, cnpg_pg_stat_database and cnpg_pg_stat_replication are missing for that instance. The DR alerts in platform/cnpg/alerts.yaml are deliberately built on cnpg_collector_* so they still work — but do not build any new alert or dashboard on pg_stat_* without checking this first. That is the trap this alert exists to mark.
  • Do not silence it. It is a warning with a 6h for precisely so it can be left visible without becoming noise, and it was a true positive for months.
  • Do not grant the exporter role broad privileges to make it go away. CONNECT on the application database is the whole requirement; anything more widens the blast radius of a metrics credential.
  • Do not restart the database to clear it — the error is a grant, not a state.
  • Escalate to the operator if the fix requires changing the Cluster's managed roles or bootstrap, since that touches the database's declarative definition and its DR path.

Source of truth: runbooks/cnpg-exporter.md. Index: /runbooks/ Records: platform/cnpg/alerts.yaml (the 2026-09-06 SQLSTATE 42501 measurement on nextcloud-db, the two-metric-families finding, and the 2026-09-12 scope audit that deliberately kept pod).