114 lines
4.7 KiB
Markdown
114 lines
4.7 KiB
Markdown
# Service images
|
|
|
|
The swarm daemons that run in the k8s cluster ship as OCI images built by nix.
|
|
`nix/images/default.nix` holds the one function that builds them,
|
|
`mkServiceImage`, and the set of daemons that use it.
|
|
|
|
## What an image holds
|
|
|
|
One image runs one daemon. Its contents are:
|
|
|
|
- the daemon's per-bin package from `nix/packages/default.nix`,
|
|
- the CA roots from `pkgs.cacert`, with `SSL_CERT_FILE` pointing at them,
|
|
- the binary's own closure, which is glibc and libgcc.
|
|
|
|
The image has no shell and no init. The kubelet restarts a crashed container, so a
|
|
service manager inside it would only disagree with the kubelet. A missing
|
|
shell also leaves nothing to `exec` into. The entrypoint is `/bin/<bin>`, and
|
|
the process runs as the `serviceUser` uid and gid that `nix/images/default.nix`
|
|
defines. A pod's `securityContext.runAsUser` and `runAsGroup` read that binding
|
|
too, so the number lives in one place.
|
|
|
|
`mkServiceImage` takes `name`, `package`, and optionally `bin` (the binary
|
|
name, default `name`), `extraContents` and `env` (an attribute set added to
|
|
the container environment).
|
|
|
|
## The image set
|
|
|
|
Each of these is a flake package named `image-<daemon>`, and the image name is
|
|
`registry.hyperhive.invalid/<daemon>` ([the registry host](#the-registry-host)):
|
|
|
|
- `swarm-controller`
|
|
- `swarm-nats-auth`
|
|
- `swarm-logs-auth`
|
|
- `swarm-metrics-auth`
|
|
- `swarm-notify-router`
|
|
- `swarm-ci-metrics`
|
|
|
|
`nix flake check` evaluates all six and builds none of them, so the workspace
|
|
compile stays out of CI. `swarm-claude-login`, `swarm-review-classifier` and
|
|
`swarm-stt` aren't in the set: [the manifest renderer](manifests.md#pods) builds
|
|
their images, with the paths their units' environments name (claude-code and
|
|
opencode, ffmpeg, whisper-cli and the model). It builds three more for the
|
|
queue's pod ([runtime](runtime.md#the-queue)): the NATS server, which also
|
|
holds `wget`, the one tool of busybox its readiness probe needs; the
|
|
Prometheus exporter; and an image of the queue's init and renewal scripts.
|
|
|
|
## Tags
|
|
|
|
An image has no fixed tag. The tag is the content hash, so a changed binary is
|
|
a changed reference. A manifest reads the full reference from the package's
|
|
`imageName` and `imageTag` attributes:
|
|
|
|
```sh
|
|
nix eval --raw .#image-swarm-logs-auth.imageName
|
|
nix eval --raw .#image-swarm-logs-auth.imageTag
|
|
```
|
|
|
|
## The registry host
|
|
|
|
Every image name starts with `registry.hyperhive.invalid`, which
|
|
`mkServiceImage` takes from `registryHost` in `nix/images/default.nix`.
|
|
RFC 6761 reserves `.invalid`, and it never resolves, so the name has no
|
|
registry behind it. Containerd expands a name with no host, such as
|
|
`hyperhive/<daemon>`, to `docker.io/hyperhive/<daemon>`, and a node that holds
|
|
no copy would ask Docker Hub for it. With this host the pull fails instead.
|
|
|
|
## Where a node gets an image
|
|
|
|
The main node imports the tarballs into containerd before k3s starts, and
|
|
k3s's embedded registry mirror (`--embedded-registry`, on the main node) shares
|
|
what a node holds with the others. `/etc/rancher/k3s/registries.yaml` on every
|
|
node enables the mirror for the one host:
|
|
|
|
```yaml
|
|
mirrors:
|
|
registry.hyperhive.invalid:
|
|
```
|
|
|
|
k3s documents the flag, the file and the rule that applies here: a node shares
|
|
images it imported from a tarball when their tag names a registry the file
|
|
enables for mirroring, and the upstream registry doesn't have to exist
|
|
(<https://docs.k3s.io/installation/registry-mirror>). The file names only this
|
|
host and not `"*"`, so the node's pulls of public images stay direct.
|
|
|
|
A pod that lands on a node without its image, a node that joined, pulls the
|
|
image from a peer that holds it (`imagePullPolicy: IfNotPresent`). If no peer
|
|
holds it, for instance because the main node is down and the node never had
|
|
the image, the pull fails with the host's name in the error, and the pod
|
|
retries once a peer has the image. Images a node holds stay: k3s doesn't
|
|
prune imported tarball images. The mirror doesn't share a `latest` tag, and
|
|
the tags here are content hashes.
|
|
|
|
## Loading an image
|
|
|
|
`nix build` produces a `.tar.gz` that `podman load`, `skopeo` and the k3s image
|
|
directory all read. Use `podman`, not docker:
|
|
|
|
```sh
|
|
podman load -i "$(nix build --no-link --print-out-paths .#image-swarm-logs-auth)"
|
|
```
|
|
|
|
For a containerd node, stream the file into the node's image store:
|
|
|
|
```sh
|
|
k3s ctr images import - < "$(nix build --no-link --print-out-paths .#image-swarm-logs-auth)"
|
|
```
|
|
|
|
## The `image-fn-smoke` check
|
|
|
|
`checks.<system>.image-fn-smoke` builds `mkServiceImage` around `hello` and
|
|
reads the tarball back. It asserts the entrypoint, the user, the CA env var, a
|
|
layer listing without `bash` or `systemd` and a size under 50 MB. The listing
|
|
must also contain `hello` and `glibc`, so an empty listing can't pass the
|
|
shell arm. The check runs without compiling any hyperhive binary.
|