1 # A test that runs a multi-node k3s cluster and verify pod networking works across nodes
2 import ../make-test-python.nix (
10 imageEnv = pkgs.buildEnv {
11 name = "k3s-pause-image-env";
19 pauseImage = pkgs.dockerTools.buildImage {
20 name = "test.local/pause";
22 copyToRoot = imageEnv;
30 # A daemonset that responds 'server' on port 8000
31 networkTestDaemonset = pkgs.writeText "test.yml" ''
49 image: test.local/pause:local
50 imagePullPolicy: Never
54 command: ["socat", "TCP4-LISTEN:8000,fork", "EXEC:echo server"]
56 tokenFile = pkgs.writeText "token" "p@s$w0rd";
59 name = "${k3s.name}-multi-node";
65 environment.systemPackages = with pkgs; [
69 # k3s uses enough resources the default vm fails.
70 virtualisation.memorySize = 1536;
71 virtualisation.diskSize = 4096;
78 images = [ pauseImage ];
82 "--disable local-storage"
83 "--disable metrics-server"
86 "--node-ip 192.168.1.1"
87 "--pause-image test.local/pause:local"
90 networking.firewall.allowedTCPPorts = [
95 networking.firewall.allowedUDPPorts = [ 8472 ];
96 networking.firewall.trustedInterfaces = [ "flannel.1" ];
97 networking.useDHCP = false;
98 networking.defaultGateway = "192.168.1.1";
99 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
101 address = "192.168.1.1";
110 environment.systemPackages = with pkgs; [
114 virtualisation.memorySize = 1536;
115 virtualisation.diskSize = 4096;
121 images = [ pauseImage ];
122 serverAddr = "https://192.168.1.1:6443";
126 "--disable local-storage"
127 "--disable metrics-server"
128 "--disable servicelb"
130 "--node-ip 192.168.1.3"
131 "--pause-image test.local/pause:local"
134 networking.firewall.allowedTCPPorts = [
139 networking.firewall.allowedUDPPorts = [ 8472 ];
140 networking.firewall.trustedInterfaces = [ "flannel.1" ];
141 networking.useDHCP = false;
142 networking.defaultGateway = "192.168.1.3";
143 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
145 address = "192.168.1.3";
154 virtualisation.memorySize = 1024;
155 virtualisation.diskSize = 2048;
161 images = [ pauseImage ];
162 serverAddr = "https://192.168.1.3:6443";
164 "--pause-image test.local/pause:local"
165 "--node-ip 192.168.1.2"
168 networking.firewall.allowedTCPPorts = [ 6443 ];
169 networking.firewall.allowedUDPPorts = [ 8472 ];
170 networking.firewall.trustedInterfaces = [ "flannel.1" ];
171 networking.useDHCP = false;
172 networking.defaultGateway = "192.168.1.2";
173 networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
175 address = "192.168.1.2";
182 testScript = # python
186 machines = [server, server2, agent]
188 m.wait_for_unit("k3s")
190 # wait for the agent to show up
191 server.wait_until_succeeds("k3s kubectl get node agent")
194 m.succeed("k3s check-config")
196 server.succeed("k3s kubectl cluster-info")
197 # Also wait for our service account to show up; it takes a sec
198 server.wait_until_succeeds("k3s kubectl get serviceaccount default")
200 # Now create a pod on each node via a daemonset and verify they can talk to each other.
201 server.succeed("k3s kubectl apply -f ${networkTestDaemonset}")
202 server.wait_until_succeeds(f'[ "$(k3s kubectl get ds test -o json | jq .status.numberReady)" -eq {len(machines)} ]')
205 pods = server.succeed("k3s kubectl get po -o json | jq '.items[].metadata.name' -r").splitlines()
206 pod_ips = [server.succeed(f"k3s kubectl get po {name} -o json | jq '.status.podIP' -cr").strip() for name in pods]
208 # Verify each server can ping each pod ip
209 for pod_ip in pod_ips:
210 server.succeed(f"ping -c 1 {pod_ip}")
211 server2.succeed(f"ping -c 1 {pod_ip}")
212 agent.succeed(f"ping -c 1 {pod_ip}")
213 # Verify the pods can talk to each other
215 resp = server.succeed(f"k3s kubectl exec {pod} -- socat TCP:{pod_ip}:8000 -")
216 assert resp.strip() == "server"
219 meta.maintainers = lib.teams.k3s.members;