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:
parent
aab13d065f
commit
7819ed2dbd
3 changed files with 1106 additions and 96 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue