Skip to content

Commit c5d8fcc

Browse files
committed
kubernetes: add multi-node test and docs
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 2796dbf commit c5d8fcc

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

hack/bats/extras/k8s.bats

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,31 @@ 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 kubeadm reset --force
49+
limactl restart "${NAME}-$i"
50+
limactl shell "${NAME}-$i" sudo ${join_command}
51+
fi
52+
done
53+
fi
3454
for node in $(k get node -o name); do
3555
k wait --timeout=5m --for=condition=ready "${node}"
3656
done
@@ -81,4 +101,53 @@ k() {
81101
done
82102
}
83103

84-
# TODO: add a test for multi-node
104+
# bats test_tags=nodes:3
105+
@test 'Multi-node' {
106+
# Based on https://github.com/rootless-containers/usernetes/blob/gen2-v20250828.0/hack/test-smoke.sh
107+
k apply -f - <<EOF
108+
apiVersion: v1
109+
kind: Service
110+
metadata:
111+
name: dnstest
112+
labels:
113+
run: dnstest
114+
spec:
115+
type: ClusterIP
116+
clusterIP: None
117+
ports:
118+
- name: http
119+
protocol: TCP
120+
port: 80
121+
targetPort: 80
122+
selector:
123+
run: dnstest
124+
---
125+
apiVersion: apps/v1
126+
kind: StatefulSet
127+
metadata:
128+
name: dnstest
129+
spec:
130+
serviceName: dnstest
131+
selector:
132+
matchLabels:
133+
run: dnstest
134+
replicas: 3
135+
template:
136+
metadata:
137+
labels:
138+
run: dnstest
139+
spec:
140+
containers:
141+
- name: dnstest
142+
image: ${TEST_CONTAINER_IMAGES[nginx]}
143+
ports:
144+
- containerPort: 80
145+
EOF
146+
k rollout status --timeout=5m statefulset/dnstest || {
147+
k describe pods -l run=dnstest
148+
false
149+
}
150+
k run --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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020
# $ limactl start --name k8s-1 --network lima:user-v2 template:k8s
2121
# $ limactl shell k8s-1 sudo kubeadm reset --force
22+
# $ limactl restart k8s-1
2223
# $ limactl shell k8s-1 sudo <JOIN_COMMAND_FROM_ABOVE>
2324

2425
minimumLimaVersion: 2.0.0

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ 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 kubeadm reset --force
70+
limactl restart k8s-1
71+
limactl shell k8s-1 sudo <JOIN_COMMAND_FROM_ABOVE>
72+
```

0 commit comments

Comments
 (0)