2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.kubernetes;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.kubernetes;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
defaultContainerdSettings = {
|
|
|
|
version = 2;
|
|
|
|
root = "/var/lib/containerd";
|
|
|
|
state = "/run/containerd";
|
|
|
|
oom_score = 0;
|
|
|
|
|
|
|
|
grpc = {
|
|
|
|
address = "/run/containerd/containerd.sock";
|
|
|
|
};
|
2021-03-09 03:18:52 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
plugins."io.containerd.grpc.v1.cri" = {
|
|
|
|
sandbox_image = "pause:latest";
|
2021-03-09 03:18:52 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
cni = {
|
|
|
|
bin_dir = "/opt/cni/bin";
|
|
|
|
max_conf_num = 0;
|
|
|
|
};
|
2021-03-09 03:18:52 +00:00
|
|
|
|
2021-09-22 15:38:15 +00:00
|
|
|
containerd.runtimes.runc = {
|
|
|
|
runtime_type = "io.containerd.runc.v2";
|
2022-01-13 20:06:32 +00:00
|
|
|
options.SystemdCgroup = true;
|
2021-09-22 15:38:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-03-09 03:18:52 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
mkKubeConfig = name: conf: pkgs.writeText "${name}-kubeconfig" (builtins.toJSON {
|
|
|
|
apiVersion = "v1";
|
|
|
|
kind = "Config";
|
|
|
|
clusters = [{
|
|
|
|
name = "local";
|
|
|
|
cluster.certificate-authority = conf.caFile or cfg.caFile;
|
|
|
|
cluster.server = conf.server;
|
|
|
|
}];
|
|
|
|
users = [{
|
|
|
|
inherit name;
|
|
|
|
user = {
|
|
|
|
client-certificate = conf.certFile;
|
|
|
|
client-key = conf.keyFile;
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
contexts = [{
|
|
|
|
context = {
|
|
|
|
cluster = "local";
|
|
|
|
user = name;
|
|
|
|
};
|
2021-05-28 09:39:13 +00:00
|
|
|
name = "local";
|
2020-04-24 23:36:52 +00:00
|
|
|
}];
|
2021-05-28 09:39:13 +00:00
|
|
|
current-context = "local";
|
2020-04-24 23:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
caCert = secret "ca";
|
|
|
|
|
|
|
|
etcdEndpoints = ["https://${cfg.masterAddress}:2379"];
|
|
|
|
|
|
|
|
mkCert = { name, CN, hosts ? [], fields ? {}, action ? "",
|
|
|
|
privateKeyOwner ? "kubernetes" }: rec {
|
|
|
|
inherit name caCert CN hosts fields action;
|
|
|
|
cert = secret name;
|
|
|
|
key = secret "${name}-key";
|
|
|
|
privateKeyOptions = {
|
|
|
|
owner = privateKeyOwner;
|
|
|
|
group = "nogroup";
|
|
|
|
mode = "0600";
|
|
|
|
path = key;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
secret = name: "${cfg.secretsPath}/${name}.pem";
|
|
|
|
|
|
|
|
mkKubeConfigOptions = prefix: {
|
|
|
|
server = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "${prefix} kube-apiserver server address.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
caFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "${prefix} certificate authority file used to connect to kube-apiserver.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = cfg.caFile;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${opt.caFile}";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
certFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "${prefix} client certificate file used to connect to kube-apiserver.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
keyFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "${prefix} client key file used to connect to kube-apiserver.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
|
|
|
|
imports = [
|
2021-12-30 13:39:12 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "kubernetes" "addons" "dashboard" ] "Removed due to it being an outdated version")
|
2020-04-24 23:36:52 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "kubernetes" "verbose" ] "")
|
|
|
|
];
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.services.kubernetes = {
|
|
|
|
roles = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Kubernetes role that this machine should take.
|
|
|
|
|
|
|
|
Master role will enable etcd, apiserver, scheduler, controller manager
|
|
|
|
addon manager, flannel and proxy services.
|
|
|
|
Node role will enable flannel, docker, kubelet and proxy services.
|
|
|
|
'';
|
|
|
|
default = [];
|
|
|
|
type = types.listOf (types.enum ["master" "node"]);
|
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "kubernetes" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
kubeconfig = mkKubeConfigOptions "Default kubeconfig";
|
|
|
|
|
|
|
|
apiserverAddress = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Clusterwide accessible address for the kubernetes apiserver,
|
|
|
|
including protocol and optional port.
|
|
|
|
'';
|
|
|
|
example = "https://kubernetes-apiserver.example.com:6443";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
caFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Default kubernetes certificate authority";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes root directory for managing kubelet files.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "/var/lib/kubernetes";
|
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
easyCerts = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Automatically setup x509 certificates and keys for the entire cluster.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
featureGates = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List set of feature gates.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [];
|
|
|
|
type = types.listOf types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
masterAddress = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Clusterwide available network address or hostname for the kubernetes master server.";
|
2020-04-24 23:36:52 +00:00
|
|
|
example = "master.example.com";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Packages added to the services' PATH environment variable. Both the bin and sbin subdirectories of each package are added.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.listOf types.package;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
clusterCidr = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes controller manager and proxy CIDR Range for Pods in cluster.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "10.1.0.0/16";
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
lib = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Common functions for the kubernetes modules.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {
|
|
|
|
inherit mkCert;
|
|
|
|
inherit mkKubeConfig;
|
|
|
|
inherit mkKubeConfigOptions;
|
|
|
|
};
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
secretsPath = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Default location for kubernetes secrets. Not a store location.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = types.path;
|
|
|
|
default = cfg.dataDir + "/secrets";
|
2022-01-07 04:07:37 +00:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
config.${opt.dataDir} + "/secrets"
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
|
|
|
|
(mkIf cfg.easyCerts {
|
|
|
|
services.kubernetes.pki.enable = mkDefault true;
|
|
|
|
services.kubernetes.caFile = caCert;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (elem "master" cfg.roles) {
|
|
|
|
services.kubernetes.apiserver.enable = mkDefault true;
|
|
|
|
services.kubernetes.scheduler.enable = mkDefault true;
|
|
|
|
services.kubernetes.controllerManager.enable = mkDefault true;
|
|
|
|
services.kubernetes.addonManager.enable = mkDefault true;
|
|
|
|
services.kubernetes.proxy.enable = mkDefault true;
|
|
|
|
services.etcd.enable = true; # Cannot mkDefault because of flannel default options
|
|
|
|
services.kubernetes.kubelet = {
|
|
|
|
enable = mkDefault true;
|
|
|
|
taints = mkIf (!(elem "node" cfg.roles)) {
|
|
|
|
master = {
|
|
|
|
key = "node-role.kubernetes.io/master";
|
|
|
|
value = "true";
|
|
|
|
effect = "NoSchedule";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
(mkIf (all (el: el == "master") cfg.roles) {
|
|
|
|
# if this node is only a master make it unschedulable by default
|
|
|
|
services.kubernetes.kubelet.unschedulable = mkDefault true;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (elem "node" cfg.roles) {
|
|
|
|
services.kubernetes.kubelet.enable = mkDefault true;
|
|
|
|
services.kubernetes.proxy.enable = mkDefault true;
|
|
|
|
})
|
|
|
|
|
|
|
|
# Using "services.kubernetes.roles" will automatically enable easyCerts and flannel
|
|
|
|
(mkIf (cfg.roles != []) {
|
|
|
|
services.kubernetes.flannel.enable = mkDefault true;
|
|
|
|
services.flannel.etcd.endpoints = mkDefault etcdEndpoints;
|
|
|
|
services.kubernetes.easyCerts = mkDefault true;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.apiserver.enable {
|
|
|
|
services.kubernetes.pki.etcClusterAdminKubeconfig = mkDefault "kubernetes/cluster-admin.kubeconfig";
|
|
|
|
services.kubernetes.apiserver.etcd.servers = mkDefault etcdEndpoints;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.kubelet.enable {
|
2021-03-09 03:18:52 +00:00
|
|
|
virtualisation.containerd = {
|
2020-04-24 23:36:52 +00:00
|
|
|
enable = mkDefault true;
|
2021-09-23 15:35:13 +00:00
|
|
|
settings = mapAttrsRecursive (name: mkDefault) defaultContainerdSettings;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.apiserver.enable || cfg.controllerManager.enable) {
|
|
|
|
services.kubernetes.pki.certs = {
|
|
|
|
serviceAccount = mkCert {
|
|
|
|
name = "service-account";
|
|
|
|
CN = "system:service-account-signer";
|
|
|
|
action = ''
|
|
|
|
systemctl reload \
|
|
|
|
kube-apiserver.service \
|
|
|
|
kube-controller-manager.service
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (
|
|
|
|
cfg.apiserver.enable ||
|
|
|
|
cfg.scheduler.enable ||
|
|
|
|
cfg.controllerManager.enable ||
|
|
|
|
cfg.kubelet.enable ||
|
|
|
|
cfg.proxy.enable ||
|
|
|
|
cfg.addonManager.enable
|
|
|
|
) {
|
|
|
|
systemd.targets.kubernetes = {
|
|
|
|
description = "Kubernetes";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d /opt/cni/bin 0755 root root -"
|
|
|
|
"d /run/kubernetes 0755 kubernetes kubernetes -"
|
|
|
|
"d /var/lib/kubernetes 0755 kubernetes kubernetes -"
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.kubernetes = {
|
|
|
|
uid = config.ids.uids.kubernetes;
|
|
|
|
description = "Kubernetes user";
|
|
|
|
group = "kubernetes";
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
users.groups.kubernetes.gid = config.ids.gids.kubernetes;
|
|
|
|
|
|
|
|
# dns addon is enabled by default
|
|
|
|
services.kubernetes.addons.dns.enable = mkDefault true;
|
|
|
|
|
|
|
|
services.kubernetes.apiserverAddress = mkDefault ("https://${if cfg.apiserver.advertiseAddress != null
|
|
|
|
then cfg.apiserver.advertiseAddress
|
|
|
|
else "${cfg.masterAddress}:${toString cfg.apiserver.securePort}"}");
|
|
|
|
})
|
|
|
|
];
|
2022-01-13 20:06:32 +00:00
|
|
|
|
|
|
|
meta.buildDocsInSandbox = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|