Skip to content

cluster-init Flag Applied to All k3s Nodes Instead of Only the Initial Node in Configuration #5

@henrichter

Description

@henrichter

Describe the Issue
In the current configuration, all k3s nodes appear to be initialized with the --cluster-init flag, despite clusterInit being explicitly set to true only for the primary node (homelab-0). This results in unintended behavior where each server in the cluster runs as an initial server node.

services.k3s = {
enable = true;
role = "server";
tokenFile = /var/lib/rancher/k3s/server/token;
extraFlags = toString ([
"--write-kubeconfig-mode \"0644\""
"--cluster-init"
"--disable servicelb"
"--disable traefik"
"--disable local-storage"
] ++ (if meta.hostname == "homelab-0" then [] else [
"--server https://homelab-0:6443"
]));
clusterInit = (meta.hostname == "homelab-0");
};

services.k3s = { 
   enable = true; 
   role = "server"; 
   tokenFile = /var/lib/rancher/k3s/server/token; 
   extraFlags = toString ([ 
      "--write-kubeconfig-mode \"0644\"" 
      "--cluster-init" 
      "--disable servicelb" 
      "--disable traefik" 
      "--disable local-storage" 
   ] ++ (if meta.hostname == "homelab-0" then [] else [ 
      "--server https://homelab-0:6443" 
   ])); 
   clusterInit = (meta.hostname == "homelab-0"); 
}; 

The expectation is that clusterInit would apply only to homelab-0. However, setting --cluster-init in extraFlags causes all nodes to receive this flag, effectively overriding the clusterInit condition.

Relevant Code in nixpkgs
The NixOS k3s service configuration processes clusterInit separately from extraFlags, meaning additional flags in extraFlags are simply appended rather than overriding the conditions set by clusterInit:

https://github.com/NixOS/nixpkgs/blob/44cb05c5e6f21e8f80333d7c9eebae456d595f57/nixos/modules/services/cluster/k3s/default.nix#L515-L517

Expected Behavior
Only the primary node, homelab-0, should initialize the cluster with --cluster-init, while all other nodes should connect to it using --server https://homelab-0:6443.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions