pin queue and poller state to per-service node labels instead of the hostname
This commit is contained in:
parent
ab6a180e79
commit
79c06cdcb0
5 changed files with 348 additions and 40 deletions
36
nix/host-modules/k8s/labels.nix
Normal file
36
nix/host-modules/k8s/labels.nix
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# The node labels that pin pods to a node, and the selectors that find them.
|
||||
# ./node.nix puts the labels on the node; the renderer and the queue's module
|
||||
# select on them. docs/4-implementation/k8s/node.md has the scheme.
|
||||
rec {
|
||||
# The node that holds the data a service keeps in the cluster. One label per
|
||||
# service, so moving one service's data moves one label.
|
||||
stateLabel = service: "hyperhive.darkest.space/state-${service}";
|
||||
|
||||
# The node the hosts' DNS points at: the pods that hosts dial run there.
|
||||
entryLabel = "hyperhive.darkest.space/entry";
|
||||
|
||||
# k3s's own (docs.k3s.io/networking/networking-services, "Controlling servicelb
|
||||
# node selection"): once any node carries it, the service load balancer binds
|
||||
# a published port on the labelled nodes only.
|
||||
loadBalancerLabel = "svccontroller.k3s.cattle.io/enablelb";
|
||||
|
||||
# A pod's `nodeSelector` for a service's state label.
|
||||
stateSelector = service: {
|
||||
${stateLabel service} = "true";
|
||||
};
|
||||
|
||||
# A static local volume's `nodeAffinity` for the same label.
|
||||
stateAffinity = service: {
|
||||
required.nodeSelectorTerms = [
|
||||
{
|
||||
matchExpressions = [
|
||||
{
|
||||
key = stateLabel service;
|
||||
operator = "In";
|
||||
values = [ "true" ];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
|
|
@ -67,6 +67,15 @@ let
|
|||
# is passed explicitly below, so the address the host's resolver forwards
|
||||
# to and the one CoreDNS gets agree by construction.
|
||||
clusterDns = intToIp (networkOf cfg.serviceCidr + 10);
|
||||
|
||||
labels = import ./labels.nix;
|
||||
deployCfg = config.services.hyperhive.deploy;
|
||||
|
||||
# The services whose data the module keeps in the cluster: those with a
|
||||
# `runtime` that is `k8s`. Each puts its state label on this node.
|
||||
stateful = lib.filterAttrs (_: svc: svc.enable && svc.runtime == "k8s") {
|
||||
inherit (deployCfg) nats ci-metrics;
|
||||
};
|
||||
in
|
||||
{
|
||||
options.services.hyperhive.deploy.k8s = {
|
||||
|
|
@ -176,6 +185,37 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
nodeLabels = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
readOnly = true;
|
||||
default =
|
||||
lib.genAttrs [
|
||||
labels.entryLabel
|
||||
labels.loadBalancerLabel
|
||||
] (_: "true")
|
||||
// lib.mapAttrs' (name: _: lib.nameValuePair (labels.stateLabel name) "true") stateful;
|
||||
defaultText = lib.literalMD "computed from the services whose `runtime` is `k8s`";
|
||||
description = ''
|
||||
The labels this node carries, which the module sets with
|
||||
`services.k3s.nodeLabel`. The pods and volumes that need a node select
|
||||
on them, so a node that joins the cluster and has none of them takes
|
||||
none of those pods.
|
||||
|
||||
- `hyperhive.darkest.space/state-<service>` for each service whose data the
|
||||
cluster keeps: `nats` and `ci-metrics`, each when its `runtime` is
|
||||
`k8s`. A volume's `nodeAffinity` and the pod's `nodeSelector` name it.
|
||||
- `hyperhive.darkest.space/entry`: the node the hosts' DNS points at.
|
||||
- `svccontroller.k3s.cattle.io/enablelb`: with it on any node, the
|
||||
service load balancer binds a published port on the labelled nodes
|
||||
only, so a joined node does not publish the queue's port.
|
||||
|
||||
k3s puts these on the node's object at every start, registered or not.
|
||||
It never takes one off: a label left from a service that has moved
|
||||
back to `host` stays and does nothing, and `kubectl label node <name>
|
||||
<label>-` removes one.
|
||||
'';
|
||||
};
|
||||
|
||||
manifestDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
|
|
@ -232,6 +272,7 @@ in
|
|||
"traefik"
|
||||
"metrics-server"
|
||||
];
|
||||
nodeLabel = lib.mapAttrsToList (k: v: "${k}=${v}") cfg.nodeLabels;
|
||||
extraFlags = [
|
||||
"--secrets-encryption"
|
||||
"--cluster-cidr=${cfg.podCidr}"
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ let
|
|||
controllerOnCluster = controllerCfg.enable && controllerCfg.runtime == "k8s";
|
||||
controllerTcp = hyperhiveCfg.swarm.controller.tcp;
|
||||
natsCfg = deployCfg.nats;
|
||||
labels = import ./labels.nix;
|
||||
servicesRootPath = "${deployCfg.hive-controller.tls.stateDir}/${servicesRootKey}";
|
||||
caSources = {
|
||||
${trustBundleKey} = if caTrust.useSelfSigned then caTrust.caHostPath else null;
|
||||
|
|
@ -275,6 +276,13 @@ let
|
|||
healthEnv = null;
|
||||
etcFiles = [ ];
|
||||
publicCredentials."bao-ca.pem" = baoServerCaKey;
|
||||
# A static local volume on the unit's own state directory, in place of the
|
||||
# default class: it binds only on the node with the service's state label
|
||||
# (./labels.nix).
|
||||
localState = {
|
||||
service = "ci-metrics";
|
||||
hostDir = deployCfg.ci-metrics.hostStateDir;
|
||||
};
|
||||
otlp = "own";
|
||||
# The timer and the service both: the prune unit holds back either, and a
|
||||
# service started by hand cannot run beside the CronJob.
|
||||
|
|
@ -551,6 +559,8 @@ let
|
|||
runtimeDir = if sc ? RuntimeDirectory then "/run/${sc.RuntimeDirectory}" else null;
|
||||
stateDir = if sc ? StateDirectory then "/var/lib/${sc.StateDirectory}" else null;
|
||||
ephemeral = svc.ephemeralState or false;
|
||||
localState = svc.localState or null;
|
||||
stateClaim = if localState != null then "${name}-store" else "${name}-state";
|
||||
|
||||
# PATH holds only NixOS's default tools (checked below), which the image
|
||||
# does not carry. `%d` is systemd's specifier for the credential
|
||||
|
|
@ -732,9 +742,7 @@ let
|
|||
{
|
||||
name = "state";
|
||||
}
|
||||
// (
|
||||
if ephemeral then { emptyDir = { }; } else { persistentVolumeClaim.claimName = "${name}-state"; }
|
||||
)
|
||||
// (if ephemeral then { emptyDir = { }; } else { persistentVolumeClaim.claimName = stateClaim; })
|
||||
)
|
||||
++ lib.optional (svc.etcFiles != [ ]) {
|
||||
name = "etc";
|
||||
|
|
@ -957,6 +965,9 @@ let
|
|||
}
|
||||
// lib.optionalAttrs baoLogin { serviceAccountName = name; }
|
||||
// lib.optionalAttrs (svc ? hostUsers && !svc.hostUsers) { hostUsers = false; }
|
||||
// lib.optionalAttrs (localState != null) {
|
||||
nodeSelector = labels.stateSelector localState.service;
|
||||
}
|
||||
// lib.optionalAttrs (listener != { } || extra.initContainers != [ ]) {
|
||||
initContainers = lib.optional (listener != { }) leafContainer ++ extra.initContainers;
|
||||
}
|
||||
|
|
@ -1048,10 +1059,30 @@ let
|
|||
"${name}/persistentvolumeclaim" = {
|
||||
apiVersion = "v1";
|
||||
kind = "PersistentVolumeClaim";
|
||||
metadata = metaFor "${name}-state";
|
||||
metadata = metaFor stateClaim;
|
||||
spec = {
|
||||
accessModes = [ "ReadWriteOnce" ];
|
||||
resources.requests.storage = "1Gi";
|
||||
}
|
||||
// lib.optionalAttrs (localState != null) {
|
||||
storageClassName = stateClaim;
|
||||
volumeName = stateClaim;
|
||||
};
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (stateDir != null && !ephemeral && localState != null) {
|
||||
"${name}/persistentvolume" = {
|
||||
apiVersion = "v1";
|
||||
kind = "PersistentVolume";
|
||||
# Cluster-scoped: no namespace.
|
||||
metadata = removeAttrs (metaFor stateClaim) [ "namespace" ];
|
||||
spec = {
|
||||
capacity.storage = "1Gi";
|
||||
accessModes = [ "ReadWriteOnce" ];
|
||||
persistentVolumeReclaimPolicy = "Retain";
|
||||
storageClassName = stateClaim;
|
||||
local.path = localState.hostDir;
|
||||
nodeAffinity = labels.stateAffinity localState.service;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@ let
|
|||
|
||||
onK8s = cfg.runtime == "k8s";
|
||||
|
||||
# Where the host unit's `StateDirectory` is: `DynamicUser` keeps it under
|
||||
# `/var/lib/private`, so the pod's volume is the directory the unit uses.
|
||||
hostStateDir = "/var/lib/private/${unit}";
|
||||
|
||||
# `hostUnit`: a host service, so the bundle reads the host CA path and waits
|
||||
# on `hive-tls-ca.service`. `enable`: `imports` is unconditional, so without
|
||||
# it a host with the poller off gets the bundle unit and an `ExecStart`-less
|
||||
|
|
@ -209,16 +213,28 @@ in
|
|||
configuration must enable. A switch moves the poller: the service and
|
||||
timer go and the CronJob is applied, or the CronJob is deleted and the
|
||||
service and timer return, so one poller runs at any time. Its cursor is
|
||||
in the service's state directory on the host, and in a volume of the
|
||||
cluster in a pod; neither moves with the poller, so the first pass after
|
||||
a switch pushes every run again, as the same points. The pod starts on
|
||||
its empty volume with no gate.
|
||||
in the service's state directory on the host, and the cluster's volume
|
||||
for it is on that directory, so a switch moves the process and not the
|
||||
cursor. The pod runs on the node with the
|
||||
`hyperhive.darkest.space/state-ci-metrics` label, which the node
|
||||
module sets, and starts with no gate.
|
||||
|
||||
The poller keeps pushing to the swarm collector's route of its own,
|
||||
not to the hive collector the cluster's other pods push to.
|
||||
'';
|
||||
};
|
||||
|
||||
hostStateDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
readOnly = true;
|
||||
internal = true;
|
||||
default = hostStateDir;
|
||||
description = ''
|
||||
The directory the poller's service keeps its cursor in on the host,
|
||||
which the cluster's volume for it is on (./k8s/render.nix).
|
||||
'';
|
||||
};
|
||||
|
||||
unit = lib.mkOption {
|
||||
type = lib.types.raw;
|
||||
readOnly = true;
|
||||
|
|
@ -287,6 +303,14 @@ in
|
|||
# name, which the cluster resolves to the hive bridge.
|
||||
(lib.mkIf (cfg.enable && onK8s) {
|
||||
services.hyperhive.network.enable = lib.mkDefault true;
|
||||
|
||||
# The pod's local volume needs its directory to exist before the pod
|
||||
# starts, which on a host that never ran the service nothing else makes.
|
||||
# The parent is the one the service manager makes for a dynamic user.
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /var/lib/private 0700 root root -"
|
||||
"d ${hostStateDir} 0700 root root -"
|
||||
];
|
||||
})
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ let
|
|||
|
||||
machine = "swarm-nats";
|
||||
|
||||
labels = import ./k8s/labels.nix;
|
||||
|
||||
# The container's journal forwarder pushes to its own receiver on the swarm
|
||||
# collector, the route `serviceProducers.swarm-nats` in
|
||||
# ./swarm-otel-service.nix declares, by the collector's gateway name.
|
||||
|
|
@ -958,10 +960,16 @@ let
|
|||
}
|
||||
];
|
||||
|
||||
podSpec = {
|
||||
shareProcessNamespace = true;
|
||||
# Only the node with the queue's state label runs the pod: the volume's
|
||||
# affinity says the same, and a node without it shows as no match
|
||||
# instead of a volume that cannot bind.
|
||||
nodeSelector = labels.stateSelector "nats";
|
||||
};
|
||||
|
||||
# The server and the responder dial the queue by its name, which the pod
|
||||
# answers with its own loopback: the leaf carries the name and no IP.
|
||||
podSpec.shareProcessNamespace = true;
|
||||
|
||||
hostAliases = [
|
||||
{
|
||||
ip = "127.0.0.1";
|
||||
|
|
@ -1056,17 +1064,7 @@ let
|
|||
persistentVolumeReclaimPolicy = "Retain";
|
||||
storageClassName = stateClass;
|
||||
local.path = hostStoreDir;
|
||||
nodeAffinity.required.nodeSelectorTerms = [
|
||||
{
|
||||
matchExpressions = [
|
||||
{
|
||||
key = "kubernetes.io/hostname";
|
||||
operator = "In";
|
||||
values = [ (lib.toLower config.networking.hostName) ];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
nodeAffinity = labels.stateAffinity "nats";
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue