2026-02-28 16:04:14 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2026-02-28 14:16:55 +00:00
|
|
|
|
2026-02-28 16:04:14 +00:00
|
|
|
let
|
|
|
|
|
pinnedK8s = lib.attrByPath [ "kubernetes_1_31" ] pkgs.kubernetes pkgs;
|
|
|
|
|
kubeVipImage = "ghcr.io/kube-vip/kube-vip:v0.8.9";
|
|
|
|
|
in
|
2026-02-28 14:16:55 +00:00
|
|
|
{
|
2026-02-28 16:04:14 +00:00
|
|
|
options.terrahome.kubeadm = {
|
|
|
|
|
k8sMinor = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = "1.31";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
controlPlaneInterface = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = "eth0";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
controlPlaneVipSuffix = lib.mkOption {
|
|
|
|
|
type = lib.types.int;
|
|
|
|
|
default = 250;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
podSubnet = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = "10.244.0.0/16";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
serviceSubnet = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = "10.96.0.0/12";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clusterDomain = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
default = "cluster.local";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = {
|
2026-02-28 14:16:55 +00:00
|
|
|
boot.kernelModules = [ "overlay" "br_netfilter" ];
|
|
|
|
|
|
|
|
|
|
boot.kernel.sysctl = {
|
|
|
|
|
"net.ipv4.ip_forward" = 1;
|
|
|
|
|
"net.bridge.bridge-nf-call-iptables" = 1;
|
|
|
|
|
"net.bridge.bridge-nf-call-ip6tables" = 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualisation.containerd.enable = true;
|
2026-02-28 16:04:14 +00:00
|
|
|
virtualisation.containerd.settings = {
|
|
|
|
|
plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options.SystemdCgroup = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
swapDevices = lib.mkForce [ ];
|
2026-02-28 14:16:55 +00:00
|
|
|
|
|
|
|
|
services.openssh.enable = true;
|
|
|
|
|
services.openssh.settings = {
|
|
|
|
|
PasswordAuthentication = false;
|
|
|
|
|
KbdInteractiveAuthentication = false;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-01 13:31:36 +00:00
|
|
|
users.users.micqdf = {
|
|
|
|
|
isNormalUser = true;
|
|
|
|
|
extraGroups = [ "wheel" ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
security.sudo.wheelNeedsPassword = false;
|
|
|
|
|
|
2026-02-28 20:03:26 +00:00
|
|
|
nix.settings.trusted-users = [ "root" "micqdf" ];
|
2026-02-28 21:24:16 +00:00
|
|
|
nix.gc = {
|
|
|
|
|
automatic = true;
|
|
|
|
|
dates = "daily";
|
|
|
|
|
options = "--delete-older-than 3d";
|
|
|
|
|
};
|
|
|
|
|
nix.settings.auto-optimise-store = true;
|
2026-02-28 20:03:26 +00:00
|
|
|
|
2026-02-28 16:04:14 +00:00
|
|
|
environment.variables = {
|
|
|
|
|
KUBECONFIG = "/etc/kubernetes/admin.conf";
|
|
|
|
|
KUBE_VIP_IMAGE = kubeVipImage;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
environment.systemPackages = (with pkgs; [
|
2026-02-28 14:16:55 +00:00
|
|
|
containerd
|
|
|
|
|
cri-tools
|
|
|
|
|
cni-plugins
|
2026-02-28 16:04:14 +00:00
|
|
|
pinnedK8s
|
2026-02-28 14:16:55 +00:00
|
|
|
kubernetes-helm
|
|
|
|
|
conntrack-tools
|
|
|
|
|
socat
|
|
|
|
|
ethtool
|
|
|
|
|
ipvsadm
|
2026-02-28 16:04:14 +00:00
|
|
|
iproute2
|
|
|
|
|
iptables
|
|
|
|
|
ebtables
|
2026-02-28 14:16:55 +00:00
|
|
|
jq
|
|
|
|
|
curl
|
|
|
|
|
vim
|
2026-02-28 16:04:14 +00:00
|
|
|
gawk
|
|
|
|
|
]) ++ [
|
|
|
|
|
(pkgs.writeShellScriptBin "th-kubeadm-init" ''
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
iface="${config.terrahome.kubeadm.controlPlaneInterface}"
|
|
|
|
|
suffix="${toString config.terrahome.kubeadm.controlPlaneVipSuffix}"
|
|
|
|
|
pod_subnet="${config.terrahome.kubeadm.podSubnet}"
|
|
|
|
|
service_subnet="${config.terrahome.kubeadm.serviceSubnet}"
|
|
|
|
|
domain="${config.terrahome.kubeadm.clusterDomain}"
|
|
|
|
|
|
|
|
|
|
local_ip_cidr=$(ip -4 -o addr show dev "$iface" | awk 'NR==1 {print $4}')
|
2026-02-28 16:12:25 +00:00
|
|
|
if [ -z "''${local_ip_cidr:-}" ]; then
|
2026-02-28 16:04:14 +00:00
|
|
|
echo "Could not determine IPv4 CIDR on interface $iface"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
subnet_prefix=$(echo "$local_ip_cidr" | cut -d/ -f1 | awk -F. '{print $1"."$2"."$3}')
|
|
|
|
|
vip="$subnet_prefix.$suffix"
|
|
|
|
|
|
|
|
|
|
echo "Using control-plane endpoint: $vip:6443"
|
|
|
|
|
echo "Using kube-vip interface: $iface"
|
|
|
|
|
|
|
|
|
|
mkdir -p /etc/kubernetes/manifests
|
|
|
|
|
ctr image pull "$KUBE_VIP_IMAGE"
|
|
|
|
|
|
|
|
|
|
ctr run --rm --net-host "$KUBE_VIP_IMAGE" kube-vip /kube-vip manifest pod \
|
|
|
|
|
--interface "$iface" \
|
|
|
|
|
--address "$vip" \
|
|
|
|
|
--controlplane \
|
|
|
|
|
--services \
|
|
|
|
|
--arp \
|
|
|
|
|
--leaderElection \
|
|
|
|
|
> /etc/kubernetes/manifests/kube-vip.yaml
|
|
|
|
|
|
|
|
|
|
kubeadm init \
|
|
|
|
|
--control-plane-endpoint "$vip:6443" \
|
|
|
|
|
--upload-certs \
|
|
|
|
|
--pod-network-cidr "$pod_subnet" \
|
|
|
|
|
--service-cidr "$service_subnet" \
|
|
|
|
|
--service-dns-domain "$domain"
|
|
|
|
|
|
|
|
|
|
mkdir -p /root/.kube
|
|
|
|
|
cp /etc/kubernetes/admin.conf /root/.kube/config
|
|
|
|
|
chmod 600 /root/.kube/config
|
|
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
echo "Next: install Cilium, then generate join commands:"
|
|
|
|
|
echo " kubeadm token create --print-join-command"
|
|
|
|
|
echo " kubeadm token create --print-join-command --certificate-key <key>"
|
|
|
|
|
'')
|
|
|
|
|
|
|
|
|
|
(pkgs.writeShellScriptBin "th-kubeadm-join-control-plane" ''
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
if [ "$#" -lt 1 ]; then
|
|
|
|
|
echo "Usage: th-kubeadm-join-control-plane '<kubeadm join ... --control-plane --certificate-key ...>'"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
iface="${config.terrahome.kubeadm.controlPlaneInterface}"
|
|
|
|
|
suffix="${toString config.terrahome.kubeadm.controlPlaneVipSuffix}"
|
|
|
|
|
local_ip_cidr=$(ip -4 -o addr show dev "$iface" | awk 'NR==1 {print $4}')
|
2026-02-28 16:12:25 +00:00
|
|
|
if [ -z "''${local_ip_cidr:-}" ]; then
|
2026-02-28 16:04:14 +00:00
|
|
|
echo "Could not determine IPv4 CIDR on interface $iface"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
subnet_prefix=$(echo "$local_ip_cidr" | cut -d/ -f1 | awk -F. '{print $1"."$2"."$3}')
|
|
|
|
|
vip="$subnet_prefix.$suffix"
|
|
|
|
|
|
|
|
|
|
mkdir -p /etc/kubernetes/manifests
|
|
|
|
|
ctr image pull "$KUBE_VIP_IMAGE"
|
|
|
|
|
ctr run --rm --net-host "$KUBE_VIP_IMAGE" kube-vip /kube-vip manifest pod \
|
|
|
|
|
--interface "$iface" \
|
|
|
|
|
--address "$vip" \
|
|
|
|
|
--controlplane \
|
|
|
|
|
--services \
|
|
|
|
|
--arp \
|
|
|
|
|
--leaderElection \
|
|
|
|
|
> /etc/kubernetes/manifests/kube-vip.yaml
|
|
|
|
|
|
|
|
|
|
eval "$1"
|
|
|
|
|
'')
|
|
|
|
|
|
|
|
|
|
(pkgs.writeShellScriptBin "th-kubeadm-join-worker" ''
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
if [ "$#" -lt 1 ]; then
|
|
|
|
|
echo "Usage: th-kubeadm-join-worker '<kubeadm join ...>'"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
eval "$1"
|
|
|
|
|
'')
|
|
|
|
|
|
|
|
|
|
(pkgs.writeShellScriptBin "th-kubeadm-status" ''
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
systemctl is-active containerd || true
|
|
|
|
|
systemctl is-active kubelet || true
|
|
|
|
|
crictl info >/dev/null && echo "crictl: ok" || echo "crictl: not-ready"
|
|
|
|
|
'')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
systemd.services.kubelet = {
|
|
|
|
|
description = "Kubernetes Kubelet";
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2026-02-28 16:19:37 +00:00
|
|
|
wants = [ "network-online.target" ];
|
2026-02-28 16:04:14 +00:00
|
|
|
after = [ "containerd.service" "network-online.target" ];
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${pinnedK8s}/bin/kubelet";
|
|
|
|
|
Restart = "always";
|
|
|
|
|
RestartSec = "10";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
|
"d /etc/kubernetes 0755 root root -"
|
|
|
|
|
"d /etc/kubernetes/manifests 0755 root root -"
|
2026-02-28 14:16:55 +00:00
|
|
|
];
|
2026-02-28 16:04:14 +00:00
|
|
|
};
|
2026-02-28 14:16:55 +00:00
|
|
|
}
|