How to setup a Kubernetes cluster on virtual machines through Vagrant and Microk8s

Kubernetes (K8S) is for a few years now a trending topic. If you are approaching it, you need a way to test what you’re learning - the usual way for beginners consists in the use of minikube, an out-of-the-box solution that sets up a single-node K8S cluster you can use for learning purposes, tipically through virtualization (but deploy on container and bare-metal is supported).

If you want to experiment with a production-like multi-node cluster, you have to find another solution - tipically you end up using a cloud provider supporting free subscription, like okteto, or consuming your free initial credit on something like GKE, EKS, or AKS.

In the past three years I’ve explored another approach, installing a K8S cluster on a group of VMs running on my physical machine.

I initially tried installing and configuring from scratch everything I needed - very interesting way of learning, but very annoying way to proceed if you need a running cluster in minutes: you need to choose and install the OS, pick and install one container manager (e.g. containerd), install official K8S packages (kubelet, kubeadm), disable the swap (for real!), check and fix firewall’s rules, configure a cgroupdriver, restart a bunch of system daemons, … then you are ready to set up the cluster, starting a primary and at least one worker node, choosing and installing a network plugin, a metrics plugin, an ingress plugin… easy, right?

If you want play with the cluster without entering the maze of system installation and configuration, the way to go is to setup your cluster through one Kubernetes Distribution - something like MicroK8s or K3s.

And if you’re an automation addicted like me, may be you end up with a set of parameterizable script you can use to setup a cluster with a handful of CLI command: this is my version ot the game - you can try opening a terminal and lunching

git clone
cd vagrant-microk8s-cluster

Configuration (for cluster name, VMs’ IPs, …) is available through environment variables (see ./ for details).

After a few minutes you’ll have a working, MicroK8s-based cluster: its configuration is available in the ${CLUSTER_NAME}-config file created in the vagrant-microk8s-cluster after the execution of the script.

The provided solution works with Vagrant and libvirt, so you need to install both on you system - and the libvirt provider for Vagrant, too. If you’re not confortable with libvirt, you can switch to VirtualBox or to another virtualization solution supported by Vagrant, installing what you need on you machine and editing the first six lines of the Vagrantfile provided in the cloned repository.

Good luck and enjoy playing with you brand new K8S cluster!!