Back in the old days, building a home lab to support your learning was tidious task. You had to build the VMs in VMware Workstation or Virtualbox and the configure those VMs individually to get the "desired state". If you made a mistake during the process you had to do everything from scratch. Alot ot time and effort was lost in building these lab environments. This was the case for me until I got my hands on Vagrant. This amazing tool makes your life easier and takes care of pulling the OS images and building them. You can add build scripts to configure the OS post installation. I am working on getting myself CKA by the end of ths year and I needed a homelab which had a master node and couple of worker nodes. In this guide I will walk you through the Vagrantfile that I am using for the lab spinup.
Make sure you have below installed beforehand:
You can run git clone to create the clone of this repository locally:
git clone https://github.com/swordfish291/vagrant-kubernetes-lab.git
Navigate to the directory and run:
vagrant up
Once the cluster is up, you can run vagrant status
to check the status of the VMs:
Now you can ssh to the master node using vagrant ssh k8s-master
Now you can run kubectl get nodes
to check the status of the nodes:
Cluster is ready. You can start hacking !
Vagrantfile for this takes couple of values as a variable that matches your Virtualbox setup and your desired number of worker nodes:
NUM_WORKER_NODE = 4
IP_NW = "192.168.56."
NODE_IP_START = 20
These are the default values and can be modifed to match your needs. Vagrant takes these variables and loops through these values to build the environment. The image that we have specified is:
config.vm.box = "ubuntu/bionic64"
There are three provisioning scripts that are being used to prepare the master and worker nodes. One script is prepared as part of the build process to join the worker nodes to the control plane.
This script prepares all the nodes for network. It adds the host entries and modifes the DNS server settings. If you are planning to use more than 2 worker nodes, you will need to modify below section and add the additional host entries for the additioal worker nodes:
cat >> /etc/hosts <<EOF
192.168.5.10 k8s-master
192.168.5.21 k8s-worker-1
192.168.5.22 k8s-worker-2
EOF
This script installs the required packages and botstraps the control plane components. Once the cluster is initialized, it prepares the join string for the worker nodes to prevent manual intervention. You can modify the last part of this script suit your needs for pod network.
This script installs the required packages to the worker nodes and uses the joinstring.sh script to join the worker nodes to the initialized cluster.