The Deployment swarm-grafana replaces the swarm-grafana container: one replica on a static local volume that is the container's data directory, an init container that fetches the OIDC client secret and creates the administrator password, the datasources' bearer and the encryption key in the secret store, and a sidecar that keeps the controller's Viewer token. The datasources reach the stores through the gateway with that bearer, and Grafana's /metrics asks for the stores' kind of credential, which the vhost sends. The five host units, the journal forwarder, its client glue and the unix-socket directory go with the container.
365 lines
19 KiB
Markdown
365 lines
19 KiB
Markdown
# The cluster node
|
|
|
|
`services.hyperhive.deploy.k8s.enable` makes the host run the main node of a k3s
|
|
cluster for the swarm services that run in the cluster. It defaults on where the
|
|
host enables one of them (`deploy.notify-router`, `stt`, `ci-metrics`,
|
|
`review-classifier`, `claude-login`, `nats`, `swarm-controller`, `victorialogs` or
|
|
`victoriametrics`), which
|
|
`services.hyperhive.deploy.singleHostSwarm` and
|
|
`services.hyperhive.deploy.allSwarmServices` do, so the all-local host gets the
|
|
cluster with its services in it and no option set. Setting it to `false` on such
|
|
a host fails the evaluation, naming the option and the services. 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
|
|
|
|
- **k3s:** one server, which also runs the workloads. traefik and
|
|
metrics-server are off, and the cluster encrypts its Secrets at rest
|
|
(`--secrets-encryption`). CoreDNS and the local-path provisioner stay on, so
|
|
pods have cluster DNS and PersistentVolumeClaims.
|
|
- **Pod network:** flannel's `wireguard-native` mode
|
|
(`--flannel-backend=wireguard-native`), so the traffic between nodes is
|
|
encrypted and authenticated. Each node holds a WireGuard key of its own,
|
|
k3s publishes the public keys through the API server, and a node drops a
|
|
frame from a host that holds no listed key, whatever address it comes from.
|
|
A joined node takes the mode from the server and needs no flag. The
|
|
tunnel is the device `flannel-wg`, and the cluster is IPv4 only, so there
|
|
is no IPv6 device or port. Every node runs the same mode: a node that runs
|
|
another one doesn't reach the others' pods.
|
|
- **Tunnel port:** UDP 51830, set by `--flannel-conf`, which names a net-conf
|
|
file the module writes with the pod range (`deploy.k8s.podCidr`) and the
|
|
tunnel's `ListenPort`. k3s has no flag for the port, and flannel's own
|
|
default, 51820, is the port of the `wg0` that a host in the
|
|
[semi-spread example](../../2-handbook/swarm/semi-spread-deploy.md#the-vpn-raw-wireguard)
|
|
runs, and a host in that mesh is a cluster node as well. Every node reads
|
|
its own file, so a joined host passes the flag too and sets the same
|
|
`deploy.k8s.podCidr` as the main node. The option
|
|
`deploy.k8s.flannelWgPort` holds the port, and the firewall rule and the
|
|
tests read it.
|
|
- **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 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.
|
|
- **Addresses:** pods take their addresses from `deploy.k8s.podCidr` (default
|
|
`10.200.0.0/16`) and services from `deploy.k8s.serviceCidr` (default
|
|
`10.201.0.0/16`). Neither may overlap the hive bridge's subnet
|
|
(`network.bridgeIp`, default `10.42.0.0/24`) or the other, and the
|
|
evaluation fails when one does: the bridge and the cluster would route the
|
|
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-wg`, are
|
|
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)).
|
|
- **NetworkManager:** a host that runs it tells it to leave `cni0`, the
|
|
`flannel*` devices and the pods' `veth*` ends alone, since it otherwise
|
|
rewrites the routes of the interfaces the CNI creates in the default
|
|
namespace. A host without NetworkManager gets no setting. A joining node
|
|
gets the same list. `veth*` also matches other veth pairs with that prefix,
|
|
such as podman's.
|
|
- **NetworkPolicy:** k3s's own controller (kube-router, iptables) enforces
|
|
NetworkPolicy objects; the node doesn't turn it off. The namespace has one
|
|
policy, `default-deny-ingress`, that selects every pod and admits nobody, so
|
|
the node's own addresses reach a pod, and so does whatever a policy of its
|
|
own admits. Other pods and the bridge's agents don't reach a port a pod opens
|
|
later until a policy names it. [The manifests](manifests.md#pods)
|
|
say which pods have a policy of their own.
|
|
- **Secret store:** the store gets a second listener and an auth mount for the
|
|
pods' ServiceAccount tokens, described under
|
|
[Logging in to the secret store](#logging-in-to-the-secret-store).
|
|
|
|
The first `ip netns add` since boot, which makes `/run/netns` a mount point,
|
|
leaves the node's running pods stuck Terminating until a reboot: it mounts
|
|
`/run/netns` over their network namespaces, which containerd then can't remove.
|
|
|
|
## What it leaves alone
|
|
|
|
The cluster holds the workload of the swarm services the host enables, and no
|
|
other: [the runtime module](runtime.md) applies their manifests, and each runs
|
|
there and nowhere else, because two copies of `swarm-notify-router`,
|
|
`swarm-ci-metrics` or `swarm-controller` would share one queue consumer, one
|
|
cursor or one swarm. A host that enables none has a cluster with no hyperhive
|
|
workload.
|
|
|
|
With the option off, the host's configuration is the one it has without the
|
|
module.
|
|
|
|
## Node labels
|
|
|
|
A service whose data sits on one node's disk runs on that node, and the node
|
|
says so with a label. The module sets the labels
|
|
(`services.hyperhive.deploy.k8s.nodeLabels`, passed to `services.k3s.nodeLabel`):
|
|
|
|
- `hyperhive.darkest.space/state-<service>=true`, one per service whose data
|
|
the cluster keeps. Today that's `nats`, `ci-metrics`, `victorialogs`, `victoriametrics` and `grafana`, each when its
|
|
`deploy.<service>.enable` is on. The
|
|
service's static local volume has a `nodeAffinity` on the label, and its pod a
|
|
`nodeSelector` on it, so a node without the label can't run the pod and shows
|
|
as no match instead of a volume that can't bind. One label per service, so
|
|
moving one service's data moves one label.
|
|
- `hyperhive.darkest.space/entry=true`, on the node the hosts' DNS points at.
|
|
Nothing selects on it yet. The pods that hosts dial will, as they move in.
|
|
- `svccontroller.k3s.cattle.io/enablelb=true`. With this label on any node, k3s's
|
|
service load balancer binds a published port on the labelled nodes only
|
|
(k3s documents it under "Controlling servicelb node selection"). Without it, a
|
|
node that joins would also bind the queue's port 4222.
|
|
|
|
On the single node, every label is on the node and the pods are the ones the
|
|
node ran without them, plus a `nodeSelector`. **A node that joins carries
|
|
none of the labels**, so it can't run a pod that keeps data on a disk, and
|
|
the load balancer doesn't bind the queue's port on it. A stateless pod can run
|
|
there.
|
|
|
|
k3s applies `--node-label` to the node's object at every start, whether the
|
|
node registered before or not, so a node that registered before a label existed
|
|
has the label after the restart a switch causes. A pod that selects on a label
|
|
the node doesn't have yet waits as Pending and starts when the label arrives.
|
|
k3s only adds labels. A label the module no longer sets, such as `state-nats`
|
|
after the queue moves back to `"host"`, stays on the node and does nothing,
|
|
because no pod selects on it. `kubectl label node <name> <label>-` removes it.
|
|
|
|
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 its external IP (`--node-external-ip`),
|
|
the server runs `--flannel-external-ip`, and the API certificate lists it
|
|
(`--tls-san`). flannel then points the tunnel at that address. Unset,
|
|
k3s picks the default route's address for the node, and flannel binds the
|
|
tunnel to the default route's interface and advertises that interface's
|
|
address, which has to be one of the peers' `nodeAddresses`.
|
|
- `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 51830 (flannel's
|
|
WireGuard tunnel) and TCP 5001 (the mirror's peers). The tunnel checks the
|
|
peer's key as well as its address, so the source filter is a second
|
|
check. 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. Write the flannel net-conf, which names the tunnel's port, to
|
|
`/etc/rancher/k3s/flannel-net-conf.json`. `<pod range>` is the main node's
|
|
`deploy.k8s.podCidr`:
|
|
|
|
```json
|
|
{
|
|
"Network": "<pod range>",
|
|
"EnableIPv4": true,
|
|
"EnableIPv6": false,
|
|
"IPv6Network": "::/0",
|
|
"Backend": {
|
|
"Type": "wireguard",
|
|
"PersistentKeepaliveInterval": 25,
|
|
"ListenPort": 51830
|
|
}
|
|
}
|
|
```
|
|
|
|
5. Start k3s as an agent:
|
|
|
|
```bash
|
|
k3s agent --server https://<main node>:6443 --token-file <token file> --node-ip <the machine's address> --node-external-ip <the machine's address> --flannel-conf /etc/rancher/k3s/flannel-net-conf.json
|
|
```
|
|
|
|
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
|
|
cluster's CoreDNS answers each such name with the hive bridge's address
|
|
(`network.bridgeIp`), the address the agents' resolver gives, so a pod reaches
|
|
the same listeners an agent does: the gateway's vhosts, the secret store's TLS
|
|
port, the queue and the hive's OTLP collector. The names are the hive domain
|
|
and every name a host module contributes to `gateway.localNames` because the
|
|
host runs that service. `services.hyperhive.deploy.k8s.hostServices` is that
|
|
table, and it holds the only host address the cluster knows.
|
|
|
|
The node applies the table to k3s as a `coredns-custom` ConfigMap, which is a
|
|
node object and not a service manifest. A name outside the table, and every
|
|
cluster name, resolves as before. A host without the hive bridge has no
|
|
address to answer with, so it gets no ConfigMap.
|
|
|
|
The firewall trusts the pod network's interfaces, so a pod reaches
|
|
these listeners without a firewall rule per port. Each service still
|
|
authenticates its callers: every listener of the secret store but its pods'
|
|
one refuses a connection without a client certificate.
|
|
|
|
## Reaching the cluster from the host
|
|
|
|
The host's resolver, the hive's dnsmasq (the host's own `/etc/resolv.conf`
|
|
names it), forwards the cluster's domain to CoreDNS. `deploy.k8s.clusterDomain`
|
|
is `cluster.local` and `deploy.k8s.clusterDns`, the tenth address of
|
|
`deploy.k8s.serviceCidr`, is the address k3s gives CoreDNS: the node passes
|
|
both as flags (`--cluster-domain`, `--cluster-dns`) and the resolver's
|
|
`server=/cluster.local/<address>` line reads the same values. A process on the
|
|
host, nginx included, so reaches a Service as
|
|
`<name>.<namespace>.svc.cluster.local`. The node doesn't set the line without
|
|
`gateway.dns.enable`, the option that runs that resolver.
|
|
|
|
A ClusterIP answers the host because kube-proxy (k3s runs it in `iptables`
|
|
mode) rewrites a host process's connection to the Service's address into the
|
|
pod's, and the firewall trusts the pod network's interfaces. The bridge's
|
|
agents ask the same resolver, so they get the same names. A name is no route:
|
|
the hive's egress rules decide whether the host forwards their traffic to the
|
|
cluster's addresses, and a pod's NetworkPolicy refuses what it forwards.
|
|
|
|
A pod that serves the host, such as the controller or the queue's exporter, has
|
|
a NetworkPolicy that selects it for ingress and allows no more than the port it
|
|
serves everyone, if it has one. kube-router, the policy engine, adds a rule at
|
|
the top of every pod's chain that admits traffic from any address of the node
|
|
(`-m addrtype --src-type LOCAL`, commented "from local node"), so the host
|
|
reaches the pod and no other pod does.
|
|
|
|
## Logging in to the secret store
|
|
|
|
A pod holds no client certificate. It logs in to the secret store with a
|
|
ServiceAccount token the kubelet projects into it, and the store checks that
|
|
token against the node's own signing keys.
|
|
|
|
- **Listener:** the store binds a listener on the hive bridge's address at
|
|
`deploy.bao.podListenerPort` (default `8208`). It serves the store's server
|
|
certificate and asks for no client certificate. A pod dials it by the name it
|
|
dials the store by, `bao.<swarm domain>`, which CoreDNS answers with the
|
|
bridge's address.
|
|
- **Firewall:** that port is open on `cni0` and `flannel-wg` and on no other
|
|
interface. The bridge's firewall refuses it, so an agent on the bridge can't
|
|
connect. Binding an address doesn't isolate a port, because the kernel
|
|
accepts a packet for any local address on any interface, so the firewall
|
|
rule does. Every other listener of the store keeps its client certificate
|
|
requirement.
|
|
- **Auth mount:** `jwt-k8s`. `swarm-bao-granter-role` enables it with the
|
|
bootstrap token, and `swarm-bao-k8s-jwt-policy` writes its config: the
|
|
public halves of the keys k3s signs ServiceAccount tokens with, derived from
|
|
the key files under `/var/lib/rancher/k3s/server/tls`, and the issuer the
|
|
node gives its API server, `https://kubernetes.default.svc.cluster.local`.
|
|
The unit runs after k3s and needs no manual step. Restart it after a rotation
|
|
of the node's signing keys.
|
|
- **Roles:** one per in-cluster service, named like the service and its cert
|
|
role (`swarm-controller`, `swarm-notify-router`, `swarm-ci-metrics`). A role
|
|
accepts the token of the ServiceAccount of that name in the services'
|
|
namespace (`system:serviceaccount:<namespace>:<service>`), and only with the
|
|
audience `bao`, so a token meant for the Kubernetes API is no use to the
|
|
store. It hands out the policy and token lifetimes of the service's cert role.
|
|
- **Pod:** the [rendered manifest](manifests.md#the-store-login) runs the pod
|
|
under that ServiceAccount, with a token projected for the audience `bao` and
|
|
`BAO_AUTH=jwt` in its environment.
|
|
|
|
The bootstrap policy that `swarmctl bao grant-bootstrap` applies carries
|
|
`sys/auth/jwt-k8s`, so a store set up before the node existed needs that step
|
|
once more.
|
|
|
|
### Turning the node on for a swarm that already runs
|
|
|
|
The first activation with the node on restarts the store's container,
|
|
`container@swarm-bao`. The node adds a `pod` listener to the store's
|
|
configuration, and a changed container configuration restarts the container.
|
|
What comes next depends on the store's seal:
|
|
|
|
- With the default `pkcs11` seal the store unseals itself. Skip the unseal
|
|
below.
|
|
- With `deploy.bao.seal = "shamir"` the store comes back sealed. Every unit that
|
|
reads it fails until an operator unseals it, and `swarmctl bao grant-bootstrap`
|
|
can't log in either. Unseal first, with the saved keys, as
|
|
[setup](../../1-getting-started/setup.md) does:
|
|
|
|
```bash
|
|
bao operator unseal # repeat with different keys until the store reports unsealed
|
|
```
|
|
|
|
Then, on either seal, as root on the host that runs `swarm-bao`:
|
|
|
|
```bash
|
|
swarmctl bao grant-bootstrap # prompts for the root token, input hidden
|
|
systemctl reset-failed 'swarm-bao-*-policy.service' swarm-bao-agent-pki.service
|
|
systemctl restart 'swarm-bao-*-policy.service' swarm-bao-agent-pki.service
|
|
```
|
|
|
|
The grant adds `sys/auth/jwt-k8s` to the bootstrap policy, and the restart lets
|
|
`swarm-bao-granter-role` enable the mount and `swarm-bao-k8s-jwt-policy` write
|
|
the pods' login. Until both have run, the pods can't log in.
|
|
|
|
Until the grant runs, `swarmctl setup status` shows step 3, the granter
|
|
bootstrap, as not done. `swarm-bao-k8s-jwt-policy` exits 0 and keeps
|
|
`/run/swarm-bao-k8s-jwt-pending` while the bootstrap token lacks the grant, and
|
|
the status reads that file.
|
|
|
|
## Validation
|
|
|
|
`module-eval-k8s` compares an all-local host with the option on against the
|
|
same host with it off, and checks the address ranges and the CoreDNS
|
|
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. 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.
|