run grafana as a pod in the cluster
The Deployment swarm-grafana replaces the swarm-grafana container: one replica on a static local volume that is the container's data directory, an init container that fetches the OIDC client secret and creates the administrator password, the datasources' bearer and the encryption key in the secret store, and a sidecar that keeps the controller's Viewer token. The datasources reach the stores through the gateway with that bearer, and Grafana's /metrics asks for the stores' kind of credential, which the vhost sends. The five host units, the journal forwarder, its client glue and the unix-socket directory go with the container.
This commit is contained in:
parent
26ef09b0d7
commit
9f62e71dae
3 changed files with 1938 additions and 1953 deletions
|
|
@ -32,6 +32,7 @@ A service gets manifests on the host whose configuration deploys it:
|
|||
| `swarm-logs-auth` | Deployment | ServiceAccount, two Services, NetworkPolicy |
|
||||
| `swarm-victoriametrics` | Deployment | Service, NetworkPolicy, PersistentVolume and Claim |
|
||||
| `swarm-metrics-auth` | Deployment | ServiceAccount, two Services, NetworkPolicy |
|
||||
| `swarm-grafana` | Deployment | ServiceAccount, Service, NetworkPolicy, PersistentVolume and Claim |
|
||||
|
||||
`namespace.yaml` holds the Namespace ([Pod Security](#pod-security)), and
|
||||
`default-deny-ingress/networkpolicy.yaml` its default-deny ingress policy
|
||||
|
|
@ -150,15 +151,17 @@ credential directory.
|
|||
in the container, with a 5-second timeout, and a 503 fails it. The pod
|
||||
declares no port for the listener. `swarm-controller` is one of
|
||||
them, with `SWARM_CONTROLLER_HEALTH_ADDR`. A third-party daemon with no health variable,
|
||||
`victoria-logs` and `victoria-metrics`, names a `probe` path instead (`/health`), which both probes
|
||||
request on its listener. A `startupFailureThreshold` in it (`victoria-metrics`, 60) adds a
|
||||
`victoria-logs`, `victoria-metrics` and `grafana`, names a `probe` path instead (`/health`; `/api/health` for `grafana`), which both probes
|
||||
request on its listener. A `startupFailureThreshold` in it (`victoria-metrics`, 60, and `grafana`, 30) adds a
|
||||
startup probe on the same path, tried every ten seconds, before the other two begin.
|
||||
The health listener of our own daemons is unreachable from outside the pod: the
|
||||
node, other pods and the bridge get a refusal, so it needs no credential.
|
||||
The one health route outside the pod is the `/health` of `victoria-logs` and
|
||||
`victoria-metrics` on their main port. Upstream answers `/health`, `/ping`,
|
||||
`/-/healthy` and `/-/ready` before it checks `-httpAuth`, and the gateway needs
|
||||
the main port on the pod's IP, so no bind or probe can close it. It's
|
||||
The health routes outside the pod are the `/health` of `victoria-logs` and
|
||||
`victoria-metrics` on their main port, and the `/api/health` and `/healthz` of
|
||||
Grafana on its own. Upstream answers `/health`, `/ping`, `/-/healthy` and
|
||||
`/-/ready` before it checks `-httpAuth`, Grafana answers its two before its
|
||||
login, and the gateway needs the main port on the pod's IP, so no bind or
|
||||
probe can close them. It's
|
||||
reachable from the node only, through the default-deny ingress policy, and it
|
||||
returns a status and no data.
|
||||
- **Listener and init container:** a service with a TCP `listener`
|
||||
|
|
@ -167,7 +170,7 @@ credential directory.
|
|||
gets an init container and a renewal container that issue its leaf into an
|
||||
`emptyDir` ([runtime](runtime.md#the-controller)). The image of those
|
||||
containers is part of `system.build.k8sImages`. A listener with no `tlsDir`
|
||||
(`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`) serves plain HTTP: it gets the
|
||||
(`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`, `swarm-grafana`) serves plain HTTP: it gets the
|
||||
Service and the policy and no leaf, init container or renewal container.
|
||||
- **More than the daemon:** a service whose pod holds more than its daemon
|
||||
(the queue, whose server and exporter run beside the responder) names a
|
||||
|
|
@ -179,9 +182,11 @@ credential directory.
|
|||
holds two store roles has both bound to it
|
||||
([runtime](runtime.md#the-queue)). The responders' pods of the log and metrics
|
||||
stores hold a second container the same way
|
||||
([runtime](runtime.md#the-log-store), [runtime](runtime.md#the-metrics-store)).
|
||||
([runtime](runtime.md#the-log-store), [runtime](runtime.md#the-metrics-store)),
|
||||
and Grafana's pod holds an init container and a sidecar
|
||||
([runtime](runtime.md#the-grafana-pod)).
|
||||
- **A unit of the module's own making:** a service that never ran under systemd on the
|
||||
host (`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`) gives the renderer the
|
||||
host (`swarm-victorialogs`, `swarm-logs-auth`, `swarm-victoriametrics`, `swarm-metrics-auth`, `swarm-grafana`) gives the renderer the
|
||||
`serviceConfig`, `environment` and `path` of one as a plain attribute set, with
|
||||
the daemon's flags as a list in `ExecStart`. The renderer reads nothing else of
|
||||
a unit. `fsGroupChangePolicy` in the entry sets the pod's field of that name.
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ says so with a label. The module sets the labels
|
|||
(`services.hyperhive.deploy.k8s.nodeLabels`, passed to `services.k3s.nodeLabel`):
|
||||
|
||||
- `hyperhive.darkest.space/state-<service>=true`, one per service whose data
|
||||
the cluster keeps. Today that's `nats`, `ci-metrics`, `victorialogs` and `victoriametrics`, each when its
|
||||
the cluster keeps. Today that's `nats`, `ci-metrics`, `victorialogs`, `victoriametrics` and `grafana`, each when its
|
||||
`deploy.<service>.enable` is on. The
|
||||
service's static local volume has a `nodeAffinity` on the label, and its pod a
|
||||
`nodeSelector` on it, so a node without the label can't run the pod and shows
|
||||
|
|
|
|||
|
|
@ -124,6 +124,10 @@ starts on an empty cache on every start of the pod; an upgrade from the
|
|||
container doesn't carry the old cache over. The whisper model isn't state: the
|
||||
pod's image holds it.
|
||||
|
||||
`swarm-grafana` keeps its database in the claim
|
||||
`swarm-grafana-store`, a static local volume on the directory its container kept
|
||||
([the Grafana pod](#the-grafana-pod)).
|
||||
|
||||
`swarm-ci-metrics` keeps a cursor, `cursor.json`, which holds the run numbers
|
||||
already pushed. It sits in the PersistentVolumeClaim `swarm-ci-metrics-store`, mounted at
|
||||
`/var/lib/private/swarm-ci-metrics`, which binds a static local volume on that
|
||||
|
|
@ -525,6 +529,72 @@ pushes to the store and Grafana that reads it.
|
|||
`agentAuthVerdictTtl` and the store identity files). The host collector reads
|
||||
the pods' output from the node like any pod's.
|
||||
|
||||
## The Grafana pod
|
||||
|
||||
`swarm-grafana` is the swarm's Grafana: one Deployment with one replica and the
|
||||
`Recreate` strategy. The vhost `grafana.<swarm domain>` stays on the host and
|
||||
dials the pod's ClusterIP Service by name, as the stores' vhosts do, so
|
||||
`gateway.dns.enable` has to be on.
|
||||
|
||||
- **The pod.** The module defines Grafana's `ExecStart`, and the image holds the
|
||||
nixpkgs Grafana with the plugins `deploy.grafana.plugins` names, its
|
||||
configuration file and its provisioning directory (datasources, dashboards and
|
||||
alert rules), all read-only. A start script applies the stored administrator
|
||||
password to the database with `grafana cli admin reset-admin-password`, because
|
||||
the `admin_password` setting gives its password to a new database only,
|
||||
and then runs `grafana server`. Grafana serves plain HTTP on port 3000,
|
||||
pushes no telemetry and logs in to nothing itself: its output is the pod's log,
|
||||
which the host collector reads like any pod's ([logs](telemetry.md#logs)). The
|
||||
startup probe requests `/api/health` every ten seconds for up to five minutes,
|
||||
which covers a database migration after an upgrade.
|
||||
- **The init container.** `fetch-secrets` logs in to the secret store with the
|
||||
pod's ServiceAccount token under the jwt role `swarm-grafana-oidc`, and writes
|
||||
four files, mode 0400, to the pod's in-memory `runtime` volume: the OIDC client
|
||||
secret, the administrator password, the bearer of the datasources and the
|
||||
datasource encryption key. It reads the client secret from
|
||||
`swarm/services/<client id>/oidc/client`, which authelia's mint stores, and
|
||||
exits non-zero when the store holds none, so the pod waits in `Init` and retries
|
||||
until the mint has written it. The other three it creates when the store holds
|
||||
none (a lost race reads the winner's value) and never replaces.
|
||||
- **Rotation.** Grafana reads the four files once, when it starts. A replaced
|
||||
OIDC client secret, administrator password or bearer takes effect when the pod
|
||||
restarts: `kubectl -n hyperhive rollout restart deployment/swarm-grafana`. No
|
||||
`swarmctl` verb restarts it. The bearer is also read by the responders of both
|
||||
stores, which pick up a replaced value within a minute.
|
||||
- **The sidecar.** `controller-token` keeps `swarm-controller`'s Viewer service
|
||||
account in Grafana and its token at
|
||||
`swarm/controller/swarm-controller/grafana/token`. Every five minutes it calls
|
||||
Grafana's API with the token it stored, and creates the account and a new
|
||||
token when Grafana refuses it, as Grafana does after its database is
|
||||
recreated. It reaches Grafana on the pod's loopback with the administrator
|
||||
password.
|
||||
- **The datasources.** Grafana dials `metrics.<swarm domain>` and
|
||||
`logs.<swarm domain>` by name through the gateway and sends its bearer, which
|
||||
the responders of those stores check. The pod holds no store's listener
|
||||
credential: the only credential Secret it mounts is its own,
|
||||
`swarm-grafana-credentials`. The cluster's DNS answers those names with the
|
||||
bridge address, and the pod's policy allows port 443 of the host.
|
||||
- **Who may use it.** The UI and the API need an OIDC session or the
|
||||
administrator's basic auth, and the login form is off. `/api/health` is open
|
||||
by Grafana's design. `/metrics` takes a credential of its own, set with
|
||||
`[metrics] basic_auth_username` and `basic_auth_password` (the user
|
||||
`swarm-store`, the password from the Secret `swarm-grafana-credentials`, which the unit of
|
||||
[the stores' listener credential](#the-stores-listener-credential) creates at
|
||||
`swarm/services/grafana/store-auth`). The vhost sends it after
|
||||
authelia has checked the caller, so a request that reaches the pod without it
|
||||
gets 401.
|
||||
- **State.** Grafana's data directory on the host is
|
||||
`/var/lib/nixos-containers/swarm-grafana/var/lib/grafana`: the claim
|
||||
`swarm-grafana-store` binds a static local volume on it, so the database the
|
||||
container kept is the pod's, with its dashboards, annotations and users. The
|
||||
node label is `hyperhive.darkest.space/state-grafana`
|
||||
([node labels](node.md#node-labels)), and the pod's `fsGroup` is `65532` with
|
||||
the `OnRootMismatch` policy, as for the stores.
|
||||
- **Who reaches what.** The namespace's `default-deny-ingress` and the pod's own
|
||||
policy admit the node alone, which is the host's nginx and the kubelet's
|
||||
probes. The pod reaches cluster DNS, the secret store's pod listener and port
|
||||
443 of the host.
|
||||
|
||||
## The stores' listener credential
|
||||
|
||||
Each store listens on its pod's address behind a ClusterIP Service, and the
|
||||
|
|
@ -544,6 +614,10 @@ every route but `/health` and the few built-in ones that answer a fixed text,
|
|||
the import, delete and admin routes of the metrics store included. Root can
|
||||
read the credentials, so treat the host as trusted for what root can do.
|
||||
|
||||
Grafana's `/metrics` route has a credential of the same kind, created by the same unit at
|
||||
`swarm/services/grafana/store-auth` and sent by the vhost from
|
||||
`/var/lib/swarm-store-auth/grafana.conf`: [the Grafana pod](#the-grafana-pod).
|
||||
|
||||
## Resources and sandbox
|
||||
|
||||
The pod takes the unit's `MemoryMax` and `CPUQuota` as its container's limits
|
||||
|
|
|
|||
Loading…
Reference in a new issue