Skip to content

Instantly share code, notes, and snippets.

@lucj
Created December 17, 2019 21:16
Show Gist options
  • Save lucj/5a0e2286b40130d02388a264e6924ed4 to your computer and use it in GitHub Desktop.
Save lucj/5a0e2286b40130d02388a264e6924ed4 to your computer and use it in GitHub Desktop.
Setup a k3s kubernetes cluster using Multipass VMs
for node in node1 node2 node3;do
multipass launch -n $node
done
# Init cluster on node1
multipass exec node1 -- bash -c "curl -sfL https://get.k3s.io | sh -"
# Get node1's IP
IP=$(multipass info node1 | grep IPv4 | awk '{print $2}')
# Get Token used to join nodes
TOKEN=$(multipass exec node1 sudo cat /var/lib/rancher/k3s/server/node-token)
# Join node2
multipass exec node2 -- \
bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"
# Join node3
multipass exec node3 -- \
bash -c "curl -sfL https://get.k3s.io | K3S_URL=\"https://$IP:6443\" K3S_TOKEN=\"$TOKEN\" sh -"
# Get cluster's configuration
multipass exec node1 sudo cat /etc/rancher/k3s/k3s.yaml > k3s.yaml
# Set node1's external IP in the configuration file
sed -i '' "s/127.0.0.1/$IP/" k3s.yaml
# We'r all set
echo
echo "K3s cluster is ready !"
echo
echo "Run the following command to set the current context:"
echo "$ export KUBECONFIG=$PWD/k3s.yaml"
echo
echo "and start to use the cluster:"
echo "$ kubectl get nodes"
echo
@devfire
Copy link

devfire commented Jan 30, 2022

this command sed -i '' "s/127.0.0.1/$IP/" k3s.yaml fails in linux.

Instead, just use this sed -i "s/127.0.0.1/$IP/" k3s.yaml this will work.

@mattmadhavan-gmail
Copy link

Hello @lucj,
Great tutorial! Thank you very much.

In addition do this do you have any example testing services Traefik and or ingress and my be Metal Any pointers or links will be appreciated

Thanks
Matt

@lucj
Copy link
Author

lucj commented Jan 8, 2025

Thanks @mattmadhavan-gmail. I have a couple of other similar scripts here as well as other Kubernetes tutorials/demos. I hope this can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment