How to spin up local Kubernetes cluster with Vagrant in 10 minutes

Published on 10 October, 2022

How to spin up local Kubernetes cluster with Vagrant in 10 minutes

Illustration by Storyset

In this article we'll explain how to setup Kubernetes cluster on your local machine with Vagrant and MicroK8s. Eventually, you'll be able to easily develop and test your Kubernetes applications without buying actual infrastructure.

Minimum software - just Vagrant and VirtualBox

In order to start our journey into Kubernetes let's make sure we have VirtualBox and Vagrant installed. We are not going to cover installation part in this article as we assume you can make it yourself.

To spin up our cluster we'll be using MicroK8s. It is the easiest and fastest way to get Kubernetes up and running as it is the simplest production-grade upstream K8s.

Let's create Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.network "public_network", use_dhcp_assigned_default_route: true
    config.vm.provider "virtualbox" do |vb|
      vb.memory = 2048
      vb.cpus = 2
    end
  config.vm.provision "shell", inline: <<-EOF
    snap install microk8s --classic
    microk8s status --wait-ready
    usermod -a -G microk8s vagrant
  EOF

  config.vm.define "microk8s_a" do |microk8s_a|
    microk8s_a.vm.hostname = "microk8s-a"
    microk8s_a.vm.provider "virtualbox" do |vb|
      vb.name = "microk8s-a"
    end
  end

  config.vm.define "microk8s_b" do |microk8s_b|
    microk8s_b.vm.hostname = "microk8s-b"
    microk8s_b.vm.provider "virtualbox" do |vb|
      vb.name = "microk8s-b"
    end
  end
end

The configuration above creates 2 virtual machines with Ubuntu 22.04 (Jammy Jellyfish), attaches machines to your network, installs Microk8s and adds vagrant user to microk8s group.

Just run vagrant up inside the directory with your Vagrantfile. Vagrant will ask you to chose a bridged network interface and in 5-10 minutes you'll get a couple of virtual machines running.

Let's configure our Kubernetes cluster

So, at this point we have 2 virtual machines: microk8s_a and microk8s_b with respective hostnames microk8s-a and microk8s-b. In order to SSH into your machine you just need to run vagrant ssh <machine>. Let's login to our primary machine using command: vagrant ssh microk8s_a. Just make sure you are running the command within the directory with your Vagrantfile.

In order to join machines together into the cluster we have to make sure our primary machine resolves hostnames of other VMs in the network. To check the IP address of your machine you can run the command ip route. Let's get IP of the first machine.

vagrant@microk8s-a:~$ ip route | grep default | grep enp0s8 | cut -d' ' -f9
192.168.11.129

And now in separate terminal login to the second machine.

vagrant@microk8s-b:~$ ip route | grep default | grep enp0s8 | cut -d' ' -f9
192.168.11.130

Make sure our primary machine (microk8s_a) has proper /etc/hosts entry for microk8s_a.

vagrant@microk8s-a:~$ sudo -i
root@microk8s-a:~# echo "192.168.11.130 microk8s-b" >> /etc/hosts
exit

Time to run our cluster, eventually! Let's run microk8s add-node command on microk8s_a machine. You'll get a command with a token. Pick the one which is corresponding the IP address of microk8s_a machine.

vagrant@microk8s-a:~$ microk8s add-node
From the node you wish to join to this cluster, run the following:
microk8s join 10.0.2.15:25000/d8046ab6aefabfb531d3e03d02e4608d/4972959f5865

Use the '--worker' flag to join a node as a worker not running the control plane, eg:
microk8s join 10.0.2.15:25000/d8046ab6aefabfb531d3e03d02e4608d/4972959f5865 --worker

If the node you are adding is not reachable through the default interface you can use one of the following:
microk8s join 10.0.2.15:25000/d8046ab6aefabfb531d3e03d02e4608d/4972959f5865
microk8s join 192.168.11.129:25000/d8046ab6aefabfb531d3e03d02e4608d/4972959f5865

In our case it is microk8s join 192.168.11.129:25000/d8046ab6aefabfb531d3e03d02e4608d/4972959f5865. So, let's login into microck8s_b machine and execute the command.

Joining confirmation will apper soon.

vagrant@microk8s-b:~$ microk8s join 192.168.11.129:25000/e2530aa2ce1fbfaee65a7cea4fcc7698/4972959f5865
Contacting cluster at 192.168.11.129
Waiting for this node to finish joining the cluster. .. .. ..

There is a command to check the list of connected nodes to the claster. It could be done on any machine.

vagrant@microk8s-a:~$ microk8s kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
microk8s-a   Ready    <none>   58m   v1.25.2
microk8s-b   Ready    <none>   51s   v1.25.2

At this point you should have ready to use Kubernetes cluster. To shutdown the cluster use vagrant halt command. Eventually, you can destroy the VMs using vagrant destroy command.

About the Author

About our team

Jet.Dev is a team of digital specialists who design, build and optimize digital solutions.

Since 2016, being a Drupal development company, the team has partnered with companies of all sizes, from startups to enterprises, to help them build appealing websites, robust web apps, and secure integrations.