Add keepalived to cluster

Add keepalived for the Kubernetes cluster
Set VIP to 10.42.5.1
Set *.cluster DNS resolution to this IP address
But do not move existing *.kubernetes targets, yet, as there is a need
to ensure that subnet routers are properly configured
This commit is contained in:
Greg Hellings
2025-07-02 23:07:08 -05:00
parent 6c4cc2cd30
commit 9940e89d2e
5 changed files with 74 additions and 18 deletions
+56 -15
View File
@@ -16,6 +16,16 @@ in
default = false;
description = "Whether to run the Kubernetes agent only";
};
priority = lib.mkOption {
type = lib.types.int;
default = 1;
description = "VIP priority";
};
vipInterface = lib.mkOption {
type = lib.types.str;
default = null;
description = "Interface to attach VIP for clustering onto";
};
};
};
@@ -55,21 +65,52 @@ in
allowedUDPPorts = [ 8472 ];
};
services.k3s = {
enable = true;
role = if cfg.agentOnly then "agent" else "server";
tokenFile = config.age.secrets.kubernetesToken.path;
extraFlags = [
"--cluster-cidr=10.211.0.0/16"
"--service-cidr=10.221.0.0/16"
"--write-kubeconfig-mode 0640"
"--write-kubeconfig-group kubeconfig"
"--resolv-conf=/etc/resolv.conf"
"--tls-san ${config.networking.hostName}.home"
"--tls-san ${config.networking.hostName}.thehellings.lan"
"--tls-san ${config.networking.hostName}.shire-zebra.ts.net"
];
serverAddr = lib.mkIf (config.networking.hostName != "isaiah") "https://isaiah.home:6443";
services = {
k3s = {
enable = true;
role = if cfg.agentOnly then "agent" else "server";
tokenFile = config.age.secrets.kubernetesToken.path;
extraFlags = [
"--cluster-cidr=10.211.0.0/16"
"--service-cidr=10.221.0.0/16"
"--write-kubeconfig-mode 0640"
"--write-kubeconfig-group kubeconfig"
"--resolv-conf=/etc/resolv.conf"
"--tls-san ${config.networking.hostName}.home"
"--tls-san ${config.networking.hostName}.thehellings.lan"
"--tls-san ${config.networking.hostName}.shire-zebra.ts.net"
];
serverAddr = lib.mkIf (config.networking.hostName != "isaiah") "https://isaiah.home:6443";
};
keepalived =
let
ip = (builtins.head config.networking.interfaces."${cfg.vipInterface}".ipv4.addresses).address;
in
{
enable = true;
openFirewall = true;
vrrpInstances.kubernetes = {
interface = cfg.vipInterface;
priority = cfg.priority;
state = if (config.networking.hostName == "isaiah") then "MASTER" else "BACKUP";
virtualIps = [
{
addr = "10.42.5.1/16";
dev = cfg.vipInterface;
}
];
virtualRouterId = 77;
unicastPeers = lib.filter (v: v != ip) [
"10.42.1.6"
"10.42.1.8"
"10.42.1.13"
];
unicastSrcIp = ip;
extraConfig = ''
advert_int 1
'';
};
};
};
users = {