232 lines
21 KiB
Markdown
232 lines
21 KiB
Markdown
# 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.<svc>.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.<svc>.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` |
|
|
| `.hyperhive-migrated` in a PVC | the copy marker the pod's start gate waits 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. For
|
|
NATS that's the stream list with message counts and last sequences, read
|
|
from the monitoring endpoint (`/jsz?streams=true`). For a state directory
|
|
it's a file list with sizes. The record goes to
|
|
`/var/lib/hyperhive-migrate/<svc>.json`.
|
|
2. **Cut-over rebuild** (the operator). The operator sets
|
|
`deploy.<svc>.runtime = "k8s"` and rebuilds. The rebuild stops the host
|
|
unit and renders it no more, and applies the workload and its PVC. The
|
|
pod starts in a **start gate**: an init container that waits until the
|
|
PVC holds `.hyperhive-migrated`. 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 and the start gate run a small `swarmctl` image with the hidden
|
|
subcommands `migrate receive` and `migrate gate`. 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 | 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 | the state directory, to a PVC | the CronJob's next Job completes | `runtime = "k8s"`, rebuild; the timer goes, the CronJob runs. Rollback copies the PVC back |
|
|
| NATS + `swarm-nats-auth` | JetStream's `store_dir`: the swarm's desired state, operator profiles, schedules and run records. `swarm-nats-auth` keeps nothing | the whole `store_dir`, to a PVC; the cut-over rebuild stops NATS, so the copy reads a quiescent tree | 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 copies the PVC back |
|
|
| `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.
|
|
`swarmctl migrate` treats the whole `store_dir` as data to keep.
|
|
- **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.
|
|
|
|
## 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`**, in one
|
|
rebuild or one at a time. No state, same pattern as step 1.
|
|
3. **`swarm-ci-metrics`.** The first copy, of one small file. It exercises
|
|
the snapshot, the gate, the copy and the rollback on state whose loss
|
|
costs a re-push, not data.
|
|
4. **NATS with `swarm-nats-auth`.** The riskiest move: the largest copy, every
|
|
hive and agent disconnects, and the data is the swarm's desired state.
|
|
The copy path has run once by then. `swarm-nats-auth` moves in the same
|
|
pod, because it answers the auth callout on every connect and dials NATS
|
|
on loopback.
|
|
5. **`swarm-controller` last.**
|
|
- It's the operator's view of the swarm. Kept on the host through step 4,
|
|
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 | `.hyperhive-migrated` in the PVC, read through a copy pod |
|
|
|
|
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 <svc>`, 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 <svc>` 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 `<host dir>.from-k8s` and checks its digest.
|
|
3. Renames the host directory to `<host dir>.pre-rollback` and
|
|
`<host dir>.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. Rolling NATS back to the untouched host copy would lose every
|
|
desired-state change made since then.
|
|
|
|
`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: `swarmctl migrate`
|
|
writes a marker that names no source, and the gate opens.
|
|
|
|
## Downtime and what hives see
|
|
|
|
The budget is the time from the cut-over rebuild stopping the host unit to the
|
|
pod passing step 4.
|
|
|
|
| service | budget | what hives, agents and the operator see |
|
|
| ------------------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
| `swarm-notify-router` | 2 min | Forge events wait in the stream behind the durable consumer and arrive late |
|
|
| `swarm-review-classifier` | 2 min | A try-out request in the gap gets no answer, and its caller reports a failure |
|
|
| `swarm-claude-login` | 2 min | A login in progress fails; the operator starts it again |
|
|
| `swarm-stt` | 2 min | Transcription requests fail during the gap; the cache starts empty |
|
|
| `swarm-ci-metrics` | one 5-minute period | One pass runs late; the copied cursor means no run gets pushed twice |
|
|
| NATS + `swarm-nats-auth` | 10 min; the copy takes most of it | below |
|
|
| `swarm-controller` | 5 min | The swarm UI and API answer with gateway errors. Forge webhook deliveries in the gap fail at the forge. After the cut-over, `swarmctl` on the host needs `--swarm` |
|
|
|
|
During the NATS gap:
|
|
|
|
- Every hive, agent and swarm daemon loses its connection. The queue client
|
|
retries at once, then backs off from 500 ms up to one minute. Clients
|
|
therefore return within a minute of the pod passing readiness.
|
|
- The desired-state buckets aren't readable. Hives apply no change, and
|
|
running agents keep running as they are.
|
|
- Operator input to agents and agent status in the UI stall, and the UI shows
|
|
status as stale.
|
|
- Core NATS messages published in the gap get lost. JetStream publishes fail
|
|
at the publisher.
|