# Moving an all-local swarm onto k8s: `swarmctl migrate` `swarmctl migrate` moves a running all-local swarm (`services.hyperhive.deploy.singleHostSwarm`) onto the k3s cluster on the same host. It copies each service's state into a persistent volume, checks the copy, and leaves the host copy in place until the operator finishes. It moves services in dependency order, resumes after a crash, and rolls a service back to the host on request. ## Scope and guarantees The command moves the swarm's own daemons and the queue into the cluster: `swarm-notify-router`, `swarm-review-classifier`, `swarm-claude-login`, `swarm-stt`, `swarm-ci-metrics`, NATS with `swarm-nats-auth`, and `swarm-controller`. bao, the forge, authelia, the gateway, `swarm-builder`, `swarm-matrix-ctl` and every hive keep running on the host. - **The hive contract stays the same.** Hives and agents keep the NATS URL, its port, its certificate name and its auth callout. No hive needs a rebuild for any move. - **The host copy of the state stays on disk** until the operator runs `swarmctl migrate finish`. A forward move only reads it, so a rollback always has it to return to. - **One nix option decides where a service runs.** `deploy..runtime` is `"host"` (the default) or `"k8s"`. The cut-over is an operator rebuild, never a runtime toggle that `swarmctl` flips on its own. ### Names | name | what it names | | --------------------------------------------------- | --------------------------------------------------------------- | | `services.hyperhive.deploy.k8s.enable` | brings up single-node k3s on the swarm host | | `services.hyperhive.deploy..runtime` | `"host"` or `"k8s"`, per service | | `/etc/hyperhive/migrate.json` | the module's render of each service's runtime, state paths, PVC | | `/var/lib/hyperhive-migrate/` | host record: pre-move snapshots and copy digests, root `0700` | | `SWARMCTL_KUBECTL`, `SWARMCTL_KUBECONFIG` | the `swarmctl` wrapper's `kubectl` and the k3s admin kubeconfig | | `.hyperhive-migrated` in a PVC | the copy marker the pod's start gate checks for | | `swarmctl migrate` `status` / `rollback` / `finish` | the verbs | ## The move of one stateful service A service with state moves in four steps. A service without state skips steps 2 and 3. 1. **Snapshot** (`swarmctl migrate`, before the rebuild). Records what the service holds while its host unit still runs, for the check in step 4: a file list with sizes of the state directory. The record goes to `/var/lib/hyperhive-migrate/.json`. 2. **Cut-over rebuild** (the operator). The operator sets `deploy..runtime = "k8s"` and rebuilds. The rebuild stops the host unit and renders it no more, and applies the workload and its PVC. The pod's first init container is the **start gate**. It exits 0 only when the PVC holds `.hyperhive-migrated`, or when the PVC is empty and the service's `deploy..k8s.freshState` is on. Any other volume fails the gate: the pod stays in `Init:Error` with one line saying to run `swarmctl migrate ` or set the option, and the kubelet runs the gate again with a growing delay. A pod therefore never starts on an empty volume, whatever re-applies the manifest. 3. **Copy** (`swarmctl migrate`). Refuses while the host unit runs. Streams the host state directory as a tar archive into a copy pod that mounts the PVC, into `.incoming/`. Checks the digest of the unpacked tree against the source, moves `.incoming/` into place, sets ownership to the image's user, and writes `.hyperhive-migrated` last, with the source path and digest. The gate opens. 4. **Verify** (`swarmctl migrate`). Waits for the pod's readiness, then compares what the service serves against the snapshot from step 1. The copy pod runs a small `swarmctl` image with the hidden subcommand `migrate receive`. The start gate is a script in an image of its own that holds coreutils and nothing else. The service images carry no shell and no `tar`, and this keeps them that way. The copy streams through the k8s API instead of writing into the volume's directory on the host, so it works for every storage class. ## Services "Ready" means the pod's readiness probe passes. The probes read the `/readyz` endpoints the [services page](../../2-handbook/swarm/services.md) lists; the [readiness page](../swarm/readiness.md) has their mechanics. | service | state | what moves | readiness signal | cut-over and rollback | | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `swarm-notify-router` | in the queue: a state bucket and a durable consumer on the forge events stream | nothing | `/readyz`: queue connected | `runtime = "k8s"`, rebuild; back with `runtime = "host"` | | `swarm-review-classifier` | none on disk; its runs and settings live in buckets the controller owns | nothing | `/readyz`: queue connected | same | | `swarm-claude-login` | none; credentials it writes go to the store | nothing; a login in progress ends | `/readyz`: queue connected | same | | `swarm-stt` | the transcript cache, entries live 24 h; the pod keeps it in an `emptyDir` | nothing; the pod starts with an empty cache | `/readyz`: queue connected | same | | `swarm-ci-metrics` | `cursor.json` in the state directory; losing it re-pushes every run | nothing; the pod starts on an empty PVC with no gate, and its first pass re-pushes every run as the same points | the CronJob's next Job completes | `runtime = "k8s"`, rebuild; the timer goes, the CronJob runs; back with `runtime = "host"` | | NATS + `swarm-nats-auth` | JetStream's `store_dir`: the swarm's desired state, operator profiles, schedules and run records. `swarm-nats-auth` keeps nothing | nothing; the pod mounts the container's `store_dir` in place | nats-server's `/healthz?js-enabled-only=true` and `swarm-nats-auth`'s `/readyz`; Ready when both pass | `runtime = "k8s"`, rebuild; the pod takes the same host port, so hives keep their URL. Rollback is `runtime = "host"` and a rebuild: the same files | | `swarm-controller` | its state directory: the operator-admin set, the legacy `webhook-secret` fallback and the forge avatar markers. The rest sits in the queue or the store | the state directory, to a PVC | `/readyz` on the controller's plain health listener | `runtime = "k8s"`, rebuild; the same rebuild points the gateway's `/api/` and `/webhook/forge/` upstreams at the controller's TCP listener. Rollback copies the PVC back and returns the upstreams to the socket | Two facts the table rests on: - **The queue's store is state that must survive.** Losing it empties the hive-status view, and it also loses the buckets that hold desired state. It never leaves its directory on the host: the pod and the container use the same files, so a switch in either direction copies nothing, and the streams, consumers and acknowledgements are the ones the other side left. See [the queue's store](#the-queues-store). - **The webhook secret lives in the store.** The state directory's `webhook-secret` is a fallback the controller reads only when the store doesn't answer. The copy moves it anyway, so a pod that starts while the store is down still verifies forge deliveries. Preflight checks that the store holds the secret. ## The queue's store The queue's pod uses the directory its container's `nats` unit already uses: `/var/lib/nixos-containers/swarm-nats/var/lib/nats` on the host, `/var/lib/nats` in both. The module renders a static `local` PersistentVolume on that path, pinned to the node, with a storage class of its own and the `Retain` policy. The claim `swarm-nats-store` binds it by name, so nothing else binds either. - **One server at a time.** The prune unit stops a pod before the container starts, and the Deployment replaces its pod rather than running two. Nothing reads the directory while the other side writes. - **Pod to host.** nats-server creates its files with mode `0600`, owned by the uid it runs as. The pod runs as uid 65532, so the container's `nats` unit runs `chown -R nats:nats` on the directory before the server starts, as root and at every start. - **Host to pod.** The pod's `fsGroup` makes the kubelet set the directory's group to the pod's and add group read and write on every file at each pod start, so the pod's uid can use files the host's `nats` user wrote. - **No lock or identity file.** nats-server keeps none in the store, so nothing there ties the directory to the side that wrote it. - **A host that never ran the container.** The module makes the directory, because the volume needs it before the pod starts. - **An older claim.** A `swarm-nats-state` claim from an earlier shape isn't the queue's any more: the pod mounts `swarm-nats-store`, so the old claim stays unused and needs no step. ## Order of moves `swarmctl migrate` moves the services in this order: 1. **`swarm-notify-router` alone.** The first cut-over proves the runtime switch, the image, the store identity and reaching the host-run NATS by name, with nothing to copy. 2. **`swarm-review-classifier`, `swarm-claude-login`, `swarm-stt`, `swarm-ci-metrics`**, in one rebuild or one at a time. Nothing to copy, same pattern as step 1: the pods start with no gate on an empty volume. 3. **NATS with `swarm-nats-auth`.** The riskiest move: every hive and agent disconnects, and the data is the swarm's desired state. Nothing is copied, and the controller is the only service `swarmctl migrate` copies. `swarm-nats-auth` moves in the same pod, because it answers the auth callout on every connect and dials the server by name, which the pod's host aliases map to its loopback. 4. **`swarm-controller` last.** - It's the operator's view of the swarm. Kept on the host through step 3, it reports from an unchanged place whether the hives reconnected after the NATS move, and `swarmctl` keeps its local socket for the checks. - Its cut-over also changes the gateway's upstream and ends the local socket for `swarmctl`, the widest change to the operator's tools. - Its state is small, so its rollback is cheap. Each step needs the previous one Ready and verified. `swarmctl migrate` says which service comes next, and refuses to copy a service out of order unless the operator passes `--out-of-order`. ## Preflight `swarmctl migrate` runs these before its first snapshot, and again before each copy. A failed check names what's missing and stops; it changes nothing. | check | how | fails when | | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- | | k3s node Ready | `kubectl get nodes` with the k3s admin kubeconfig | the node is NotReady, or `deploy.k8s.enable` is off | | images present | every image the next move needs shows in the node's image list | an image is missing | | store login from a pod | a short-lived probe pod with the next service's identity logs in to bao and reads one path that service may read | the login or the read fails | | host services reachable | the probe pod resolves the bao, forge, authelia and collector names to the node and completes TLS to each; while NATS runs on the host, NATS too | a name doesn't resolve, or a handshake fails | | disk space | free space on the filesystem behind the PVCs is at least twice the sum of the state still to copy, plus 1 GiB | not enough room for the copy and a later rollback copy | | host unit state | the service's host unit is active before the snapshot, and inactive before the copy | the order of steps doesn't hold | | webhook secret in the store (controller only) | the controller's store path for it holds a value | the store has none; the operator restarts the host controller first, which seeds it | | controller listener (controller only) | the controller's TCP listener answers a client holding the gateway leaf, and refuses one without | the listener is off or refuses the gateway | ## Resume and rollback `swarmctl migrate` keeps no progress file of its own. Like `swarmctl setup`, it observes the system, judges each step as done, not done or unknown from what it sees, and plans again after every step. A rerun resumes where the last run stopped, and the loop offers retry, skip or quit on a failed step. ### What's observed and what's recorded | fact | observed from | | ---------------------------------- | ---------------------------------------------------------- | | where the config wants the service | `/etc/hyperhive/migrate.json`, which the rebuild renders | | host unit active or not | `systemctl is-active` | | workload, pod phase, Ready | `kubectl` | | copy done | the pod's `state-gate` init container, in the pod's status | `swarmctl migrate status` reads the gate's answer from the pod's status: the gate exited 0 when the marker is in the volume, and a refusal puts its reason in the container's termination message. Status never starts a pod, so it creates nothing and reads only. Two facts vanish once the host unit stops, so `/var/lib/hyperhive-migrate/` records them: the step 1 snapshot, and the digest of the copied tree. Without a snapshot, step 4 falls back to the stream names on disk and says so. ### Idempotence - The snapshot overwrites its record while the host unit runs, and refuses to once it stops. - The copy starts by emptying `.incoming/`, and runs only while the marker is absent. Rerunning it after an interrupted copy starts over. - A rerun skips a copy whose marker exists. - Verification only reads. - `rollback` and `finish` check their own preconditions and skip what's done. ### Half-done migrations | what the operator sees | resume | roll back | | ------------------------------------------------- | --------------------------------------------------------- | ------------------------------------------------------------------- | | rebuilt, the pod waits in its gate, no marker | `swarmctl migrate` copies | `runtime = "host"`, rebuild; the host state never changed | | `.incoming/` in the PVC, no marker (copy cut off) | `swarmctl migrate` empties it and copies again | same as above | | marker written, pod not Ready | read the pod's log; fix, then `swarmctl migrate` verifies | `swarmctl migrate rollback `, then `runtime = "host"`, rebuild | | Ready, verification failed | the mismatch says what differs; the operator decides | same as above | | verified | nothing to do | same as above | `swarmctl migrate rollback ` works in this order: 1. Removes the marker and deletes the pod. The replacement waits in the gate, so nothing writes to the PVC from here on, whatever re-applies the manifest. 2. Copies the PVC into `.from-k8s` and checks its digest. 3. Renames the host directory to `.pre-rollback` and `.from-k8s` to the host directory, owned by the host unit's user. Both renames stay on one filesystem. 4. Tells the operator to set `runtime = "host"` and rebuild, then verifies the host unit's readiness after that rebuild. The rollback copies back because every write after the cut-over exists only in the PVC. `swarmctl migrate finish` deletes the host copies, the `.pre-rollback` directories and the PVC of any service back on the host. It runs only when every service passed verification where the config wants it. A rebuild that stops a workload leaves its PVC in place, so the data survives until `finish`. A fresh install straight onto k8s has no host state to copy: with `deploy..k8s.freshState` on, the gate marks the empty volume itself (`{"fresh": true, "at":