1# Kubernetes {#sec-kubernetes} 2 3The NixOS Kubernetes module is a collective term for a handful of 4individual submodules implementing the Kubernetes cluster components. 5 6There are generally two ways of enabling Kubernetes on NixOS. One way is 7to enable and configure cluster components appropriately by hand: 8 9```nix 10services.kubernetes = { 11 apiserver.enable = true; 12 controllerManager.enable = true; 13 scheduler.enable = true; 14 addonManager.enable = true; 15 proxy.enable = true; 16 flannel.enable = true; 17}; 18``` 19 20Another way is to assign cluster roles ("master" and/or "node") to 21the host. This enables apiserver, controllerManager, scheduler, 22addonManager, kube-proxy and etcd: 23 24```nix 25services.kubernetes.roles = [ "master" ]; 26``` 27 28While this will enable the kubelet and kube-proxy only: 29 30```nix 31services.kubernetes.roles = [ "node" ]; 32``` 33 34Assigning both the master and node roles is usable if you want a single 35node Kubernetes cluster for dev or testing purposes: 36 37```nix 38services.kubernetes.roles = [ "master" "node" ]; 39``` 40 41Note: Assigning either role will also default both 42[](#opt-services.kubernetes.flannel.enable) 43and [](#opt-services.kubernetes.easyCerts) 44to true. This sets up flannel as CNI and activates automatic PKI bootstrapping. 45 46::: {.note} 47As of NixOS 19.03, it is mandatory to configure: 48[](#opt-services.kubernetes.masterAddress). 49The masterAddress must be resolveable and routeable by all cluster nodes. 50In single node clusters, this can be set to `localhost`. 51::: 52 53Role-based access control (RBAC) authorization mode is enabled by 54default. This means that anonymous requests to the apiserver secure port 55will expectedly cause a permission denied error. All cluster components 56must therefore be configured with x509 certificates for two-way tls 57communication. The x509 certificate subject section determines the roles 58and permissions granted by the apiserver to perform clusterwide or 59namespaced operations. See also: [ Using RBAC 60Authorization](https://kubernetes.io/docs/reference/access-authn-authz/rbac/). 61 62The NixOS kubernetes module provides an option for automatic certificate 63bootstrapping and configuration, 64[](#opt-services.kubernetes.easyCerts). 65The PKI bootstrapping process involves setting up a certificate authority (CA) 66daemon (cfssl) on the kubernetes master node. cfssl generates a CA-cert 67for the cluster, and uses the CA-cert for signing subordinate certs issued 68to each of the cluster components. Subsequently, the certmgr daemon monitors 69active certificates and renews them when needed. For single node Kubernetes 70clusters, setting [](#opt-services.kubernetes.easyCerts) 71= true is sufficient and no further action is required. For joining extra node 72machines to an existing cluster on the other hand, establishing initial 73trust is mandatory. 74 75To add new nodes to the cluster: On any (non-master) cluster node where 76[](#opt-services.kubernetes.easyCerts) 77is enabled, the helper script `nixos-kubernetes-node-join` is available on PATH. 78Given a token on stdin, it will copy the token to the kubernetes secrets directory 79and restart the certmgr service. As requested certificates are issued, the 80script will restart kubernetes cluster components as needed for them to 81pick up new keypairs. 82 83::: {.note} 84Multi-master (HA) clusters are not supported by the easyCerts module. 85::: 86 87In order to interact with an RBAC-enabled cluster as an administrator, 88one needs to have cluster-admin privileges. By default, when easyCerts 89is enabled, a cluster-admin kubeconfig file is generated and linked into 90`/etc/kubernetes/cluster-admin.kubeconfig` as determined by 91[](#opt-services.kubernetes.pki.etcClusterAdminKubeconfig). 92`export KUBECONFIG=/etc/kubernetes/cluster-admin.kubeconfig` will make 93kubectl use this kubeconfig to access and authenticate the cluster. The 94cluster-admin kubeconfig references an auto-generated keypair owned by 95root. Thus, only root on the kubernetes master may obtain cluster-admin 96rights by means of this file.