Skip to main content
In this guide, you’ll learn how to provision virtual machines for a Kubernetes cluster using Vagrant and VirtualBox. The Kubernetes environment will include one master node and two worker nodes. We use two essential tools:
  1. VirtualBox – a hypervisor for running the VMs.
  2. Vagrant – an automation tool that spins up multiple VMs from a single configuration file.
Before you begin, ensure that both VirtualBox and Vagrant are installed on your system.
A preconfigured Vagrantfile is provided in the course repository. This file contains all the configuration details necessary to spin up the VMs. To get started, follow these steps:
  1. Copy the repository URL from the code dropdown.
  2. Open a terminal and execute the command below to clone the repository:
After cloning the repository, change into the directory:
List the directory contents using ls and verify that the Vagrantfile is present. Open the Vagrantfile to review its configuration.
The image shows the VirtualBox download page, offering links to download VirtualBox 7.0.6 for various platforms, along with additional resources like the Extension Pack and User Manual.

Vagrantfile Configuration

The provided Vagrantfile is set up to provision one master node and two worker nodes. It assigns specific IP addresses within the 192.168.56.x network, although Kubernetes configuration will be added later. Below is an excerpt from the Vagrantfile demonstrating the basic configuration:
The Vagrantfile further defines the provisioning of the master node as follows:
This configuration creates the master node with necessary provisioning scripts to update host files and DNS settings.

Checking Vagrant Status

Before creating the VMs, you can verify their current state. Since the VMs have not yet been created, running the command below will show that all nodes are in the “not created” state:
The image shows a GitHub repository page for a Certified Kubernetes Administrator (CKA) course, featuring files, a README, and options to clone or download the repository.
The output will list the VMs (e.g., kubemaster, kubenode01, and kubenode02) as “not created.”

Bringing Up the VMs

To provision all VMs, run the following command:
This command instructs Vagrant to pull the Ubuntu Bionic 64 base image and provision each machine according to the Vagrantfile configuration. The process might take some time as VirtualBox imports the box and deploys the VMs. You will see log messages that detail tasks such as:
  • Importing the base box.
  • Configuring network interfaces.
  • Executing provisioning shell scripts.
  • Replacing the default insecure SSH key.
Monitor the logs for progress messages like booting of the master node followed by the worker nodes. Once the provisioning is complete, check the status again:
The output will now list all three nodes (kubemaster, kubenode01, kubenode02) as “running.”

Accessing the VMs

You can access any VM using the vagrant ssh command followed by the VM’s name. For example, to connect to the master node, run:
After connecting, verify your directory contents using:
This confirms that you are inside the VM. When you need to exit, type:
Here is a sample interaction on the master node:
After logging out, you can verify that the nodes remain active by executing:
To connect to a worker node, such as kubenode01, use:
Within the worker node, you can check the system uptime by running:
A sample output might look like:
To access the remaining worker node, simply run:
After finishing work on any node, exit the SSH session by typing logout.

Next Steps

With all VMs provisioned and accessible, the next phase is to bootstrap the Kubernetes cluster using kubeadm. This step will configure the cluster components on each node and prepare the environment for running Kubernetes workloads.
This guide demonstrated automated provisioning of a multi-node Kubernetes cluster with Vagrant and VirtualBox. By using a consistent Vagrantfile, you ensure that each learner sets up an identical environment for a smooth Kubernetes deployment experience.
Happy provisioning!

Watch Video