Multi-node Kubernetes cluster on your local Ubuntu 22.04 machine using Vagrant and VirtualBox
Setting up a multi-node Kubernetes cluster on your local Ubuntu 22.04 machine using Vagrant and VirtualBox involves the below steps. We'll create one master node and two worker nodes for this example. Make sure you have Vagrant and VirtualBox installed on your Ubuntu 22.04 machine before proceeding.
Step 1: Install Prerequisites Make sure you have Vagrant and VirtualBox installed. You can install them using apt
:
sudo apt update
sudo apt install virtualbox vagrant
Step 2: Initialize the Vagrant Project Create a new directory for your Vagrant project and navigate to it:
mkdir kubernetes-cluster
cd kubernetes-cluster
Step 3: Create the Vagrantfile Create a Vagrantfile
with the following content:
Vagrant.configure("2") do |config|
config.vm.define "k8s-master" do |master|
master.vm.box = "ubuntu/bionic64"
master.vm.network "private_network", type: "dhcp"
master.vm.provider "virtualbox"
master.vm.hostname = "k8s-master"
master.vm.provision "shell", inline: "echo 'This is the Kubernetes Master Node'"
end
(1..2).each do |i|
config.vm.define "k8s-worker-#{i}" do |worker|
worker.vm.box = "ubuntu/bionic64"
worker.vm.network "private_network", type: "dhcp"
worker.vm.provider "virtualbox"
worker.vm.hostname = "k8s-worker-#{i}"
worker.vm.provision "shell", inline: "echo 'This is Kubernetes Worker Node #{i}'"
end
end
end
This Vagrantfile creates one master node and two worker nodes, all running Ubuntu 18.04.
Step 4: Start and Provision the Cluster Initialize and provision the cluster:
vagrant up
Step 5: SSH into the Nodes SSH into the master and worker nodes:
vagrant ssh k8s-master
vagrant ssh k8s-worker-1
vagrant ssh k8s-worker-2
Step 6: Install Docker On each node (master and workers), install Docker:
sudo apt update
sudo apt install -y docker.io
Step 7: Install Kubernetes On each node (master and workers), install Kubernetes:
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubeadm kubelet kubectl
Step 8: Initialize the Kubernetes Master Node On the master node, initialize the Kubernetes master:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Step 9: Set Up Cluster Networking On the master node, set up a cluster network using Flannel. You can choose other CNI plugins like Calico, but Flannel is simple and suitable for this example:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Step 10: Join Worker Nodes After initializing the master node, you'll see a kubeadm join
command. Copy and run it on each worker node to join them to the cluster. It will look something like:
sudo kubeadm join <master-node-ip>:6443 --token <token> --discovery-token-ca-cert-hash <hash>
Step 11: Verify the Cluster On the master node, you can verify that all nodes are part of the cluster:
kubectl get nodes
You should see the master node and both worker nodes listed as "Ready."
Your multi-node Kubernetes cluster is now set up on your local Ubuntu 22.04 machine using Vagrant and VirtualBox. You can start deploying applications and services to your cluster.