Kubernetes cluster with Vagrant

Martin Devlin
3 min readDec 7, 2021

If you’re using Kubernetes in the real world you’ll typically do one of two things:

  1. Spin up real Kubernetes clusters on a cloud platform like EKS or GKE.

2. Use a lightweight play-runtime like minikube on your laptop for development or testing.

If you’re looking to take a closer look under the hood, however- to gain the CKA accreditation or work through Kelsey Hightower’s excellent Kubernetes The Hard Way, for example- then you’ll need a few bare-metal machines to play the role of “master” and “worker” nodes. In both cases you’ll be doing things that the cloud offerings and the likes of minikube do for you: configuring networking and security, installing and managing Kubernetes control-plane and worker components, etc.

In most walkthroughs you’re encouraged to use a cloud-compute runtime like EC2 or GCE to provision these machines. That works fine, but maybe you want to run the whole lot on your laptop instead. There’s a few advantages here: you don’t need a cloud account and you don’t need connectivity to the internet once you’ve built your cluster. You will save a few pennies as well.

Enter Vagrant. If you’re not familiar with this product it’s a tool for running virtual machines with useful additions like provisioners, networking, ssh and volume mounts; if you’ve grown up with containers instead of VMs think docker compose but for actual machines. It’s from the Hashicorp stable and it’s been around a long time so it’s got a first-class pedigree. Vagrant machines are cattle: you vagrant up, do your work and then vagrant destroy and it’s as though they never existed- perfect for our use-case. All you need is a linux laptop with at least 16GB RAM, an internet connection for the initial set up and you’re good to go.

So here’s the Vagrantfile I used for this purpose:

Note that it installs some packages and the basic Kubernetes binaries so you can jump right in and start running kubeadm but there’s a comment saying where to omit this bit if you’d rather do it yourself. To start, cd to the directory where you save this file (this directory is also mounted into the VMs at /var/kubernetes, which can be useful and works both ways) and

vagrant up

Once up and running (enter your password for sudo if it pauses and allow a good 5 minutes for the VMs to install) you can jump into the VMs using, for example:

vagrant ssh master1

From there you can start installing your cluster with:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address 192.168.121.10

When it’s finished it will tell you how to configure kubectl and how to join nodes to the cluster.

The workers are reached in the same way:

vagrant ssh worker-1

You join them to the cluster with a command like this (note that yours will have a different token and cert hash):

kubeadm join 192.168.121.10:6443 --token 8grxys.hu5qo7kyr30w01kx --discovery-token-ca-cert-hash sha256:9e602759664d018c1b296773358764669a492ea4b7ccb22a624ca2660ed0078e

Your nodes have joined the cluster but will be in the NotReadystate until you install a networking plugin, so back on the master1 node let’s install Calico to fix that:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

After a few minutes Calico will be up and running and your cluster is ready for action:

Split terminal with master1 on the left and the workers on the right

When you’re finished or you just want to nuke the site from orbit, go back to your directory with the Vagrantfile and

vagrant destroy

The End

--

--