Skip to content

Commit 1301c04

Browse files
committed
kubernetes: add multi-node test and docs
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 2308c29 commit 1301c04

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

hack/bats/extras/k8s.bats

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,30 @@ get_num_nodes() {
2626

2727
local_setup() {
2828
local nodes=$(get_num_nodes)
29+
local join_command=""
2930
for ((i=0; i<nodes; i++)); do
3031
limactl delete --force "${NAME}-$i" || :
31-
limactl start --tty=false --name "${NAME}-$i" "template:${TEMPLATE}" 3>&- 4>&-
32-
# NOTE: No support for multi-node clusters yet.
32+
local limactl_start_flags="--tty=false --name "${NAME}-$i""
33+
# Multi-node setup requires user-v2 network for VM-to-VM communication
34+
if [[ $nodes -gt 1 ]]; then
35+
limactl_start_flags+=" --network lima:user-v2"
36+
fi
37+
limactl start ${limactl_start_flags} "template:${TEMPLATE}" 3>&- 4>&- &
3338
done
39+
wait $(jobs -p)
40+
# Multi-node setup
41+
if [[ $nodes -gt 1 ]]; then
42+
for ((i=0; i<nodes; i++)); do
43+
if [[ $i -eq 0 ]]; then
44+
# Get the join command from the first node
45+
join_command=$(limactl shell "${NAME}-0" sudo kubeadm token create --print-join-command)
46+
else
47+
# Execute the join command on worker nodes
48+
limactl shell "${NAME}-$i" sudo bash -euxc "kubeadm reset --force ; ip link delete cni0 ; ip link delete flannel.1 ; rm -rf /var/lib/cni /etc/cni"
49+
limactl shell "${NAME}-$i" sudo ${join_command}
50+
fi
51+
done
52+
fi
3453
for node in $(k get node -o name); do
3554
k wait --timeout=5m --for=condition=ready "${node}"
3655
done
@@ -81,4 +100,54 @@ k() {
81100
done
82101
}
83102

84-
# TODO: add a test for multi-node
103+
# bats test_tags=nodes:3
104+
@test 'Multi-node' {
105+
# Based on https://github.com/rootless-containers/usernetes/blob/gen2-v20250828.0/hack/test-smoke.sh
106+
k apply -f - <<EOF
107+
apiVersion: v1
108+
kind: Service
109+
metadata:
110+
name: dnstest
111+
labels:
112+
run: dnstest
113+
spec:
114+
type: ClusterIP
115+
clusterIP: None
116+
ports:
117+
- name: http
118+
protocol: TCP
119+
port: 80
120+
targetPort: 80
121+
selector:
122+
run: dnstest
123+
---
124+
apiVersion: apps/v1
125+
kind: StatefulSet
126+
metadata:
127+
name: dnstest
128+
spec:
129+
serviceName: dnstest
130+
selector:
131+
matchLabels:
132+
run: dnstest
133+
replicas: 3
134+
template:
135+
metadata:
136+
labels:
137+
run: dnstest
138+
spec:
139+
containers:
140+
- name: dnstest
141+
image: ${TEST_CONTAINER_IMAGES[nginx]}
142+
ports:
143+
- containerPort: 80
144+
EOF
145+
k rollout status --timeout=5m statefulset/dnstest || {
146+
k describe pods -l run=dnstest
147+
false
148+
}
149+
# --rm requires -i
150+
k run -i --rm --image=${TEST_CONTAINER_IMAGES[nginx]} --restart=Never dnstest-shell -- sh -exc 'for f in $(seq 0 2); do wget -O- http://dnstest-${f}.dnstest.default.svc.cluster.local; done'
151+
k delete service dnstest
152+
k delete statefulset dnstest
153+
}

templates/k8s.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# (The join command printed here)
1919
#
2020
# $ limactl start --name k8s-1 --network lima:user-v2 template:k8s
21-
# $ limactl shell k8s-1 sudo kubeadm reset --force
21+
# $ limactl shell k8s-1 sudo bash -euxc "kubeadm reset --force ; ip link delete cni0 ; ip link delete flannel.1 ; rm -rf /var/lib/cni /etc/cni"
2222
# $ limactl shell k8s-1 sudo <JOIN_COMMAND_FROM_ABOVE>
2323

2424
minimumLimaVersion: 2.0.0

website/content/en/docs/examples/containers/kubernetes/_index.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ See also <https://github.com/rootless-containers/usernetes>.
5656

5757
A multi-node cluster can be created by creating multiple VMs connected via the [`lima:user-v2`](../../../config/network/user-v2.md) network.
5858

59+
As of Lima v2.0, the built-in `k8s` template is designed to support multi-node mode.
60+
5961
```bash
60-
limactl create --name k8s-0 --network lima:user-v2
61-
limactl create --name k8s-1 --network lima:user-v2
62+
limactl start --name k8s-0 --network lima:user-v2 template:k8s
63+
limactl shell k8s-0 sudo kubeadm token create --print-join-command
64+
# (The join command printed here)
6265
```
6366

64-
The cluster has to be set up manually, as the built-in templates do not support multi-node mode yet.
65-
Support for multi-node template is tracked in <https://github.com/lima-vm/lima/issues/4100>.
67+
```bash
68+
limactl start --name k8s-1 --network lima:user-v2 template:k8s
69+
limactl shell k8s-1 sudo bash -euxc "kubeadm reset --force ; ip link delete cni0 ; ip link delete flannel.1 ; rm -rf /var/lib/cni /etc/cni"
70+
limactl shell k8s-1 sudo <JOIN_COMMAND_FROM_ABOVE>
71+
```

0 commit comments

Comments
 (0)