How to Install Kubernetes on Ubuntu

Install Kubernetes on Ubuntu

Setting up a Kubernetes cluster might seem like climbing Mount Everest, but don’t worry – I’m here to guide you through every step of the journey. Whether you’re a DevOps engineer looking to orchestrate containers or a developer wanting to scale applications seamlessly, this comprehensive guide will walk you through installing Kubernetes on Ubuntu from start to finish.

Kubernetes has revolutionized how we deploy and manage containerized applications, and Ubuntu remains one of the most popular platforms for hosting these clusters. By the end of this tutorial, you’ll have a fully functional Kubernetes cluster running on your Ubuntu system, ready to handle your containerized workloads like a pro.

What is Kubernetes and Why Use It?

Think of Kubernetes as the conductor of an orchestra, where each musician represents a container. Just as a conductor ensures all musicians play in harmony, Kubernetes orchestrates your containers to work together seamlessly. It’s an open-source platform that automates the deployment, scaling, and management of containerized applications.

Originally developed by Google, Kubernetes has become the gold standard for container orchestration. It handles the heavy lifting of managing distributed systems, including:

  • Automatic scaling: Your applications grow and shrink based on demand
  • Self-healing: Failed containers get replaced automatically
  • Load balancing: Traffic gets distributed evenly across your applications
  • Rolling updates: Deploy new versions without downtime
  • Resource management: Efficient allocation of CPU and memory

The beauty of Kubernetes lies in its ability to abstract away the complexity of managing multiple servers, letting you focus on building great applications instead of worrying about infrastructure.

Prerequisites and System Requirements

Before we dive into the installation process, let’s make sure your system meets the necessary requirements. Installing Kubernetes without proper preparation is like trying to bake a cake without checking if you have all the ingredients first.

Hardware Requirements

Your Ubuntu system needs adequate resources to run Kubernetes smoothly. Here’s what you’ll need for each node in your cluster:

  • RAM: Minimum 2 GB per node (4 GB recommended for production)
  • CPU: At least 2 CPU cores or more
  • Storage: 20 GB of free disk space minimum
  • Network: Stable internet connection for initial setup

These specifications provide enough overhead for running kubeadm while leaving resources for your deployed applications. Remember, if you’re planning to run multiple applications or scale significantly, you’ll need to adjust these requirements accordingly.

Software Requirements

You’ll need a clean Ubuntu installation with:

  • Ubuntu 20.04, 22.04, or 24.04 LTS (recommended)
  • Root or sudo access
  • SSH access if working with remote servers

Network Requirements

For multi-node clusters, ensure:

  • Network connectivity between all nodes
  • Unique hostname and MAC address for every node
  • Open ports for Kubernetes services (we’ll configure these later)

Preparing Your Ubuntu System

Let’s start by preparing your Ubuntu system for Kubernetes installation. This preparation phase is crucial – it’s like laying a solid foundation before building a house.

Updating Your System

First things first, let’s update your system packages:

sudo apt update && sudo apt upgrade -y

This ensures you have the latest security patches and package versions.

Setting Up Hostnames

If you’re setting up a multi-node cluster, you need unique hostnames for each node. This step ensures smooth communication between your master and worker nodes.

Edit the /etc/hosts file:

sudo nano /etc/hosts

Add entries for each node in your cluster:

192.168.1.10 k8s-master-node
192.168.1.11 k8s-worker-node-1
192.168.1.12 k8s-worker-node-2

Set the hostname for each node:

For the master node:

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

For worker nodes:

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

Apply the changes:

sudo exec bash

Disabling Swap Space

Kubernetes and swap don’t play well together. Swap can interfere with Kubernetes’ scheduling decisions, so we need to disable it.

Temporarily disable swap:

sudo swapoff -a

Make the change permanent by editing the fstab file:

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

This command comments out any swap entries in your fstab file, ensuring swap remains disabled after reboots.

Installing Container Runtime

Kubernetes needs a container runtime to actually run containers. We’ll use containerd, which is the recommended choice for modern Kubernetes installations.

Installing containerd

Install containerd using Ubuntu’s package manager:

sudo apt install -y containerd

Configuring containerd

Create the containerd configuration directory and generate the default configuration:

sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml

For better integration with Kubernetes, we need to enable systemd as the cgroup driver. Edit the configuration file:

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

Restart and enable containerd:

sudo systemctl restart containerd
sudo systemctl enable containerd

Verify containerd is running:

sudo systemctl status containerd

Installing Kubernetes Components

Now for the main event – installing Kubernetes! We’ll install three essential components that work together like a well-oiled machine.

Adding Kubernetes Repository

Since Kubernetes isn’t available in Ubuntu’s default repositories, we need to add the official Kubernetes repository.

First, install required packages:

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

Create a directory for the signing key:

sudo mkdir -p /etc/apt/keyrings

Download and add the Kubernetes signing key:

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

Add the Kubernetes repository:

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

Update the package index:

sudo apt update

Installing kubeadm, kubelet, and kubectl

Now let’s install the three musketeers of Kubernetes:

  • kubeadm: Bootstraps your Kubernetes cluster
  • kubelet: The node agent that manages containers
  • kubectl: Your command-line interface for cluster management

Install all three components:

sudo apt install -y kubeadm kubelet kubectl

Holding Package Versions

To prevent automatic updates that might break your cluster:

sudo apt-mark hold kubeadm kubelet kubectl

Verify the installation:

kubeadm version

Configuring Kubernetes Networking

Kubernetes requires specific networking configurations to function properly. Think of this as setting up the highways that allow your containers to communicate.

Loading Required Modules

Create a configuration file for containerd modules:

sudo nano /etc/modules-load.d/containerd.conf

Add these lines:

overlay
br_netfilter

Load the modules:

sudo modprobe overlay
sudo modprobe br_netfilter

Setting Up IPv4 Bridge Configuration

Create a Kubernetes networking configuration file:

sudo nano /etc/sysctl.d/kubernetes.conf

Add the following networking parameters:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

Apply the configuration:

sudo sysctl --system

Initializing Your Kubernetes Cluster

Time to bring your cluster to life! This is where the magic happens.

Creating the Master Node

Initialize your Kubernetes cluster on the master node:

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

The --pod-network-cidr flag specifies the IP range for pods. We’re using the Flannel default range.

After successful initialization, you’ll see output containing a kubeadm join command. Save this command – you’ll need it to add worker nodes later.

Setting Up kubectl Access

Configure kubectl for your regular user account:

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

Test your configuration:

kubectl get nodes

Setting Up Pod Network

Your cluster needs a pod network to enable communication between pods across different nodes.

Installing Flannel Network Add-on

Apply the Flannel network configuration:

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

For single-node clusters, remove the taint from the master node:

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

Adding Worker Nodes to Your Cluster

If you’re setting up a multi-node cluster, run the kubeadm join command (saved from the cluster initialization) on each worker node:

sudo kubeadm join [master-node-ip]:6443 --token [token] --discovery-token-ca-cert-hash sha256:[hash]

On some systems, you might need to stop AppArmor on worker nodes:

sudo systemctl stop apparmor && sudo systemctl disable apparmor
sudo systemctl restart containerd.service

Verifying Your Installation

Let’s make sure everything is working correctly:

kubectl get nodes
kubectl get pods --all-namespaces

You should see all nodes in “Ready” status and all system pods running.

Common Troubleshooting Issues

Even the best-laid plans can hit snags. Here are common issues and their solutions:

Swap not disabled: Ensure swap is completely disabled with sudo swapoff -a and check /etc/fstab.

Container runtime issues: Restart containerd with sudo systemctl restart containerd.

Network plugin problems: Reapply the Flannel configuration or check pod network CIDR settings.

Node not ready: Check kubelet status with sudo systemctl status kubelet.

Best Practices for Kubernetes on Ubuntu

To keep your cluster running smoothly:

  • Regular backups: Backup your etcd data regularly
  • Monitor resources: Keep an eye on CPU and memory usage
  • Security updates: Plan for regular security patches
  • Resource limits: Set appropriate resource limits for pods
  • Logging: Implement centralized logging from the start

Frequently Asked Questions (FAQs)

1. What’s the minimum hardware required to run Kubernetes on Ubuntu?
You need at least 2 GB RAM, 2 CPU cores, and 20 GB disk space per node. However, 4 GB RAM is recommended for production environments to ensure smooth operation.

2. Can I install Kubernetes on a single Ubuntu machine?
Yes! You can run a single-node cluster for development and testing. Just remove the taint from the master node using kubectl taint nodes --all node-role.kubernetes.io/control-plane- to allow pod scheduling.

3. Which Ubuntu version is best for Kubernetes?
Ubuntu 22.04 LTS is currently the most stable and widely supported version for Kubernetes installations. Ubuntu 20.04 and 24.04 are also supported.

4. How do I update Kubernetes after installation?
Since we held the packages with apt-mark hold, you’ll need to unhold them first: sudo apt-mark unhold kubeadm kubelet kubectl, update the packages, then hold them again. Always check the Kubernetes upgrade documentation for version-specific instructions.

5. What should I do if my pods are stuck in “Pending” status?
This usually indicates resource constraints or scheduling issues. Check available resources with kubectl describe nodes and ensure your pod network is properly configured. Also verify that worker nodes are properly joined to the cluster.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts