diff --git a/docs/grafana-persistence.md b/docs/grafana-persistence.md new file mode 100644 index 0000000..02c683d --- /dev/null +++ b/docs/grafana-persistence.md @@ -0,0 +1,112 @@ +# 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-_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. + +## 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: + +```bash +kubectl -n monitoring exec deploy/kube-prometheus-stack-grafana -c grafana -- \ + grafana cli --homepath /usr/share/grafana admin reset-admin-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: + +```bash +# 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: + +```bash +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.