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 7819ed2dbd

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.