2024-07-12 08:08:35 +00:00
|
|
|
{ self, inputs, pkgs, lib, config, ... }:
|
2024-06-24 21:31:06 +00:00
|
|
|
let
|
|
|
|
cfg = config.lab.k3s;
|
|
|
|
|
|
|
|
k3s-cni-plugins = pkgs.buildEnv {
|
|
|
|
name = "k3s-cni-plugins";
|
|
|
|
paths = with pkgs; [
|
|
|
|
cni-plugins
|
|
|
|
cni-plugin-flannel
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
image = pkgs.nix-snapshotter.buildImage {
|
|
|
|
name = "redis";
|
|
|
|
resolvedByNix = true;
|
|
|
|
config = {
|
|
|
|
entrypoint = [ "${pkgs.redis}/bin/redis-server" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2024-04-15 21:28:23 +00:00
|
|
|
options.lab.k3s = {
|
|
|
|
enable = lib.mkOption {
|
|
|
|
default = false;
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to run k3s on this server.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-05-09 15:03:13 +00:00
|
|
|
role = lib.mkOption {
|
|
|
|
default = "server";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
Whether to run k3s as a server or an agent.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
clusterInit = lib.mkOption {
|
|
|
|
default = false;
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether this node should initialize the K8s cluster.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-15 21:28:23 +00:00
|
|
|
serverAddr = lib.mkOption {
|
|
|
|
default = null;
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
description = ''
|
|
|
|
Address of the server whose cluster this server should join.
|
|
|
|
Leaving this empty will make the server initialize the cluster.
|
|
|
|
'';
|
|
|
|
};
|
2024-03-19 21:56:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2024-05-18 21:32:58 +00:00
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
k3s
|
|
|
|
openiscsi # Required for Longhorn
|
|
|
|
nfs-utils # Required for Longhorn
|
|
|
|
];
|
2024-03-19 21:56:52 +00:00
|
|
|
|
|
|
|
networking = {
|
|
|
|
nftables.enable = lib.mkForce false;
|
|
|
|
firewall.enable = lib.mkForce false;
|
|
|
|
};
|
|
|
|
|
2024-06-24 21:31:06 +00:00
|
|
|
virtualisation.containerd = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
settings = {
|
|
|
|
version = 2;
|
|
|
|
|
|
|
|
proxy_plugins.nix = {
|
|
|
|
type = "snapshot";
|
|
|
|
address = "/run/nix-snapshotter/nix-snapshotter.sock";
|
|
|
|
};
|
|
|
|
|
|
|
|
plugins = {
|
|
|
|
"io.containerd.grpc.v1.cri" = {
|
|
|
|
stream_server_address = "127.0.0.1";
|
|
|
|
stream_server_port = "10010";
|
|
|
|
enable_selinux = false;
|
|
|
|
enable_unprivileged_ports = true;
|
|
|
|
enable_unprivileged_icmp = true;
|
|
|
|
disable_apparmor = true;
|
|
|
|
disable_cgroup = true;
|
|
|
|
restrict_oom_score_adj = true;
|
|
|
|
sandbox_image = "rancher/mirrored-pause:3.6";
|
|
|
|
containerd.snapshotter = "nix";
|
|
|
|
|
|
|
|
cni = {
|
|
|
|
conf_dir = "/var/lib/rancher/k3s/agent/etc/cni/net.d/";
|
|
|
|
bin_dir = "${k3s-cni-plugins}/bin";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
"io.containerd.transfer.v1.local".unpack_config = [{
|
|
|
|
platform = "linux/amd64";
|
|
|
|
snapshotter = "nix";
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-18 21:32:58 +00:00
|
|
|
services = {
|
2024-06-24 21:31:06 +00:00
|
|
|
nix-snapshotter.enable = true;
|
|
|
|
|
2024-05-18 21:32:58 +00:00
|
|
|
k3s =
|
|
|
|
let
|
2024-06-24 21:31:06 +00:00
|
|
|
serverFlagList = [
|
|
|
|
"--image-service-endpoint=unix:///run/nix-snapshotter/nix-snapshotter.sock"
|
|
|
|
"--snapshotter=overlayfs"
|
|
|
|
"--container-runtime-endpoint=unix:///run/containerd/containerd.sock"
|
|
|
|
"--tls-san=${config.networking.fqdn}"
|
|
|
|
"--disable=servicelb"
|
|
|
|
"--cluster-cidr=10.42.0.0/16,2001:cafe:42::/56"
|
|
|
|
"--service-cidr=10.43.0.0/16,2001:cafe:43::/112"
|
|
|
|
];
|
|
|
|
|
|
|
|
serverFlags = builtins.concatStringsSep " " serverFlagList;
|
2024-05-18 21:32:58 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
enable = true;
|
|
|
|
role = cfg.role;
|
2024-06-15 20:27:07 +00:00
|
|
|
tokenFile = config.sops.secrets."k3s/serverToken".path;
|
2024-06-24 21:31:06 +00:00
|
|
|
extraFlags = lib.mkIf (cfg.role == "server") (lib.mkForce serverFlags);
|
2024-05-18 21:32:58 +00:00
|
|
|
clusterInit = cfg.clusterInit;
|
|
|
|
serverAddr = lib.mkIf (! (cfg.serverAddr == null)) cfg.serverAddr;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Required for Longhorn
|
|
|
|
openiscsi = {
|
2024-05-09 15:03:13 +00:00
|
|
|
enable = true;
|
2024-05-18 21:32:58 +00:00
|
|
|
name = "iqn.2016-04.com.open-iscsi:${config.networking.fqdn}";
|
2024-05-09 15:03:13 +00:00
|
|
|
};
|
2024-05-18 21:32:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# HACK: Symlink binaries to /usr/local/bin such that Longhorn can find them
|
|
|
|
# when they use nsenter.
|
|
|
|
# https://github.com/longhorn/longhorn/issues/2166#issuecomment-1740179416
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"L+ /usr/local/bin - - - - /run/current-system/sw/bin/"
|
|
|
|
];
|
2024-05-09 15:03:13 +00:00
|
|
|
|
|
|
|
system = lib.mkIf (cfg.role == "server") {
|
|
|
|
activationScripts = {
|
|
|
|
k3s-bootstrap.text = (
|
|
|
|
let
|
2024-06-13 17:46:21 +00:00
|
|
|
k3sBootstrapFile = (inputs.kubenix.evalModules.x86_64-linux {
|
2024-05-09 15:03:13 +00:00
|
|
|
module = import ./bootstrap.nix;
|
|
|
|
}).config.kubernetes.result;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
mkdir -p /var/lib/rancher/k3s/server/manifests
|
|
|
|
ln -sf ${k3sBootstrapFile} /var/lib/rancher/k3s/server/manifests/k3s-bootstrap.json
|
|
|
|
''
|
|
|
|
);
|
2024-03-19 21:56:52 +00:00
|
|
|
|
2024-05-09 15:03:13 +00:00
|
|
|
k3s-certs.text = ''
|
|
|
|
mkdir -p /var/lib/rancher/k3s/server/tls/etcd
|
2024-05-18 21:32:58 +00:00
|
|
|
cp -f ${./k3s-ca/server-ca.crt} /var/lib/rancher/k3s/server/tls/server-ca.crt
|
|
|
|
cp -f ${./k3s-ca/client-ca.crt} /var/lib/rancher/k3s/server/tls/client-ca.crt
|
|
|
|
cp -f ${./k3s-ca/request-header-ca.crt} /var/lib/rancher/k3s/server/tls/request-header-ca.crt
|
|
|
|
cp -f ${./k3s-ca/etcd/peer-ca.crt} /var/lib/rancher/k3s/server/tls/etcd/peer-ca.crt
|
|
|
|
cp -f ${./k3s-ca/etcd/server-ca.crt} /var/lib/rancher/k3s/server/tls/etcd/server-ca.crt
|
2024-05-09 15:03:13 +00:00
|
|
|
'';
|
2024-06-24 21:31:06 +00:00
|
|
|
|
2024-07-12 08:08:35 +00:00
|
|
|
docker-images =
|
|
|
|
let
|
|
|
|
pulledImages = (import "${self}/container-images/pulled-images.nix") pkgs lib;
|
|
|
|
basePath = "/var/docker_images";
|
|
|
|
linesForImage = projectName: imageName: pulledImage:
|
|
|
|
let
|
|
|
|
projectPath = "${basePath}/${projectName}";
|
|
|
|
in
|
|
|
|
''
|
|
|
|
mkdir -p ${projectPath}
|
|
|
|
ln -sf ${pulledImage} ${projectPath}/${imageName}.tar
|
|
|
|
'';
|
|
|
|
linesForProject = projectName: project:
|
|
|
|
let
|
|
|
|
lines = lib.attrsets.mapAttrsToList (linesForImage projectName) project;
|
|
|
|
in
|
|
|
|
builtins.concatStringsSep "\n" lines;
|
|
|
|
generateLines = projects:
|
|
|
|
let
|
|
|
|
lines = lib.attrsets.mapAttrsToList linesForProject projects;
|
|
|
|
in
|
|
|
|
builtins.concatStringsSep "\n" lines;
|
|
|
|
in
|
|
|
|
generateLines pulledImages;
|
2024-05-09 15:03:13 +00:00
|
|
|
};
|
2024-04-15 21:28:23 +00:00
|
|
|
};
|
|
|
|
|
2024-06-15 20:27:07 +00:00
|
|
|
sops.secrets =
|
|
|
|
let
|
|
|
|
keyPathBase = "/var/lib/rancher/k3s/server/tls";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"k3s/serverToken" = { };
|
|
|
|
"k3s/keys/clientCAKey".path = "${keyPathBase}/client-ca.key";
|
|
|
|
"k3s/keys/requestHeaderCAKey".path = "${keyPathBase}/request-header-ca.key";
|
|
|
|
"k3s/keys/serverCAKey".path = "${keyPathBase}/server-ca.key";
|
|
|
|
"k3s/keys/serviceKey".path = "${keyPathBase}/service.key";
|
|
|
|
"k3s/keys/etcd/peerCAKey".path = "${keyPathBase}/etcd/peer-ca.key";
|
|
|
|
"k3s/keys/etcd/serverCAKey".path = "${keyPathBase}/etcd/server-ca.key";
|
2024-05-09 15:03:13 +00:00
|
|
|
};
|
2024-03-19 21:56:52 +00:00
|
|
|
};
|
|
|
|
}
|