Install Kubernetes Cluster on Ubuntu 22.04.2 VM Lab in 2023

April 13, 2023
install kubernetes cluster on ubuntu 22042

Introduction: Kubernetes has become the leading platform for container orchestration and cluster management, making it an essential tool for developers and system administrators. In this step-by-step guide, we will show you how to install Kubernetes cluster on Ubuntu 22.04.2 VMware Lab in With the help of bash code, we’ll make the process of installation and configuration easier and faster.

Understanding Kubernetes and VMware Lab Before we dive into the installation process, let’s take a moment to understand what Kubernetes and VMware Lab are. Kubernetes is an open-source container orchestration platform that enables the deployment, scaling, and management of containerized applications. VMware Lab is a virtualization platform that provides a computing environment for creating and managing virtual machines.

Prerequisites To follow this guide, you will need:

  • A working VMware Lab environment with three ubuntu 20.04.2 installed vms
  • A Ubuntu 22.04.2 virtual machine (Master, Worker1, Worker2)
  • VMs=3, Processor=2CPU, Disk=20GB free disk space or more
  • Root access to the Ubuntu 22.04.2 virtual machine
  • Basic knowledge of Linux commands
  • Internet connectivity

Skip Ubuntu 22.04.2 Intstallation Guide

Lab Setup

  • VM1=Kubernetes Master Node: –<hostname>
  • VM2=Worker Node-1: –<hostname>
  • VM3=Worker Node-2: –<hostname>

Installing Kubernetes on Ubuntu 22.04.2

  • Update and upgrade the Ubuntu 22.04.2 virtual machine
sudo apt update && sudo apt upgrade

Add hostname entries in the host-nodes, set hostname

  • Login to master node vm and set hostname using hostnamectl
sudo hostnamectl set-hostname ""
exec bash
  • Login to worker1 nodes vm and set hostname using hostnamectl
sudo hostnamectl set-hostname "" //1 worker node
exec bash
  • Login to worker2 nodes vm and set hostname using hostnamectl
sudo hostnamectl set-hostname "" //2 worker node
exec bash

Add the following entries in /etc/hosts file on each node (master, w1, w2)

sudo nano /etc/hosts master w1 w2

Install Docker Container on all VMs

sudo apt install
  • Add user in docker group (dtechyworld)
sudo add usermod -aG docker dtechyworld
  • Check docker version currently installed
docker ––version

Enable and Start Docker Container

  • Set Docker to launch at boot by entering the following:
sudo systemctl enable docker
sudo systemctl start docker
  • Confirm Docker Container running status:
sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-03-20 17:04:01 -03; 3h 1min ago
TriggeredBy: ● docker.socket
   Main PID: 7272 (dockerd)
      Tasks: 23
     Memory: 156.3M
     CGroup: /system.slice/docker.service
             └─2282 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  • Add user in sudo group to install kubernetes (optional)
sudo add usermod -aG sudo dtechyworld

Install Kubernetes in Ubuntu 22.04.2

curl -s | sudo apt-key add -
  • if curl is not installed or find error install curl
sudo apt install curl
  • Add the Kubernetes repository to the Ubuntu 20.04.2 virtual machine
sudo apt-add-repository "deb kubernetes-xenial main"
  • Update the package list and install Kubernetes components
sudo apt update && sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
  • All Steps completed now verify kubeadm
kubeadm version
  • Repeat for each server node.

Kubernetes Deployment Controller

Before Initialze kube Disable the swap memory on each server one by one (master, w1, w2)

sudo swapoff –a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  • Initialize Kubernetes cluster with Kubeadm on Master Ubuntu 20.04.2 virtual Machine
sudo kubeadm init
your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join --token fpzyxd.51my1qocwb68t1nd \
	--discovery-token-ca-cert-hash sha256:4cf856842339f8d3bb9bf660212ce5dab743560232334f36e3fc3ebc48dfb1af \

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join --token fpzyxd.51my1qocwb68t1nd \
	--discovery-token-ca-cert-hash sha256:4cf856842339f8d3bb9bf660212ce5dab743560232334f36e3fc3ebc48dfb1af
  • Copy the kubeconfig file to your home directory or run following
mkdir -p $HOME/.kube 
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • Install the flannel network addon using the following command on master node
kubectl apply -f

Joining Worker Nodes to the Kubernetes Cluster

  • To join worker nodes to the Kubernetes cluster, got w1 worker node paste or type
kubeadm join --token fpzyxd.51my1qocwb68t1nd \
	--discovery-token-ca-cert-hash sha256:4cf856842339f8d3bb9bf660212ce5dab743560232334f36e3fc3ebc48dfb1af
  • Repeat step on w2 worker node
kubeadm join --token fpzyxd.51my1qocwb68t1nd \
	--discovery-token-ca-cert-hash sha256:4cf856842339f8d3bb9bf660212ce5dab743560232334f36e3fc3ebc48dfb1af
  • kubectl commands to view cluster and node status
kubectl cluster-info
kubectl get nodes
NAME               STATUS     ROLES           AGE   VERSION  Ready   	  control-plane   50M   v1.26.3	   Ready   	  <none>   		  10M   v1.26.3 	   Ready   	  <none>          10M   v1.26.3

if it is not ready so please wait

Test Deployment POD

kubectl create deployment nginx-app --image=nginx --replicas=2
  • Check status of deployment Pods
kubectl get deployment nginx-app
nginx-app   2/2     2            2           68s
  • Expose node port
$ kubectl expose deployment nginx-app --type=NodePort --port=80
service/nginx-app exposed
  • check service status
$ kubectl get svc nginx-app
$ kubectl describe svc nginx-app

Hope this guide found useful. That’s all for now..