Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Using Vagrant with Multi-Node MicroK8S to experiment with Kubernetes

This script (using Vagrant) is used to build a pair of Ubuntu based virtual machines that have the MicroK8S setup of Kubernetes.

It also installs docker, and sets up aliases for kubectl in line with the MicroK8S documentation.

Please note that this script relies on the fact that Vagrant transfers files in /vagrant to the host file system, which we can then use to setup the second node.

If you only want a single node version, here's a similar repository.

Rationale

On my Windows machine, I wanted to follow the advice of a colleague and run MicroK8S to experiment with Kubernetes. I followed the advice on MicroK8S, and tried to use Multipass and found that it recommends using Hyper-V on Windows Hosts. Unfortunately, on my machine, I also use VirtualBox for some machines I've worked with, so I'm unable to use the Hyper-V based MultiPass. Multipass does allow a VirtualBox backend, however, it doesn't then bridge an interface, nor does it allow port forwarding.

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
config.vm.provider "virtualbox" do |vb|
vb.memory = 4096
vb.cpus = 2
end
config.vm.provision "shell", inline: <<-EOF
snap install microk8s --classic
snap install docker
microk8s.status --wait-ready
microk8s.enable dns dashboard registry
usermod -a -G microk8s vagrant
echo "alias kubectl='microk8s.kubectl'" > /home/vagrant/.bash_aliases
chown vagrant:vagrant /home/vagrant/.bash_aliases
echo "alias kubectl='microk8s.kubectl'" > /root/.bash_aliases
chown root:root /root/.bash_aliases
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
microk8s_a.vm.provision "shell", inline: <<-EOF
export local_ip="$(ip route | grep default | grep enp0s8 | cut -d' ' -f9)"
microk8s.add-node | grep $local_ip | tee /vagrant/add_k8s
EOF
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
microk8s_b.vm.provision "shell", inline: <<-EOF
bash -x /vagrant/add_k8s
EOF
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.