Watch
0
0
Fork
You've already forked hyperhive
0

let a second machine join the main node's k3s and run pods from images only the main node holds

This commit is contained in:
atlas 2026-11-25 22:36:35 +01:00 • committed by maia
commit 23865cb223
4 changed files with 1106 additions and 96 deletions

View file

@ -26,7 +26,7 @@ the container environment).
## The image set
Each of these is a flake package named `image-<daemon>`, and the image name is
`hyperhive/<daemon>`:
`registry.hyperhive.invalid/<daemon>` ([the registry host](#the-registry-host)):
- `swarm-controller`
- `swarm-nats-auth`
@ -55,6 +55,41 @@ 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

View file

@ -94,7 +94,8 @@ credential directory.
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 `Never`, because the node imports the image tarball.
`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

View file

@ -1,10 +1,12 @@
# The cluster node
`services.hyperhive.deploy.k8s.enable` (default `false`) makes the host run a
single-node k3s server for the swarm services that run in the cluster. It's
the Kubernetes counterpart of `services.hyperhive.deploy.singleHostSwarm`: set
it on the host that runs the swarm's services, and the cluster comes up with the
service images loaded and no service's objects applied.
k3s server for the swarm services that run in the cluster. It's the Kubernetes
counterpart of `services.hyperhive.deploy.singleHostSwarm`: set it on the host
that runs the swarm's services, and the cluster comes up with the service
images loaded and no service's objects applied. Other machines can
[join it](#joining-nodes) and run the services' pods; hyperhive runs no
cluster of its own beyond that one main node, and it replicates no service.
## What the host gets
@ -14,8 +16,9 @@ service images loaded and no service's objects applied.
pods have cluster DNS and PersistentVolumeClaims.
- **Images:** the node holds k3s's own images and the image of each service
that [renders manifests](manifests.md) on this host, in containerd before k3s
starts. No registry exists, and the pods name their images with
`imagePullPolicy: Never`.
starts. No registry service exists. A pod names its image with
`imagePullPolicy: IfNotPresent`, and a node that joined and lacks the image
gets it from a peer ([Service images](images.md#where-a-node-gets-an-image)).
- **Manifests:** `services.hyperhive.k8s.enable` is on, so the host builds
`system.build.k8sManifests` and `system.build.k8sImages`. Setting it to
`false` explicitly fails the evaluation.
@ -27,7 +30,9 @@ service images loaded and no service's objects applied.
same addresses. k3s's own default pod range, `10.42.0.0/16`, contains the
bridge's default subnet.
- **Firewall:** the pod network's interfaces, `cni0` and `flannel.1`, are
trusted, so the node reaches its pods.
trusted, so the node reaches its pods. The node-to-node ports are open to
the addresses in `deploy.k8s.nodeAddresses` only, which is empty by default
([Joining nodes](#joining-nodes)).
- **NetworkPolicy:** k3s's own controller (kube-router, iptables) enforces
NetworkPolicy objects; the node doesn't turn it off. A pod no policy selects
reaches whatever it can route to. [The manifests](manifests.md#pods) say which
@ -85,6 +90,86 @@ Moving a service's data to another node is manual: copy the directory, put the
label on the new node, take it off the old one. Moving it with a command
belongs to a later version.
## Joining nodes
The main node is the k3s server. A machine that joins is a k3s agent: it runs
pods, and none of the server's manifests, units or labels. It doesn't need the
swarm's services on it. The pods the main node renders run on it unless they
keep data on a disk ([Node labels](#node-labels)), and a pod that has moved
there pulls its image from a peer.
On the main node, set the addresses:
- `deploy.k8s.address`: the address the other nodes reach this one on. Set,
it's the node's IP (`--node-ip`) and the API certificate lists it
(`--tls-san`). Unset, k3s picks the default route's address, as it did before
the option existed.
- `deploy.k8s.nodeAddresses`: the addresses of the nodes that may join. The
firewall admits these ports from them and from no other source: TCP 6443
(the API, and the image mirror), TCP 10250 (the kubelet), UDP 8472 (flannel's
VXLAN tunnel) and TCP 5001 (the mirror's peers). It writes iptables rules
and refuses a host with `networking.nftables.enable`. With the list empty,
no port is open.
On a NixOS host with a hive, set `deploy.k8s.join.server` to the main node's
address (and `deploy.k8s.address` to this host's own, listed in the main node's
`nodeAddresses`). The host runs `k3s agent` against
`https://<server>:6443` with the token the store holds, and the same image
mirror. It needs the store identity of its hive
(`deploy.hive-controller.tls.baoHiveIdentityCertFile` and its key), as the
hive's other units do, and refuses to evaluate without it. The module refuses a
host that sets both `join` and `deploy.k8s.enable`.
### The join token
k3s keeps the token agents join with in
`/var/lib/rancher/k3s/server/agent-token`. The main node's unit
`k3s-join-token-publish` writes it to `secret/swarm/services/k8s/agent-token`
in the secret store, under the identity of the hive that runs the main node, at
each start of k3s, and writes it only when the stored value differs. A joining host's unit
`k3s-join-token` reads that leaf under its own hive's identity and writes
`/run/k3s-join/token` (tmpfs, root, `0400`) before k3s starts, so no step is
manual. Every hive's identity reads the leaf and only the main node's hive
writes it. The token is a join credential, not a cluster admin one, but it lets
a machine that holds it become a node, so the nodes' firewall rules are the
second check.
If the store is down or still locked when the host starts, the unit retries every 30
seconds for a day, and k3s restarts every 5 seconds until the file exists.
### Joining a machine that isn't a hyperhive host
Nothing in the module reaches a machine it doesn't configure, so an admin does
this by hand:
1. List the machine's address in the main node's `deploy.k8s.nodeAddresses`
and activate.
2. Read the token from the store, with an identity that may read
`secret/swarm/services/k8s/agent-token` (the operator's, or the root
token), and put it on the machine in a file only root reads:
```bash
bao kv get -field=value secret/swarm/services/k8s/agent-token
```
3. Write `/etc/rancher/k3s/registries.yaml` on the machine. It's the file the
main node has, so the machine gets images from its peers:
```yaml
mirrors:
registry.hyperhive.invalid:
```
4. Start k3s as an agent:
```bash
k3s agent --server https://<main node>:6443 --token-file <token file> --node-ip <the machine's address>
```
The machine's firewall has to admit the same four ports from the main node and
the other nodes. The machine has no network route to the images' host, so a
pod's image comes from a peer or fails ([Service images](images.md#where-a-node-gets-an-image)).
## Reaching host-run services
A pod reaches a service that runs on the host by the service's swarm name. The
@ -213,4 +298,8 @@ ConfigMap. The VM test `k8s-node` boots a node through the module and runs a
pod against it. The VM test `k8s-bao-login` runs the node and the secret store
together: a pod under a service's ServiceAccount logs in and reads a value, a
pod under another account gets no token, and a client on the hive bridge can't
connect to the pods' port.
connect to the pods' port. The VM test `k8s-join` boots a second machine that
joins the main node through `deploy.k8s.join` with the token from the main
node's store, runs a pod there from an image only the main node imported, and
checks that the queue stays on the main node and that an image no node holds
fails to pull instead of reaching a public registry.

View file

@ -41,6 +41,7 @@ the rest of the cell says how.
| `swarm/agents/<agent>/proxy-credential` | `swarm-controller`: 32 random bytes, at start and every 5 minutes for every agent that holds a store identity | the hive (`hive-c0re`) and the agent, which fetches it | ❌ never replaced: written only while the path holds nothing | ✅ `hive-c0re` re-reads it every 30 seconds |
| `ssh-agents/sign/swarm-agent` (a signing call, nothing is stored) | the store's SSH CA mount (`ssh-agents`), when the agent's `hive-agent-ssh-cert` unit asks it to sign, which the operator's per-agent `ssh` switch allows the public key the unit generated in its own `/run`; the CA key is generated inside the store by `swarm-bao-agent-ssh-policy` | the agent, which keeps the key and the signed certificate in `/run` only; a target machine trusts the CA's public key by value | ✅ the certificate lasts 60 minutes; the CA is never replaced, and a target that holds a different CA key refuses every certificate | ✅ the agent signs again every 20 minutes |
| `swarm/hives/<hive>/squid-selfcheck` | `swarm-controller`, in the same pass as the agents' credentials | the hive: `hive-c0re` and `squid-selfcheck-credential` (root) | ❌ never replaced: written only while the path holds nothing | ✅ `hive-c0re` every 30 seconds, the root unit every 10 minutes |
| `swarm/services/k8s/agent-token` | `k3s-join-token-publish`, on the cluster's main node, as that node's hive: k3s's `agent-token`, written again whenever the file differs | a node that joins: `k3s-join-token`, as that node's hive, before k3s starts | ✅ at each start of the main node's k3s | ✅ at each start of the joining node's k3s |
| `swarm/hives/<hive>/oidc/client` | authelia, first boot, straight into the store | the hive | ❌ minted once, straight into the store; minted again only when the store holds none | ✅ re-read when authelia refuses it |
| `swarm/services/<clientId>/oidc/client` | authelia | that service | must be stated | must be stated |
| `swarm/services/ci-runner/registration-token` | `swarm-controller`, from the forge, at start and every 5 minutes | `swarm-bao-ci-runner` on the runner's host | ❌ the forge keeps one token until an admin resets it; the controller then writes the new one within 5 minutes | ✅ every 10 minutes; restarts the runner on a change |
@ -160,6 +161,7 @@ pieces fit, and the reasoning behind them, is in [`credentials.md`](../../2-hand
| the secret store's own contents | openbao, on first `bao operator init` — **an operator action, not a unit** | inside the `swarm-bao` container, at its own `/var/lib/openbao`, kept across rebuilds by `ephemeral = false`. ⚠️ **Not a host path**: `nixos-container destroy swarm-bao` takes the raft data with it, so back up the container's tree, not `/var/lib/`. Only the store's TLS material (`/var/lib/swarm-bao-tls`) and its PKCS11 token (`/var/lib/swarm-bao-token`) are host-level | n/a — there is one store; a hive elsewhere is a _client_ of it and holds none of this |
| the secret store's unseal material | the HSM/TPM under `deploy.bao.seal = "pkcs11"`; openbao itself under `"shamir"` | in the token; or held by whoever ran `bao operator init`, which is what `"shamir"` means and why it's stated rather than inferred | n/a — only the host running the store seals anything |
| a cluster pod's store login token (a ServiceAccount token) | the cluster's API server, which signs it for the pod's account and the audience `bao`. The kubelet projects it into the pod and rewrites it before it expires | `/var/run/secrets/hyperhive/bao/token` in the pod. The store accepts it at its `jwt-k8s` mount, for the role of that account only, against the public halves of the node's signing keys, which `swarm-bao-k8s-jwt-policy` derives from the key files. A node whose signing keys changed needs that unit restarted, and tokens signed with the old key fail to log in meanwhile | Nothing to place: the pods and the store run on the cluster's one host |
| the k3s join token | `k3s-join-token-publish` on the main node writes `swarm/services/k8s/agent-token` from the file k3s keeps | `/run/k3s-join/token` (tmpfs, root, `0400`) on a node with `deploy.k8s.join`, which `k3s-join-token` fills from the store under that host's own hive identity and k3s reads as its token | A different token than the server's: the node fails to register and k3s retries. The server's file wins: the main node's unit rewrites the leaf at its next k3s start, and the joining node's next k3s start reads it. A node that isn't a NixOS host gets the token from an admin, who reads the leaf (`docs/4-implementation/k8s/node.md`) |
### Hive-level — one of each per hive