Multi-nodes cluster
In this part, you will create a multi-nodes cluster.
Create the virtual machines
First, run the following command to launch VMs named k0s-1, k0s-2 and k0s-3.
multipass launch -n k0s-1 --disk 10G --cpus 2 --memory 2G
multipass launch -n k0s-2 --disk 10G --cpus 2 --memory 2G
multipass launch -n k0s-3 --disk 10G --cpus 2 --memory 2GNext list the VMs and make sure they are running fine:
multipass listYou should get an output similar to the following one (the IP addresses you get would be different though).
Name State IPv4 Image
k0s-1 Running 192.168.64.27 Ubuntu 24.04 LTS
k0s-2 Running 192.168.64.25 Ubuntu 24.04 LTS
k0s-3 Running 192.168.64.26 Ubuntu 24.04 LTSDownload k0s binary in each VMs
for i in 1 2 3; do
multipass exec k0s-$i -- /bin/sh -c "curl -sSf https://get.k0s.sh | sudo sh"
doneInit the cluster
First, run a shell in k0s-1.
multipass shell k0s-1Next, install k0s controller and start it.
sudo k0s install controller
sudo k0s start--single option as this instance will only act as a control plane Node.You can now exit the shell from k0s-1.
Adding worker Nodes
In order to add worker Nodes, we need to generate a token from the control plane and use this token to install k0s on the worker Nodes.
First, generate the token as follows. This will create the file worker_token in the current folder.
multipass exec k0s-1 -- sudo k0s token create --role=worker > ./worker_tokenworker role specified in the command indicates that the token will be used to add a worker (default value). We could also use a controller role to add additional controllers in the cluster.Next, copy that token into k0s-2 and k0s-3.
multipass transfer ./worker_token k0s-2:/tmp/worker_token
multipass transfer ./worker_token k0s-3:/tmp/worker_tokenNext, install k0s onto k0s-2 and k0s-3 VMs.
multipass exec k0s-2 -- sudo k0s install worker --token-file /tmp/worker_token
multipass exec k0s-3 -- sudo k0s install worker --token-file /tmp/worker_tokenThen, start k0s on both worker Nodes:
multipass exec k0s-2 -- sudo k0s start
multipass exec k0s-3 -- sudo k0s startGet a kubeconfig file
multipass exec k0s-1 -- sudo cat /var/lib/k0s/pki/admin.conf > k0s.kubeconfigNext, replace localhost with the actual IP address of the Multipass VM.
K0S1_IP=$(multipass info k0s-1 | grep IP | awk '{print $2}')
sed -i'' "s/localhost/$K0S1_IP/" k0s.kubeconfigThen, configure your local kubectl, so it uses this modified kubeconfig file.
export KUBECONFIG=$PWD/k0s.kubeconfigListing the cluster’s Nodes you should get a list similar to the one below.
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k0s-2 Ready <none> 92s v1.33.4+k0s
k0s-3 Ready <none> 92s v1.33.4+k0sk0s-1 was configured as a control plane only Node, it does not appear in this list which only shows the worker Nodes.Cleanup
You can now delete the VMs used in this section.
multipass delete -p k0s-1 k0s-2 k0s-3In the next section, you will use k0sctl, a k0s’s companion tool that simplifies the management of multi-nodes clusters.