Complete Guide: Setting Up a Kubernetes Cluster from Scratch

Complete Guide: Setting Up a Kubernetes Cluster from Scratch

Setting up a Kubernetes cluster can seem like a daunting task, but with the right tools and a step-by-step approach, it becomes manageable. This guide will take you through the process of setting up a Kubernetes cluster using Oracle VirtualBox for virtual machines. I'll cover everything from hardware requirements to installing Kubernetes and configuring it with Calico networking. Specifically, I'll use two Oracle VirtualBox virtual machines: one for the control plane node and the other for the worker node.


Part 1: Hardware and Virtual Machine Setup

Software Requirements


- Oracle VirtualBox: A powerful x86 and AMD64/Intel64 virtualization product.

- ISO image of a Linux distribution (Ubuntu Budgie 22.04.2 LTS 64bit).

Setting Up Virtual Machines

  1. Install Oracle VirtualBox

Download and install Oracle VirtualBox from the official website.


  1. Create Virtual Machines

I'll create two virtual machines (VMs) - one for the control plane node and one for the worker node.


  • Control Plane Node


Name: k8s-control-plane

OS Type: Linux

Version: Ubuntu Budgie 22.04.2 LTS 64bit

Recommended Memory: 2 GB (Used: 4 GB)

Recommended Hard Disk: 20 GB (Used: 60 GB)

Recommended 2 CPU (Used: 4 CPU)


  • Worker Node


Name: k8s-worker-node

OS Type: Linux

Version: Ubuntu Budgie 22.04.2 LTS 64bit

Recommended Memory : 2 GB  (Used: 4 GB)

Recommended Hard Disk: 20 GB  (Used: 60 GB)

Recommended 2 CPU (Used: 4 CPU)


  • Configure Network Settings


Set the network adapter to Bridged Adapter for both VMs to ensure they can communicate with each other.



Set the network adapter to Bridged Adapter
Set the network adapter to Bridged Adapter
  • Configure CPU Settings


Enable VT-x/AMD-V




Configure CPU Settings
Configure CPU Settings








Part 2: Configuring Linux for Kubernetes


Prepare the System (both nodes)


  • Disable firewall (default is disabled)

  • Set static IP on both nodes

192.168.1.12 on control plane node

192.168.1.13 on worker node


  • Disable firewall

sudo swapoff -a

sudo sed -i '/ swap / s/^/#/' /etc/fstab


  • Update hostname

  1. on control plane node

sudo hostnamectl set-hostname "k8s-control-plane"

exec bash

  1. on worker node

sudo hostnamectl set-hostname "k8s-worker-node"

exec bash


  • Update file /etc/hosts on both nodes

192.168.1.12 k8s-control-plane

192.168.1.13 k8s-worker-node


  • Configure the IPV4 bridge on all nodes, execute the following commands on each node:

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf

overlay

br_netfilter

EOF



sudo modprobe overlay

sudo modprobe br_netfilter


  • Set Up sysctl params required by setup, params persist across reboots

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-iptables  = 1

net.bridge.bridge-nf-call-ip6tables = 1

net.ipv4.ip_forward                 = 1

EOF


Apply the settings:

sudo sysctl --system


  • Add new required packages

  1. apt-transport-https - APT transport for downloading via the HTTP Secure protocol (HTTPS)


sudo apt-get install -y apt-transport-https ca-certificates curl gpg


  1. curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg 


(If the directory `/etc/apt/keyrings` does not exist, it should be created before the curl command, read the note below.

sudo mkdir -p -m 755 /etc/apt/keyrings).


  1. echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list


  1. sudo apt-get update

Install containerd (both nodes)

Containerd is an industry-standard core container runtime that is widely used in Kubernetes environments. It serves as a lightweight and extensible container runtime that manages the complete container lifecycle on a single node, including image transfer and storage, container execution and supervision, and low-level storage and network attachments.

  1. Install Docker


sudo apt install docker.io


  1. Configure containerd


sudo mkdir /etc/containerd

sudo sh -c "containerd config default > /etc/containerd/config.toml"

sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml

sudo systemctl restart containerd.service

sudo systemctl restart kubelet.service


Part 3: Installing Kubernetes


Install Kubernetes Components  (both nodes)


Install kubelet, kubeadm, and kubectl:


  • Kubelet: Node agent ensuring the containers in pods are running as expected.
  • Kubeadm: Tool for easily setting up and managing Kubernetes clusters.
  • Kubectl: Command-line tool for interacting with the Kubernetes cluster and managing resources.

sudo apt-get install -y kubelet kubeadm kubectl

sudo apt-mark hold kubelet kubeadm kubectl

sudo systemctl enable --now kubelet


Install Kubernetes (control plane node)


sudo kubeadm config images pull


Initialize the Control Plane Node (control plane node)


Initialize control node

sudo kubeadm init --pod-network-cidr=192.168.0.0/16


Set Up kubeconfig for the Default User*


mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config


Install Calico Network Plugin


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


Join the Worker Node to the Cluster with the Join Token on Control Plane Node


A Join Token on the Control Plane Node is a token generated by Kubernetes that allows worker nodes to securely join a Kubernetes cluster. When initializing the control plane with kubeadm, a join token is created and can be used to add additional nodes to the cluster. This token serves as a form of authentication, ensuring that only authorized nodes can join the cluster.

kubeadm token create --print-join-command


This command will output a join command. Copy this command:


sudo kubeadm join 192.168.1.12:6443 --token xxxxxxxxxxx --discovery-token-ca-cert-hash yyyyyyyyyyyyy


Run the Join Command on the Worker Node

   - SSH into the worker node and execute the join command provided by the control plane node.


Execute the kubectl command from the master node to check if the node is added to the master


kubectl get nodes


kubectl get nodes output
kubectl get nodes output

Conclusion


Congratulations! You've successfully set up a Kubernetes cluster from scratch using Oracle VirtualBox. Your cluster is now ready to deploy containerized applications. This guide covered the essential steps, from configuring your virtual machines and installing necessary software to setting up Kubernetes and networking with Calico.


Reference


Call to Action:

Follow for more Kubernetes and software architecture tips! Share your suggestions on topics you'd like to explore next.

About Me

I am passionate about IT technologies. If you’re interested in learning more or staying updated with my latest articles, feel free to connect with me on:

Feel free to reach out through any of these platforms if you have any questions!

I am excited to hear your thoughts! 👇



Comments

Popular posts from this blog

Monitoring and Logging with Prometheus: A Practical Guide

Creating a Complete CRUD API with Spring Boot 3: Authentication, Authorization, and Testing

Logging in Spring Boot 3: Best Practices for Configuration and Implementation