Spin-up a Kubernetes cluster on personal laptop
Creating a Kubernetes cluster is very expensive on any cloud providers. Using Kubernetes on cloud for learning purpose is not recommended. Its because control plane is managed by cloud itself and we never get to know whats happening in control plane.
We can also spin-up a Kubernetes cluster on our personal computer/laptop given that we have enough resources on our laptop.
Before we begin the demo, Make sure Virtualization
is enabled in your laptop. This can be enabled from BIOS setting. Follow the manual for your laptop brand and model.
If you have already been using VirtualBox or VMWare workstation on your laptop, that means Virtualization is already enabled and working on your laptop.
Your Laptop is host machine/os and is used interchangeable here.
Contents
- Pre-requisites
- Why it works on my machine ?
- Install Virtualbox
- Install Vagrant
- Demo — Configurations
- Demo — Spin up the cluster
Pre-requisites
- 6 CPUs and 4Gi RAM available in laptop for 1 control plane + 1 node cluster
- At least 10Gi of free disk space
- Linux or Windows 10/11 desktop (Linux desktop preferred)
- A good internet connection
Why it works on my machine ?
I am running this demo on:
- Ubuntu 23.10 Desktop with 20 CPUs and 16G RAM
- VirtualBox version 7.0.14
- Vagrant version 2.3.4
Install VirtualBox
VirtualBox is a hypervisor (virtualization software) which is used to create and manage virtual machines on your laptop.
If you do not already have virtualbox, Install it on your laptop. If this is first time you are using virtual machine on your laptop, You may need to enable Virtualization from BIOS setting.
To install VirtualBox on your host, Follow this → https://www.virtualbox.org/wiki/Downloads
Windows 11 users can also use winget to install virtual box using below command
winget install virtualbox
Install Vagrant
Vagrant is a tool for building and managing virtual machine environments in a single workflow. It is developed and maintained by HashiCorp, An organization which also develops and maintains terraform.
It is used to write Virtual Machines as a code.
To install Vagrant on your laptop, follow this → https://developer.hashicorp.com/vagrant/install
Windows 11 users can use winget to install vagrant using below command
winget install vagrant
Reboot your computer after installation of VirtualBox and Vagrant.
Demo
Clone the Github repository on your laptop
git clone https://github.com/navilg/vagrant-kubernetes.git
Configurations
Inside the repository, there is a settings.yaml
file. You can change some configurations of Kuberenetes cluster by updating settings.yaml
file.
---
# cluster_name is used to group the nodes in a folder within VirtualBox:
cluster_name: Kubernetes Cluster
# VMs does not start in headless mode with some older version of VirtualBox in Windows 10 and 11 host.
# If VMs fails to start in headless mode, set gui as true.
gui: false # true or false.
runtime: containerd # containerd or crio
# All IPs/CIDRs should be private and allowed in /etc/vbox/networks.conf.
network:
# Worker IPs are simply incremented from the control IP.
control_ip: 10.0.0.10
dns_servers:
- 8.8.8.8
- 1.1.1.1
pod_cidr: 172.16.1.0/16
service_cidr: 172.17.1.0/18
nodes:
control:
cpu: 2 # Recommended: 2. Minimum: 1
memory: 2048 # RAM on control plane in Mi
workers:
count: 1 # Number of worker nodes
cpu: 2 # Number of vCPU per node. Recommended: 2 Minimum: 1
memory: 1536 # RAM per node in Mi
# Mount additional shared folders from the host into each virtual machine.
# Note that the project directory is automatically mounted at /vagrant.
shared_folders:
# - host_path: ./synced_volumes/common
# vm_path: /home/vagrant/common
software:
box: bento/ubuntu-22.04 # DON'T change
calico: 3.26.1 # Calico CNI plugin for networking. https://github.com/projectcalico/calico/releases
# To install dashboard installation, uncomment below line and set its version:
# dashboard: 2.7.0
kubernetes: 1.29 # >=1.28 Kubernetes version in format of major.minor
helm: v3.14.2
# Set the version of ingress_nginx to install or else comment it to skip installation
ingress_nginx: 4.10.0 # Helm chart version. https://github.com/kubernetes/ingress-nginx
# Set the version of csi_driver_nfs to install CSI driver for NFS or else comment it to skip installation
csi_driver_nfs: v4.6.0 # https://github.com/kubernetes-csi/csi-driver-nfs
- gui: Due to a known issue in Windows 10/11, Some versions of VirtualBox may not work in headless mode. So, If your laptop is running Windows 10/11 and you experience issue with running it in headless mode, consider enabling GUI mode. You can set
gui: true
in above file in that case. - Container Runtime: Containerd and CRI-O are two popular container runtime for running Kubernetes. You can set
runtime: containerd
orruntime: crio
to use respective container runtime. - Node count: You may adjust number of worker nodes by adjusting
.nodes.workers.count
in yaml file. - CPU and Memory: You may adjust CPU and memory allocated to control plane and worker nodes by adjusting cpu and memory field of
nodes.control
andnodes.workers
- Kubernetes Version: You can also set Kubernetes version by setting
.software.kubernetes
in yaml file. Always use latest or latest-1 or latest-2 version. E.g..software.kubernetes: 1.28
- Ingress Nginx: You can keep the field
.software.ingress_nginx
empty if you do not want to install nginx ingress controller on cluster. - CSI driver for NFS: You can keep the field
.software.csi_driver_nfs
empty if you do not wwant to install csi driver for nfs. This will make sure no csi driver is installed on cluster and no storage class is deployed by default. - Share files between host and nodes: You can share files between host system and control planes/worker nodes by uncommenting values of
shared_folders
field. This will mount the folder./synced_volumes/common
from repository folder at/home/vagrant/common
in all the nodes.
There are other configurable options, But its best to keep it as it is.
For Linux desktop host
If your host OS is Linux, use below command to allow virtualbox to allow all IP ranges.
sudo mkdir -p /etc/vbox/
echo "* 0.0.0.0/0 ::/0" | sudo tee -a /etc/vbox/networks.conf
Spin up the cluster
Once configuration is done. Run below command from repository folder to spin up the cluster
cd vagrant-kubernetes
vagrant up
It will start creating VMs for control plane and worker nodes and bootstrap them with required installations. It will take some time depending on network speed and your computer’s resources.
For Windows host OS, You may face some issue due to networking where SSH connection times out during bootstrapping. In that case, Just destroy the VM using below command and re-create it. You only need to destroy the VM in which problem happened and then re-run vagrant up.
For e.g. If master node (control plane) had some issue:
vagrant destroy master # or, vagrant destroy node01 -f # or, vagrant destroy node02 -f # depending on which vm had problem
vagrant up
Once all the nodes are up and spin up is completed. You can connect to cluster by connecting to control plane using below command.
vagrant ssh master
kubectl cluster-info
kubectl get nodes -o wide
helm ls -A
Stop the cluster
To stop the cluster, Run below command.
vagrant halt
This will shutdown all the nodes and control plane. Next time, when you start the cluster, It will be quick and does not need rebuilding and bootstrapping again.
To start the cluster again. You can run vagrant up
again.
To destroy/terminate the cluster
To terminate the cluster, Use below command
vagrant destroy -f
This will destroy/terminate the control plane and nodes. Next time, when you will start the cluster, It will again create new VMs and bootstrap it.
That’s it. I hope you liked this article and it helps you to learn Kubernetes. You can find and review the code for this spin-up at https://github.com/navilg/vagrant-kubernetes
Thank you for reading until the end. Before you go:
- Please consider clapping and following the author! 👏
- Subscribe to email notifications for new articles: https://navratangupta.medium.com/subscribe
- You can support my work by buying me a cup of coffee on https://www.buymeacoffee.com/linuxshots
Thank You!
Navratan Lal Gupta
Linux Shots