Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/docs/4-implementation/k8s/manifests.md
atlas 9f62e71dae 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.
2026-11-28 10:01:39 +01:00

260 lines
17 KiB
Markdown

# Service manifests
`services.hyperhive.k8s` renders Kubernetes manifests for the swarm services
that run in the cluster. `nix/host-modules/k8s/render.nix` builds them from
the systemd unit each service runs today. A host with
`services.hyperhive.k8s.enable` gets them as `system.build.k8sManifests`, a
directory with one YAML file per object, and the images their pods run as
`system.build.k8sImages`, which [the cluster node](node.md) loads. Rendering
changes no unit. [The runtime module](runtime.md) applies a service's manifests
to the cluster, and the service runs there and nowhere else.
## Options
- `services.hyperhive.k8s.enable` (default `false`): render the manifests.
- `services.hyperhive.k8s.namespace` (default `hyperhive`): the namespace of
every rendered object.
## What gets rendered
A service gets manifests on the host whose configuration deploys it:
| service | workload | other objects |
| ------------------------- | ---------- | ------------------------------------------------------------------------------------------ |
| `swarm-controller` | Deployment | ServiceAccount, ConfigMap `swarm-controller-etc`; Service, NetworkPolicy with the listener |
| `swarm-nats` | Deployment | ServiceAccount, two Services, NetworkPolicy, PersistentVolume and Claim |
| `swarm-notify-router` | Deployment | ServiceAccount |
| `swarm-stt` | Deployment | ServiceAccount, NetworkPolicy `swarm-stt` |
| `swarm-review-classifier` | Deployment | ServiceAccount |
| `swarm-claude-login` | Deployment | ServiceAccount |
| `swarm-ci-metrics` | CronJob | ServiceAccount, PersistentVolume and Claim |
| `swarm-victorialogs` | Deployment | Service, NetworkPolicy, PersistentVolume and Claim |
| `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
([Network](#pods)).
## One source: the unit
The renderer reads the service's evaluated unit, not a copy of its settings:
- **Environment:** the unit's `environment`, including what other modules
merge into it, minus `PATH`. A systemd `%d`, here and in `ExecStart`'s arguments, becomes the credential
directory it stands for. On a host with a hive collector, the OTLP endpoint
variables of a hive-tier service point at it ([Pod logs and metrics](telemetry.md))
instead; a swarm-tier producer, `swarm-ci-metrics`, keeps its own. The
renderer fails if the unit's `path` holds anything beyond the NixOS default
tools, because the image doesn't carry them. The other change is the store
login, [below](#the-store-login).
- **Files:** the pod mounts each file the unit names at the same path,
so the environment carries over unchanged:
- credentials at `/run/credentials/<service>.service/<id>`, from the Secret
`<service>-credentials`, one key per `LoadCredential` id, except the ones
that are public CA files, which come from the ConfigMap
`hyperhive-ca-trust` [below](#secrets);
- trust bundles (any `…/trust-bundle.pem` the environment names), from the
ConfigMap `hyperhive-ca-trust`, key `trust-bundle.pem`;
- `RuntimeDirectory` as an `emptyDir` in memory, as the `/run` it stands for
is, so nothing a service keeps there reaches the node's disk, and
`StateDirectory` on the service's PersistentVolumeClaim, with
`RUNTIME_DIRECTORY` and `STATE_DIRECTORY` set the way systemd sets them;
- `/etc` files the unit reads, from the service's own ConfigMap.
The renderer fails evaluation if an environment value names an absolute
path the pod doesn't mount, or a credential the unit doesn't load. A new
path in a unit then shows up as an error here, not as a pod that can't
find a file.
- **Command:** `ExecStart` must be the service's own binary. Its arguments
become the container's `args`.
- **Schedule:** the CronJob runs every `OnUnitInactiveSec` minutes of the
unit's timer, never overlapping, with `TimeoutStartSec` as its deadline.
A change to a unit's environment therefore changes its manifest in the same
commit.
## Secrets
The YAML lands in the world-readable nix store, so it holds no credential.
It names the Secret `<service>-credentials` and the credential ids as keys.
It never names the host path the unit loads a credential from, and never its
contents. Nothing here creates the Secret: the unit `swarm-bao-store-auth` publishes
the stores' ([the log store](runtime.md#the-log-store)).
A credential that holds a public CA file isn't a secret. `swarm-notify-router`'s
`notify-router-bao-server-ca` and `swarm-ci-metrics`'s `bao-ca.pem` come from
the ConfigMap `hyperhive-ca-trust`, key `bao-server-ca.pem`, and the trust
bundles from the key `trust-bundle.pem`.
The manifests only name the ConfigMap, because the hive CA exists only at
runtime. [The runtime module](runtime.md#what-the-cluster-runs) publishes it from the
host's files. A service with both kinds of credential gets one
projected volume that merges the Secret's keys and the ConfigMap's into the
credential directory.
## Pods
- **Image:** `mkServiceImage` from [Service images](images.md), built from the
same `deploy.<service>.package` the unit runs. Store paths the environment
names (the controller's branding assets, for example) go into the image
too. Where the environment names none, the image is the flake's
`image-<service>`. The manifest carries the image name and tag but doesn't
depend on the image: building the manifests builds no daemon.
`imagePullPolicy` is `IfNotPresent`: the node imports the image tarball, and a
node that joined pulls it from a peer ([Service images](images.md#where-a-node-gets-an-image)).
- **Security:** the `serviceUser` uid and gid from [Service images](images.md),
with `fsGroup` its gid so the pod reads its credentials (mode `0440`),
read-only root filesystem with an `emptyDir` on `/tmp`, no privilege
escalation, every capability dropped, the runtime's default seccomp profile,
and no mounted API token. A service that logs in to the store gets a token
of its own, [below](#the-store-login).
`swarm-stt` also runs in a user namespace of its own (`hostUsers: false`), as
the unit's `PrivateUsers` does: its root and every id it holds are
unprivileged ids on the node. A volume keeps the in-pod ownership it has
without one, so `fsGroup` and the credential files' mode work as before.
A service asking for it needs a kernel of 6.3 or later (an assertion).
- **Resources:** the unit's `MemoryMax` becomes the container's memory limit
(systemd's `K`, `M`, `G` and `T` are Kubernetes' `Ki`, `Mi`, `Gi` and `Ti`; a
byte count passes unchanged; `infinity` sets none) and its `CPUQuota`, a
percent of one core, becomes the cpu limit in millicores (`400%` is
`4000m`). Both are caps, not reservations, so the renderer sets the requests
too: Kubernetes would otherwise set each to its limit and reserve the whole
cap on the node. The memory request is the unit's `MemoryMin`, else its
`MemoryLow`, else `0`, and the cpu request is `0`, so the pod is Burstable. A
value in another form fails evaluation. `TasksMax` has no pod field, so the
renderer drops it. The unit's sandbox settings (`MemoryDenyWriteExecute`,
`SystemCallFilter` and the rest) stay on the host: the pod has the security
shape above and no other.
- **Network:** a service whose entry has `egress` (`swarm-stt`) gets a
NetworkPolicy named after it, selecting its pods for ingress and egress. It
allows no ingress; its egress is cluster DNS (the CoreDNS pod, port 53) and,
on the hive bridge's address, one TCP port per host service the entry names:
the store's pod listener (`store`), the gateway's 443 for the identity
provider (`identity`), the queue's port (`queue`) and the hive collector's
(`collector`). The policy allows its pod on the queue's port too. The node trusts
the pod network's interfaces, so without the policy a pod reaches every port of
the host. A service with neither `egress` nor a listener has no policy of
its own and relies on the namespace's `default-deny-ingress`, which selects
every pod and admits nobody. The cluster's policy engine takes the union of
the policies that select a pod and always admits the node's own addresses, so
the node reaches a pod, and so does whatever its own policy names. A service with
a listener and `egress` (`swarm-logs-auth`, `swarm-metrics-auth`) has the one policy: it admits the
node to the listener and nothing else, and allows the egress above.
- **Probes:** a service with an opt-in health listener gets its variable set to
`127.0.0.1:8080`, with liveness on `/healthz` and readiness on `/readyz`
(see [Readiness checks](../swarm/readiness.md)). The kubelet's `httpGet`
probes dial the pod's IP, so these are `exec` probes: each runs `wget` from
the image (`wgetOnly`, in `nix/images/default.nix`) against the loopback URL
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`, `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 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`
(`swarm-controller`, with `swarm.controller.tcp.enable`) publishes its port
with a ClusterIP Service, admits only the node to it with a NetworkPolicy, and
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`, `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
function in its entry that adds containers, init containers, volumes, host
aliases, pod settings, objects and images. A service named differently from
its daemon (`swarm-nats`, whose daemon is `swarm-nats-auth`) names the
daemon's binary, and the credential directory its unit loads from follows
the daemon's name. The pod runs under one ServiceAccount, so a queue that
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)),
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`, `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.
- **Config checksum:** a Deployment whose service has a ConfigMap (`etcFiles`)
carries `checksum/etc` on its pod template, the hash of that ConfigMap's
content, so a changed file rolls the pod.
- **Shutdown:** `terminationGracePeriodSeconds` is 15, the 10-second drain a
daemon allows itself after SIGTERM (`DRAIN` in
`swarm-secret-client/src/shutdown.rs`) plus headroom.
- **Rollout:** each Deployment has one replica and the `Recreate` strategy,
because each service runs once per swarm.
## Pod Security
`namespace.yaml` labels the namespace with the `restricted` level of Pod
Security for `enforce`, `warn` and `audit`. The API server refuses a pod that
breaks the level: a host port, a host namespace or path, root, a kept
capability or privilege escalation, or no seccomp profile. A Deployment that
would make such a pod gets a warning on apply, and its pods fail to start, so a
drifted workload shows as a Deployment with no ready replica. The security
settings of [Pods](#pods) are what the level asks for, and no pod needs a
`hostPort`: the controller's port and the queue's exporter are ClusterIP
Services, and the queue's client port is a `LoadBalancer` Service that k3s's
service load balancer binds on the node from `kube-system`, which the label
doesn't cover. The level follows the cluster's Kubernetes version, so an
upgrade can tighten it. `module-eval-k8s` reads every rendered pod against the
level's rules, and the VM test `k8s-controller` shows the API server
refusing a pod with a host port, a kept capability or privilege.
## The store login
A service that has a role at the store's `jwt-k8s` auth mount
(`services.hyperhive.deploy.bao.k8sPrincipals`) logs in with its pod's
ServiceAccount token. A unit logs in with the client certificate in its
environment, so the pod differs from the unit in these places:
- **ServiceAccount:** one named like the service, in the rendered namespace,
with `automountServiceAccountToken: false`, and the pod runs under it.
- **Token:** a projected volume `bao-token` with one `serviceAccountToken`
source for the audience `bao`, expiring after 600 seconds (the shortest the
API server grants) and mounted read-only at `/var/run/secrets/hyperhive/bao`.
The kubelet rewrites the file before it expires, and the client reads it
afresh at each login. The store's role for the service accepts this
account and this audience only.
- **Environment:** the pod sets neither `BAO_CLIENT_CERT` nor
`BAO_CLIENT_KEY`, and the credentials they named aren't in the pod's
Secret. `BAO_AUTH=jwt`, `BAO_JWT_FILE`, `BAO_JWT_ROLE` (the service's name,
or the role its entry names) and `BAO_JWT_MOUNT` (`jwt-k8s`) take their
place, and `BAO_ADDR` names the store's own name at the port of the listener
that asks for no client certificate (`deploy.bao.podListenerPort`).
`BAO_CACERT` stays.
[The cluster node](node.md#logging-in-to-the-secret-store) describes the
store's side.
## Not covered yet
- The pods reach the swarm's host-run services (store, identity provider, and
the queue where it runs on the host) by the same names as the units do. The
node's CoreDNS answers those names with the hive bridge's address.
- `swarm-controller`'s socket lives in an `emptyDir`, so only the pod can
reach it. The gateway dials the TCP listener instead.
## Validation
The flake check `k8s-manifests-all-local` renders the manifests for the
single-host swarm fixture and runs `kubeconform -strict` over them, offline,
against JSON schemas for Kubernetes 1.35 pinned by hash. A deliberately
malformed Deployment in the same check must fail, so a validator that loads no
schema can't pass. `module-eval-k8s` checks that each workload carries its
unit's environment and that no credential source path reaches the YAML.