Files
nixos-setup/modules/nixos/server/k8s/default.nix
T
2026-07-01 18:07:52 -04:00

80 lines
2.1 KiB
Nix

{ config, pkgs, lib, ... }:
let
cfg = config.skyg.nixos.server.k8s;
in
{
imports = [
./master.nix
./node.nix
];
options = {
skyg.nixos.server.k8s = {
enable = lib.mkEnableOption
"Enable K8s";
isMaster = lib.mkEnableOption
"Enable as master";
isNode = lib.mkEnableOption
"Enable as node";
masterIP = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "10.0.0.1";
};
masterHostName = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "k8s-master";
};
masterAPIPort = lib.mkOption {
type = lib.types.number;
default = 6443;
};
};
};
config =
let
ports = [
cfg.masterAPIPort # k8s api server
10250 # kubelet api
10259 # kubelet scheduler
10257 # kubelet controller
];
portRanges = [
{ from = 2379; to = 2380; } # etcd
];
in
lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.masterIP != null;
message = "skyg.nixos.server.k8s: masterIP must be set when k8s is enabled";
}
{
assertion = cfg.masterHostName != null;
message = "skyg.nixos.server.k8s: masterHostName must be set when k8s is enabled";
}
];
# resolve master hostname
networking.extraHosts = "${cfg.masterIP} ${cfg.masterHostName}";
networking.firewall.allowedUDPPorts = ports;
networking.firewall.allowedUDPPortRanges = portRanges;
networking.firewall.allowedTCPPorts = ports;
networking.firewall.allowedTCPPortRanges = portRanges;
# packages for administration tasks
environment.systemPackages = with pkgs; [
kubectl
kubernetes
];
services.kubernetes = {
proxy.hostname = cfg.masterHostName;
masterAddress = cfg.masterHostName;
easyCerts = true;
apiserverAddress = "https://${cfg.masterHostName}:${toString cfg.masterAPIPort}";
addons.dns.enable = true;
kubelet.extraOpts = "--fail-swap-on=false";
};
};
}