k8s: deploy.k8s.enable runs a single-node k3s that holds the service images
This commit is contained in:
parent
fd950cab12
commit
9654aa0ac7
6 changed files with 357 additions and 18 deletions
|
|
@ -4,8 +4,10 @@
|
|||
that run in the cluster. `nix/host-modules/k8s/render.nix` builds them from
|
||||
the systemd unit each service runs today. A host with
|
||||
`services.hyperhive.k8s.enable` gets them as `system.build.k8sManifests`, a
|
||||
directory with one YAML file per object. Rendering changes no unit, and
|
||||
nothing in this module applies the manifests to a cluster.
|
||||
directory with one YAML file per object, and the images their pods run as
|
||||
`system.build.k8sImages`, which [the cluster node](node.md) loads. Rendering
|
||||
changes no unit, and nothing in this module applies the manifests to a
|
||||
cluster.
|
||||
|
||||
## Options
|
||||
|
||||
|
|
|
|||
52
docs/4-implementation/k8s/node.md
Normal file
52
docs/4-implementation/k8s/node.md
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
# 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 nothing applied.
|
||||
|
||||
## 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.
|
||||
- **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`.
|
||||
- **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.1`, are
|
||||
trusted, so the node reaches its pods.
|
||||
|
||||
## What it leaves alone
|
||||
|
||||
The cluster starts with no hyperhive workload. Each service keeps running as
|
||||
its systemd unit, and nothing applies the manifests: two copies of
|
||||
`swarm-notify-router`, `swarm-ci-metrics` or `swarm-controller` would share one
|
||||
queue consumer, one cursor or one swarm. A service's objects go into the
|
||||
cluster when that service moves in (see [`swarmctl migrate`](migrate.md)).
|
||||
|
||||
With the option off, the host's configuration is the one it has without the
|
||||
module.
|
||||
|
||||
## Validation
|
||||
|
||||
`module-eval-k8s` compares an all-local host with the option on against the
|
||||
same host with it off. The off host has no k3s, no trusted interface and no
|
||||
image list; the on host adds the k3s unit and nothing else. The VM test
|
||||
`k8s-node` boots a node through the module, and asserts that the node is Ready,
|
||||
that containerd holds every image, that the cluster has no hyperhive
|
||||
workload, and that the node and the hive bridge hold addresses from
|
||||
different ranges. The module-eval also checks that overlapping ranges fail the
|
||||
evaluation.
|
||||
|
|
@ -17,8 +17,12 @@ let
|
|||
# so the YAML carries no string context: building it builds no image and
|
||||
# no daemon.
|
||||
withoutContext = v: builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.toJSON v));
|
||||
|
||||
rendered = import ./render.nix { inherit lib pkgs config; };
|
||||
in
|
||||
{
|
||||
imports = [ ./node.nix ];
|
||||
|
||||
options.services.hyperhive.k8s = {
|
||||
enable = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
|
|
@ -27,7 +31,8 @@ in
|
|||
Render Kubernetes manifests for the in-cluster swarm services this
|
||||
host deploys (swarm-controller, swarm-notify-router and
|
||||
swarm-ci-metrics) into `system.build.k8sManifests`, one YAML file per
|
||||
object. The manifests reference their Secrets by name only.
|
||||
object, and the images their pods run into `system.build.k8sImages`.
|
||||
The manifests reference their Secrets by name only.
|
||||
|
||||
Rendering only: the host's units stay as they are, and nothing
|
||||
applies the manifests to a cluster.
|
||||
|
|
@ -42,11 +47,14 @@ in
|
|||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
# The images those pods run, for a node to load: ./node.nix.
|
||||
system.build.k8sImages = rendered.images;
|
||||
|
||||
system.build.k8sManifests = pkgs.linkFarm "hyperhive-k8s-manifests" (
|
||||
lib.mapAttrsToList (path: object: {
|
||||
name = "${path}.yaml";
|
||||
path = yaml.generate "${baseNameOf path}.yaml" (withoutContext object);
|
||||
}) (import ./render.nix { inherit lib pkgs config; })
|
||||
}) rendered.objects
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
153
nix/host-modules/k8s/node.nix
Normal file
153
nix/host-modules/k8s/node.nix
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
# The single-node k3s cluster this host runs for the swarm services that run
|
||||
# in the cluster, the Kubernetes counterpart of `deploy.singleHostSwarm`'s
|
||||
# all-local swarm. docs/4-implementation/k8s/node.md has the design.
|
||||
#
|
||||
# The node holds the images of the services ./render.nix renders and has no
|
||||
# registry. Nothing is applied to the cluster: a service's host unit keeps
|
||||
# running, and its objects go in when the service moves in.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hyperhive.deploy.k8s;
|
||||
networkCfg = config.services.hyperhive.network;
|
||||
|
||||
# IPv4 CIDR arithmetic on nix's 64-bit integers.
|
||||
ipToInt =
|
||||
ip:
|
||||
builtins.foldl' (acc: x: acc * 256 + x) 0 (
|
||||
map lib.strings.toIntBase10 (lib.strings.splitString "." ip)
|
||||
);
|
||||
pow2 = n: if n == 0 then 1 else 2 * (pow2 (n - 1));
|
||||
parseCidr =
|
||||
cidr:
|
||||
let
|
||||
parts = lib.strings.splitString "/" cidr;
|
||||
in
|
||||
{
|
||||
base = ipToInt (lib.head parts);
|
||||
len = lib.strings.toIntBase10 (lib.last parts);
|
||||
};
|
||||
# Two ranges overlap when they agree on the leading bits of the shorter one.
|
||||
overlap =
|
||||
a: b:
|
||||
let
|
||||
pa = parseCidr a;
|
||||
pb = parseCidr b;
|
||||
keep = 4294967296 - pow2 (32 - lib.min pa.len pb.len);
|
||||
in
|
||||
builtins.bitAnd pa.base keep == builtins.bitAnd pb.base keep;
|
||||
|
||||
cidrType = lib.types.strMatching "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}";
|
||||
in
|
||||
{
|
||||
options.services.hyperhive.deploy.k8s = {
|
||||
enable = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = ''
|
||||
Run a single-node k3s server on this host, with the images of the
|
||||
swarm services that run in the cluster loaded into it, and render
|
||||
their manifests (`services.hyperhive.k8s.enable`). Set it on the host
|
||||
that runs the swarm's services, which is the one
|
||||
`services.hyperhive.deploy.singleHostSwarm` names.
|
||||
|
||||
The cluster starts with no hyperhive workload: the services keep
|
||||
running as systemd units. Its secrets are encrypted at rest, and
|
||||
traefik and metrics-server stay off. Pods and services take their
|
||||
addresses from {option}`services.hyperhive.deploy.k8s.podCidr` and
|
||||
{option}`services.hyperhive.deploy.k8s.serviceCidr`. The firewall
|
||||
trusts the pod network's interfaces (`cni0`, `flannel.1`), so the node
|
||||
reaches its pods.
|
||||
'';
|
||||
};
|
||||
|
||||
podCidr = lib.mkOption {
|
||||
type = cidrType;
|
||||
default = "10.200.0.0/16";
|
||||
description = ''
|
||||
IPv4 range k3s hands pod addresses out of (`--cluster-cidr`). It must
|
||||
overlap neither the hive bridge's subnet
|
||||
({option}`services.hyperhive.network.bridgeIp`) nor
|
||||
{option}`services.hyperhive.deploy.k8s.serviceCidr`, nor any network
|
||||
the host routes to. k3s's own default, `10.42.0.0/16`, contains the
|
||||
hive bridge's default subnet.
|
||||
'';
|
||||
};
|
||||
|
||||
serviceCidr = lib.mkOption {
|
||||
type = cidrType;
|
||||
default = "10.201.0.0/16";
|
||||
description = ''
|
||||
IPv4 range k3s hands service cluster IPs out of (`--service-cidr`);
|
||||
the cluster DNS address is the tenth address of it. The same
|
||||
constraints as {option}`services.hyperhive.deploy.k8s.podCidr` apply.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
assertions = [
|
||||
{
|
||||
assertion = config.services.hyperhive.k8s.enable;
|
||||
message = ''
|
||||
services.hyperhive.deploy.k8s.enable loads the images
|
||||
services.hyperhive.k8s.enable renders into the node, so it needs
|
||||
services.hyperhive.k8s.enable to be true.
|
||||
'';
|
||||
}
|
||||
{
|
||||
assertion =
|
||||
!networkCfg.enable
|
||||
|| !(lib.any (overlap networkCfg.networkCidr) [
|
||||
cfg.podCidr
|
||||
cfg.serviceCidr
|
||||
]);
|
||||
message = ''
|
||||
services.hyperhive.deploy.k8s.podCidr (${cfg.podCidr}) and
|
||||
services.hyperhive.deploy.k8s.serviceCidr (${cfg.serviceCidr}) must
|
||||
not overlap the hive bridge's subnet (${networkCfg.networkCidr}, from
|
||||
services.hyperhive.network.bridgeIp and bridgePrefixLength): the
|
||||
bridge and the cluster would route the same addresses.
|
||||
'';
|
||||
}
|
||||
{
|
||||
assertion = !(overlap cfg.podCidr cfg.serviceCidr);
|
||||
message = ''
|
||||
services.hyperhive.deploy.k8s.podCidr (${cfg.podCidr}) and
|
||||
services.hyperhive.deploy.k8s.serviceCidr (${cfg.serviceCidr}) must
|
||||
not overlap each other.
|
||||
'';
|
||||
}
|
||||
];
|
||||
|
||||
services.hyperhive.k8s.enable = lib.mkDefault true;
|
||||
|
||||
services.k3s = {
|
||||
enable = true;
|
||||
role = "server";
|
||||
disable = [
|
||||
"traefik"
|
||||
"metrics-server"
|
||||
];
|
||||
extraFlags = [
|
||||
"--secrets-encryption"
|
||||
"--cluster-cidr=${cfg.podCidr}"
|
||||
"--service-cidr=${cfg.serviceCidr}"
|
||||
];
|
||||
# k3s's own images come first: with no registry the node pulls nothing,
|
||||
# not even CoreDNS.
|
||||
images = [
|
||||
config.services.k3s.package.airgap-images
|
||||
]
|
||||
++ (config.system.build.k8sImages or [ ]);
|
||||
};
|
||||
|
||||
networking.firewall.trustedInterfaces = [
|
||||
"cni0"
|
||||
"flannel.1"
|
||||
];
|
||||
};
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
# The Kubernetes objects that run each in-cluster swarm service this host
|
||||
# deploys, read from that service's systemd unit as this host renders it.
|
||||
# Imported by ./default.nix, which writes them out as YAML.
|
||||
# Imported by ./default.nix, which writes the objects out as YAML. Returns
|
||||
# `objects` and the `images` the pods run.
|
||||
#
|
||||
# The pod takes the unit's environment, credential ids, state and runtime
|
||||
# directories, and timer. Every file the unit names is mounted at the path the
|
||||
|
|
@ -375,9 +376,12 @@ let
|
|||
else if missingCreds != { } then
|
||||
fail name "${lib.concatStringsSep ", " (lib.attrNames missingCreds)} names a credential the unit does not load"
|
||||
else
|
||||
objects;
|
||||
{ inherit objects image; };
|
||||
|
||||
enabled = lib.mapAttrs workload (lib.filterAttrs (_: svc: svc.enable) services);
|
||||
in
|
||||
{
|
||||
objects = {
|
||||
"namespace" = {
|
||||
apiVersion = "v1";
|
||||
kind = "Namespace";
|
||||
|
|
@ -387,4 +391,9 @@ in
|
|||
};
|
||||
};
|
||||
}
|
||||
// lib.concatMapAttrs (name: svc: lib.optionalAttrs svc.enable (workload name svc)) services
|
||||
// lib.concatMapAttrs (_: w: w.objects) enabled;
|
||||
|
||||
# The image each enabled service's pod runs: what a node has to hold,
|
||||
# since there is no registry to pull from.
|
||||
images = lib.mapAttrsToList (_: w: w.image) enabled;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
# `checks.module-eval-k8s` — see ./lib.nix for the shared rationale. The k8s
|
||||
# manifests ../host-modules/k8s/render.nix builds from the single-host swarm's
|
||||
# units: each workload carries its unit's environment, no credential source
|
||||
# reaches the world-readable YAML, and the CronJob follows the timer.
|
||||
# reaches the world-readable YAML, and the CronJob follows the timer. And the
|
||||
# k3s node ../host-modules/k8s/node.nix adds to that host: present with
|
||||
# `deploy.k8s.enable`, absent and changing nothing without it.
|
||||
#
|
||||
# Every fixture is a shared one, so this cluster evaluates no host of its own.
|
||||
# The all-local host with the node on is this cluster's own fixture; the
|
||||
# rest are shared ones.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -22,15 +25,44 @@ let
|
|||
;
|
||||
})
|
||||
runGroup
|
||||
hive
|
||||
hiveWithController
|
||||
;
|
||||
|
||||
allLocal = shared.allLocal;
|
||||
|
||||
# The four auth responder packages pinned to four distinct derivations, as
|
||||
# in ./shared.nix: the all-local host needs them set.
|
||||
marker = name: pkgs.runCommand "auth-marker-${name}" { } "mkdir $out";
|
||||
|
||||
# The all-local host with the node on. It does not set
|
||||
# `services.hyperhive.k8s.enable`: the node turns the rendering on.
|
||||
withNode = hiveWithController {
|
||||
deploy.singleHostSwarm = true;
|
||||
deploy.k8s.enable = true;
|
||||
deploy.victorialogs.authPackage = marker "logs";
|
||||
deploy.victorialogs.grafanaAuthPackage = marker "logs-grafana";
|
||||
deploy.victoriametrics.authPackage = marker "metrics";
|
||||
deploy.victoriametrics.agentAuthPackage = marker "metrics-agent";
|
||||
};
|
||||
|
||||
# A node every assertion refuses: no rendering, a pod range containing the
|
||||
# hive bridge's subnet, and a service range inside the pod range.
|
||||
nodeRefused = hive {
|
||||
deploy.k8s.enable = true;
|
||||
k8s.enable = false;
|
||||
network.enable = true;
|
||||
deploy.k8s.podCidr = "10.42.0.0/16";
|
||||
deploy.k8s.serviceCidr = "10.42.128.0/17";
|
||||
};
|
||||
refusals = lib.filter (a: !a.assertion) nodeRefused.assertions;
|
||||
refuses = needle: lib.any (a: lib.hasInfix needle a.message) refusals;
|
||||
|
||||
render =
|
||||
config:
|
||||
import ../host-modules/k8s/render.nix {
|
||||
(import ../host-modules/k8s/render.nix {
|
||||
inherit lib pkgs config;
|
||||
};
|
||||
}).objects;
|
||||
objects = render allLocal;
|
||||
|
||||
units = {
|
||||
|
|
@ -147,6 +179,89 @@ let
|
|||
name = "a unit variable naming a path the pod does not mount fails the render";
|
||||
ok = renders allLocal && !(renders withStrayPath);
|
||||
}
|
||||
{
|
||||
# The host without the node is the control that the lookups find what
|
||||
# is there: the same paths on the host with it on.
|
||||
name = "the node exists only where deploy.k8s.enable is set";
|
||||
ok =
|
||||
!allLocal.services.k3s.enable
|
||||
&& !(allLocal.systemd.services ? k3s)
|
||||
&& allLocal.services.k3s.images == [ ]
|
||||
&& !(lib.elem "cni0" allLocal.networking.firewall.trustedInterfaces)
|
||||
&& withNode.services.k3s.enable
|
||||
&& withNode.services.k3s.role == "server"
|
||||
&& withNode.systemd.services ? k3s
|
||||
&& lib.elem "cni0" withNode.networking.firewall.trustedInterfaces
|
||||
&& lib.elem "flannel.1" withNode.networking.firewall.trustedInterfaces;
|
||||
}
|
||||
{
|
||||
name = "the node runs k3s with traefik and metrics-server off and its secrets encrypted";
|
||||
ok =
|
||||
let
|
||||
start = withNode.systemd.services.k3s.serviceConfig.ExecStart;
|
||||
in
|
||||
lib.hasInfix "--disable=traefik" start
|
||||
&& lib.hasInfix "--disable=metrics-server" start
|
||||
&& lib.hasInfix "--secrets-encryption" start
|
||||
&& lib.hasInfix "--cluster-cidr=10.200.0.0/16" start
|
||||
&& lib.hasInfix "--service-cidr=10.201.0.0/16" start
|
||||
&& !(lib.hasInfix "--disable=coredns" start)
|
||||
&& !(lib.hasInfix "--disable=local-storage" start);
|
||||
}
|
||||
{
|
||||
# The images are the ones the manifests run, one per rendered service:
|
||||
# the three workloads are the control that the list is not empty.
|
||||
name = "the node loads the image of every service the manifests run, and k3s's own";
|
||||
ok =
|
||||
let
|
||||
loaded = map toString withNode.services.k3s.images;
|
||||
rendered = map toString withNode.system.build.k8sImages;
|
||||
in
|
||||
lib.length rendered == 3
|
||||
&& lib.all (i: lib.elem i loaded) rendered
|
||||
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
|
||||
&& lib.length loaded == 4;
|
||||
}
|
||||
{
|
||||
# Nothing the host already runs differs: every unit, timer and
|
||||
# container is still there, the only new units are k3s's, every unit but
|
||||
# the firewall (whose rules carry the trusted interfaces) starts the same
|
||||
# command, and the manifests are the same derivation as without the node.
|
||||
name = "the node changes no unit, container, timer or manifest of the all-local host";
|
||||
ok =
|
||||
let
|
||||
names = a: lib.attrNames a;
|
||||
added = lib.subtractLists (names allLocal.systemd.services) (names withNode.systemd.services);
|
||||
in
|
||||
names allLocal.systemd.timers == names withNode.systemd.timers
|
||||
&& names allLocal.containers == names withNode.containers
|
||||
&& lib.subtractLists (names withNode.systemd.services) (names allLocal.systemd.services) == [ ]
|
||||
&& lib.elem "k3s" added
|
||||
&& lib.all (n: lib.hasPrefix "k3s" n) added
|
||||
&& lib.all (
|
||||
n:
|
||||
withNode.systemd.services.${n}.serviceConfig.ExecStart or null
|
||||
== allLocal.systemd.services.${n}.serviceConfig.ExecStart or null
|
||||
) (lib.remove "firewall" (names allLocal.systemd.services))
|
||||
&& withNode.system.build.k8sManifests.drvPath == allLocal.system.build.k8sManifests.drvPath;
|
||||
}
|
||||
{
|
||||
# The all-local host's assertions are the control: they all hold.
|
||||
name = "the node refuses a host that does not render the manifests it loads";
|
||||
ok = lib.all (a: a.assertion) withNode.assertions && refuses "deploy.k8s.enable loads the images";
|
||||
}
|
||||
{
|
||||
# The default ranges on the all-local host, whose bridge is up, are the
|
||||
# control: the same assertions hold there, and the flags carry the ranges.
|
||||
name = "the node refuses pod and service ranges that overlap the hive bridge or each other";
|
||||
ok =
|
||||
withNode.services.hyperhive.network.enable
|
||||
&& withNode.services.hyperhive.network.networkCidr == "10.42.0.0/24"
|
||||
&& withNode.services.hyperhive.deploy.k8s.podCidr == "10.200.0.0/16"
|
||||
&& withNode.services.hyperhive.deploy.k8s.serviceCidr == "10.201.0.0/16"
|
||||
&& refuses "overlap the hive bridge"
|
||||
&& refuses "overlap each other";
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "k8s" cases
|
||||
|
|
|
|||
Loading…
Reference in a new issue