homek8s/docs/grafana-persistence.md
Lance Walters 6b91152a79 monitoring(logs): a readable Loki — Logs dashboard + provisioning-reload runbook
The log store shipped in t_34ddaa83 but nothing could read it: the Loki datasource
ConfigMap was live and written into Grafana's provisioning volume, and Grafana still
listed only Prometheus + Alertmanager. Root cause was not the ConfigMap. The
datasource sidecar writes the file and then POSTs
/api/admin/provisioning/datasources/reload with monitoring/grafana-admin, which was
401-ing because Grafana's DB password had drifted from that Secret (the documented
"the Secret is only the seed" behaviour) — Grafana reads provisioning/datasources
only at startup, so the datasource never loaded.

- Reset the live admin password to the Secret's value (in-pod `grafana cli
  admin reset-admin-password`, value read from the Secret and never printed), so
  seed and DB match again and the sidecar's reload returns 200. Documented in
  docs/grafana-persistence.md, including the sidecar consequence of drift.
- New generated dashboard `logs` (uid: logs) with the two queries worth having:
  secret access over time by user from the apiserver audit stream, and pod errors
  by namespace, plus the runner namespaces and a note panel on field names.
- The audit stream's names are the Alloy allowlist's, not the audit event's:
  objectRef.resource arrives as the stream label `resource` and user.username as
  the field `user`. `| json | objectRef_resource="secrets"` returns 0 rows
  (measured); the panels use `resource="secrets"` in the selector + `| json` for
  the user dimension. No pipeline, retention or filter change.
- Runner panels discover their namespace via a `runner_ns` variable
  (.*(runner|forgejo).*) instead of pinning one name, so the t_2bbe34a8 move to
  ci-runners needs no edit here.
- validate_panels.py now dispatches on the target's datasource and queries Loki
  for `loki` panels (a logs panel must return rows, not just series).
2026-11-27 01:23:05 -05:00

6.3 KiB

Grafana persistence and the admin password

Grafana (monitoring/kube-prometheus-stack-grafana) used to run with no persistence at all: /var/lib/grafana was an emptyDir, so its sqlite DB (grafana.db) — the admin user row, sessions, API keys, alert state and any UI-created dashboard — died with the pod. GF_SECURITY_SECRET_KEY was also unset, so every restart generated a fresh session-signing key. The visible symptom was a browser holding a session cookie that could never validate: logger=authn.service msg="Failed to authenticate request" client=auth.client.session error="user token not found" followed by path=/ status=302, i.e. a client bouncing on / — which looks like "Grafana is unreachable" but is not a network fault.

Both are fixed in monitoring/controllers/home/values.yaml:

What Where
DB + plugins PVC monitoring/kube-prometheus-stack-grafana, 5Gi local-path, mounted at /var/lib/grafana (Grafana's GF_PATHS_DATA)
GF_SECURITY_SECRET_KEY Secret monitoring/grafana-secret-key, key secret_key (out of band — not in git), injected with grafana.envValueFrom
Deployment strategy Recreate — a RWO local-path PVC cannot be mounted by an old and a new pod at once, so RollingUpdate deadlocks the rollout

The PVC is local-path, so the data physically lives on siriussec (nodeSelector: kubernetes.io/hostname: siriussec is unchanged) under /opt/local-path-provisioner/pvc-<uuid>_monitoring_kube-prometheus-stack-grafana/. That also means the data does not follow the pod to another node — if siriussec is rebuilt, the admin state is gone with the rest of the local PVs.

Provisioned dashboards still win

Dashboards and datasources are sidecar-loaded from ConfigMaps (grafana_dashboard / grafana_datasource labels, provider allowUiUpdates: false, foldersFromFilesStructure off). They are the source of truth: editing a provisioned dashboard in the UI is overwritten on the next sidecar reload/restart, and deleting the ConfigMap deletes the dashboard. That is intended — persistence exists for credential/session state, not to turn Grafana into a hand-edited dashboard store. A new dashboard created purely in the UI has no ConfigMap and does persist in the DB, but nothing backs it up and it will not exist after a node rebuild.

Where the admin password lives

Two owners, one rule: the Secret is only the seed.

  • monitoring/grafana-admin (admin-user, admin-password) is used through grafana.admin.existingSecret → GF_SECURITY_ADMIN_USER / GF_SECURITY_ADMIN_PASSWORD. Grafana creates the admin row from these values only when the DB has no admin user (first boot on an empty PVC).
  • After that the DB owns the password. An in-app change sticks across restarts, and the Secret is not updated by Grafana — the two drift the moment someone changes the password in the UI.

So: changing grafana-admin alone on a populated PVC does nothing. That is by design (no silent password resets on redeploy), and it is exactly why the recovery path below matters.

Drift breaks the sidecars, not just the login. The pod's sidecars (grafana-sc-dashboard, grafana-sc-datasources) carry REQ_USERNAME/REQ_PASSWORD from grafana-admin and POST /api/admin/provisioning/{dashboards,datasources}/reload as their "make Grafana read what I just wrote" step. Once the DB password has drifted from the Secret that POST returns 401, so a datasource ConfigMap is written into /etc/grafana/provisioning/datasources and never loaded: the datasource is simply absent from /api/datasources while the file sits in the pod. Observable 2026-11-27 with the Loki datasource (ConfigMap live, file in the volume, no datasource in Grafana for 40 minutes). The DB was then reset to the Secret's value, so the seed and the DB match again and the reload answers 200. Fix drift the same way before debugging a "missing" datasource.

Recovery: reset a lost admin password

The trap with a persistent DB is that re-applying the Secret no longer helps. Reset it from inside the pod against the live DB:

kubectl -n monitoring exec deploy/kube-prometheus-stack-grafana -c grafana -- \
  grafana cli --homepath /usr/share/grafana admin reset-admin-password '<new-password>'

That rewrites the password in grafana.db (and clears the admin's sessions), so it survives restarts. Then also update the Secret so the seed matches the DB and nobody gets a surprise if the PVC is ever lost:

# keep the value out of shell history / the terminal: read it from a 0600 file
kubectl -n monitoring patch secret grafana-admin --type merge \
  -p "{\"stringData\":{\"admin-password\":\"$(cat /root/gf-admin.pw)\"}}"

Full reset (last resort, destroys every UI-created dashboard, API key and alert rule): scale the Deployment to 0, rm -rf the PV sub-directory on siriussec (or delete the PVC), scale back to 1. Grafana re-seeds the admin user from grafana-admin on the empty DB.

Recreating the secret_key Secret

grafana-secret-key is out of band. If it is lost, Grafana will not start (the secretKeyRef is not optional). Recreate it with a fresh random value — this invalidates every existing session, which is the acceptable cost:

kubectl -n monitoring create secret generic grafana-secret-key \
  --from-literal=secret_key="$(head -c 48 /dev/urandom | base64 | tr -d '\n=/+' | head -c 63)"
kubectl -n monitoring rollout restart deploy/kube-prometheus-stack-grafana

(Or apply stringData from stdin so the value never touches a file. Never commit it.)

One-time transition note (2026-10-27)

Switching the existing Deployment to strategy: Recreate needed one manual step: the live Deployment still owned spec.strategy.rollingUpdate from the previous release, and server-side apply does not drop a field merely by omitting it, so Helm failed with

Deployment.apps "kube-prometheus-stack-grafana" is invalid:
spec.strategy.rollingUpdate: Forbidden: may not be specified when strategy `type` is 'Recreate'

Putting rollingUpdate: null in the values does not help — Helm drops null keys during values coalescing. The fix was a one-off kubectl -n monitoring patch deploy kube-prometheus-stack-grafana -p '{"spec":{"strategy":{"type":"Recreate","rollingUpdate":null}}}' followed by a HelmRelease reconcile. A fresh install never needs it.