How To Install Kubernetes on Ubuntu 22.04: A Step-by-Step Guide

Install kubernetes on Ubuntu

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Kubernetes is an open-source platform that manages containers like Docker. It is a management system that provides a base platform for deployment automation. With Kubernetes, you can freely make use of the hybrid, on-premise, and public cloud infrastructure to deploy your project. 

By using Kubernetes, developers can deploy applications easily across different environments for on-premises, cloud, or hybrid setups. It ensures high availability and scalability by distributing workloads dynamically. 

This guide will walk you through the easy steps to install Kubernetes on Ubuntu along with prerequisites, other important steps, and a troubleshooting guide. 

Prerequisites to Install Kubernetes on Ubuntu 22.04

Before you install Kubernetes on Ubuntu, make sure that your system meets the required settings: 

  • Ubuntu Version: Use Ubuntu 20.04 or 22.04 (older versions may require additional configurations).
  • Sufficient Resources: At least 2 CPU cores, 4GB RAM (8GB recommended for production), and 20GB of disk space.
  • Network Connectivity: Ensure internet access for package installation and cluster communication.
  • User Privileges: You must have sudo or root privileges.
  • Container Runtime: Kubernetes requires a container runtime like Docker or containerd.
  • Time Synchronization: Install ntp or chrony to keep system time in sync.

Prerequisites To Install Kubernetes on Ubuntu

Before you set up Kubernetes, you need to install all the essential packages and dependencies that includes: 

Update System Packages

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

sudo apt update && sudo apt upgrade -y

Install Required Packages

sudo apt install -y apt-transport-https curl gnupg2 software-properties-common

Install a Container Runtime for example containerd

sudo apt install -y containerd

Then configure containerd and enable it, using:
sudo systemctl enable –now containerd

Disabling Swap and Configuring Kernel Modules

Kubernetes requires swapping to be disabled for proper performance: 

Disable Swap Temporarily

sudo swapoff -a

Disable Swap Permanently

Edit the /etc/fstab file and remove any command lines containing swap:

sudo sed -i ‘/swap/d’ /etc/fstab

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

Load Kernel Modules Required by Kubernetes

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

overlay

br_netfilter

EOF

sudo modprobe overlay

sudo modprobe br_netfilter

Configure Sysctl Settings for Kubernetes Networking

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

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

net.ipv4.ip_forward = 1

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

EOF

Apply the changes:
sudo sysctl –system

Components To Install Kubernetes on Ubuntu (kubectl, kubeadm, and kubelet)

After setting up the prerequisites, you can install Kubernetes core components. 

Add the Kubernetes Repository

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo tee /etc/apt/trusted.gpg.d/kubernetes.asc

echo “deb https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /” | sudo tee /etc/apt/sources.list.d/kubernetes.list

Install Kubernetes Components

sudo apt update

sudo apt install -y kubelet kubeadm kubectl

sudo apt-mark hold kubelet kubeadm kubectl

Enable and Start kubelet

sudo systemctl enable –now kubelet

Initializing the Kubernetes Cluster

The first node in a Kubernetes cluster is the master node, known as the control panel. To initialize Kuberenetes, start by: 

Run kubeadm init

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

This command will initialize the cluster and provide a joining command for worker nodes. 

Set Up Kubeconfig for the Current User

mkdir -p $HOME/.kube

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

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

Verify the Cluster Status

kubectl get nodes

The control plane node should be in a Ready state.

Setting Up Networking for the Cluster

Kubernetes needs a Container Network Interface (CNI) for pod communication. You can install a CNI like Calico or Flannel. However, Calico is recommended. 

Install Calico (Recommended)

kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml

Verify Network Plugin Installation

kubectl get pods -n kube-system

Ensure all network-related pods are running.

Joining Worker Nodes to the Cluster

Once you have set up the control panel for Kubernetes, you can add worker nodes to expand the cluster. 

Get the Join Command from the Control Plane Node

If you did not save the join command from kubeadm init, you need to retrieve it using:

kubeadm token create –print-join-command

Run the Join Command on Each Worker Node

Copy the join command and execute it on each worker node by:
sudo kubeadm join <control-plane-ip>:6443 –token <token> –discovery-token-ca-cert-hash sha256:<hash>

You need to replace <control-plane-ip>, <token>, and <hash> with the actual values from the join command.

Verify That the Nodes Joined Successfully (Run on the Control Plane)

Run the following command on the Control Panel to verify that the nodes have joined successfully. 

kubectl get nodes

The worker nodes would appear in a Ready state after a couple of minutes. 

Verifying the Kubernetes Installation

After you install Kubernetes on Ubuntu, you should verify its installation. 

Check the Cluster Nodes

kubectl get nodes

The control plane and worker nodes should be in a Ready state.

Check System Pods

kubectl get pods -n kube-system

Ensure all necessary system pods (such as CoreDNS, kube-proxy, and network plugin) are running.

Deploy a Test Application

kubectl create deployment nginx –image=nginx

kubectl expose deployment nginx –type=NodePort –port=80

kubectl get services

Access the service using the node’s external IP and assigned port.

Managing Kubernetes with kubectl

The kubectl command-line tool allows you to interact with the cluster.

Basic Cluster Management

  • Check cluster information:

kubectl cluster-info

  • List running pods:

kubectl get pods

  • Describe a specific pod:

kubectl describe pod <pod-name>

Managing Deployments

  • Create a deployment:

kubectl create deployment my-app –image=my-image

  • Scale the deployment:

kubectl scale deployment my-app –replicas=3

  • Delete a deployment:

kubectl delete deployment my-app

Common Issues and Troubleshooting Guide To Install Kubernetes on Ubuntu

IssuePossible CauseSolution
kubeadm init failsInsufficient system resources, firewall issues, or swap enabledEnsure at least 2 CPUs, 2GB RAM, disable swap (sudo swapoff -a), and check firewall rules
Worker node fails to joinIncorrect join command, token expiredRe-run kubeadm token create –print-join-command on the control plane and use the updated command
Pods stuck in Pending stateNetwork plugin not installedInstall a CNI like Calico (kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml)
kubectl get nodes shows NotReadyNetwork issues, kubelet not runningRestart kubelet (sudo systemctl restart kubelet), check network settings
CoreDNS pods stuck in CrashLoopBackOffNetwork issues, missing kube-proxyEnsure kube-proxy is running: kubectl get pods -n kube-system, restart CoreDNS (kubectl rollout restart deployment coredns -n kube-system)
Service is unreachableIncorrect service type or firewall blocking portsUse kubectl get svc to check service type, ensure NodePort or LoadBalancer is set correctly
kubectl commands fail with permission errorsMissing or misconfigured kubeconfig fileRun export KUBECONFIG=/etc/kubernetes/admin.confand ensure the file exists
High resource usage on nodesToo many running pods or insufficient resourcesMonitor with kubectl top nodes and kubectl top pods, consider scaling cluster

Wrapping Up – Install Kubernetes On Ubuntu

Kubernetes offers intense flexibility and scalability, which is a must in the modern day applications. After following the steps in this guide carefully, you can easily install Kubernetes on Ubuntu and deploy containers seamlessly.

1. How do I disable swap for Kubernetes?

Run the following command:
sudo swapoff -a sudo sed -i '/ swap / s/^/#/' /etc/fstab
This disables swap permanently.

2. What networking plugin should I use with Kubernetes?

Popular choices include:
Calico (Network security and policy enforcement)
Flannel (Simple overlay network)
– Weave Net (Self-healing networking)

3. What should I do if kubeadm init fails?

– Check logs using journalctl -xeu kubelet
– Ensure swap is disabled
– Verify system requirements are met
– Restart the system and try again

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!