Mastering Kubernetes Setup on Ubuntu: A Step-by-Step Guide

Mastering Kubernetes Setup on Ubuntu: A Step-by-Step Guide


Kubernetes, developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF), has become the go-to solution for container orchestration, streamlining application deployment, scaling, and management. This guide will walk you through the steps to set up Kubernetes on an Ubuntu server, specifically tailored for environments like EC2 instances.

Housekeeping Before Kubernetes Installation:

Before diving into the Kubernetes setup, some preliminary steps are necessary to prepare your Ubuntu system.

  1. Change of Hostname:

To begin, it’s crucial to set a descriptive hostname for your node that reflects its role within your Kubernetes cluster.

sudo hostnamectl set-hostname your-desired-hostname

To make that change without closing your terminal:

exec bash

  1. Update the /etc/hosts File:

Next, ensure your system can resolve its hostname by updating the /etc/hosts file with the IP address and the new hostname.

sudo vi /etc/hosts

# Add a line: <IP-Address> your-desired-hostname

  1. Disable Swap:

Kubernetes requires swap to be disabled to function correctly, as swap space usage can affect scheduling decisions.

sudo swapoff -a

To keep this change consistent even after a reboot, you need to modify the fstb file.

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

  1. Update and Upgrade System:

Ensure your system is up-to-date with all its packages before proceeding with the Kubernetes setup.

sudo apt-get update && sudo apt-get upgrade -y

  1. Set up IP Bridge for nodes to communicate over the network:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf

sudo modprobe overlay
sudo modprobe br_netfilter

Parameters required by sysctl:

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

To apply changes without reboot:

sudo sysctl --system

With the housekeeping out of the way, let’s move on to installing Kubernetes components:

To initiate the creation of a Kubernetes cluster, one must install the kubelet, kubeadm, and kubectl software components on each node. These components are crucial for managing the cluster.

The kubelet, acting as the node’s agent, operates on each node to manage the containers within a Pod, ensuring they function according to the Pod’s specifications. Pods are the smallest deployable units in a Kubernetes cluster.

The installation of kubeadm is necessary for initializing the Kubernetes cluster, preparing the master node, and facilitating the addition of worker nodes.

Kubectl, the command-line interface tool for Kubernetes, allows users to execute commands for deploying applications, examining resources, and managing cluster operations through interaction with the cluster’s API server.

Before installing these components, it is critical to refresh the package index with the sudo apt-get update command to ensure the latest versions are available.

Following this, establishing a secure environment for the safe downloading and installation of packages from the internet is imperative:

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

If the setup is incomplete, the subsequent action is to establish a directory specifically for housing a distinctive key. This key is essential for verifying the legitimacy of Kubernetes packages, akin to the verification of an ID card prior to allowing access to a secured area.

sudo mkdir /etc/apt/keyrings

Next, we’ll retrieve the public key from Google and place it in the directory we established earlier. This key is vital for ensuring the Kubernetes packages we’re downloading are authentic and have remained unaltered.

curl -fsSL | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg

Following this, it’s necessary to configure the apt package manager with the location of the Kubernetes packages to enable their download.

echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

We should then update the apt package index to incorporate new entries by executing the sudo apt-get update command once more.

With the setup complete, we’re now prepared to install kubelet, kubeadm, and kubectl. For stability and compatibility reasons, we’ll specify version 1.26.5-00 for each by using the command: sudo apt install -y kubelet=1.26.5-00 kubeadm=1.26.5-00 kubectl=1.26.5-00. This ensures we work with a version tested for reliability. If you’re open to using newer versions, simply omit the version numbers in the command.

The command: sudo apt-mark hold kubelet kubeadm kubectlis used to prevent these specific packages from being automatically updated when using apt-get upgrade or a similar command to update other packages on the system. By marking these packages as “hold,” you ensure that your Kubernetes environment remains stable and consistent, avoiding potential compatibility issues or disruptions that could arise from unexpected updates to these critical components. This practice is particularly important in environments where specific versions of kubelet, kubeadm, and kubectl have been tested and verified to work well together, ensuring the cluster’s reliability and performance.

Docker Time: Install Docker, because Kubernetes loves its containers:

Docker offers a comprehensive platform for constructing, distributing, and running applications within containers. These containers provide a consistent and portable environment, ensuring consistent performance across diverse setups. Serving as the foundational container runtime for Kubernetes, Docker plays a crucial role in facilitating the efficient orchestration and deployment of containerized applications, streamlining their management across different environments.

To install Docker, use the command:

sudo apt install

Subsequently, it’s crucial to set up containerd across all nodes to guarantee its alignment with Kubernetes requirements. Begin by generating a directory for the configuration file using the command:

sudo mkdir /etc/containerd

This step ensures that the container runtime environment is properly configured for seamless integration with Kubernetes.

Next, proceed to generate a default configuration file for containerd. This can be accomplished by executing the command:

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

This action creates the config.toml file within the previously established directory, setting up containerd with a standard configuration suitable for further customization.

Following the execution of these commands, it’s necessary to adjust the `config.toml` file by finding the line where `”SystemdCgroup“` is set to false and altering this value to true. This adjustment is crucial as Kubernetes relies on all its components, including the container runtime, to utilize systemd for control groups (cgroups). This ensures that Kubernetes can efficiently manage system resources for containers.

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

Then, to apply the modifications you’ve made, restart the containerd and kubelet services on all nodes. This ensures that the changes take effect and that both services are running with the updated configurations, maintaining the smooth operation of your Kubernetes environment.

sudo systemctl restart containerd.service && sudo systemctl restart kubelet.service

To ensure the kubelet service automatically starts up with the machine, you should enable it to run at boot time. This can be achieved by executing the command:

sudo systemctl enable kubelet.service

This step is critical for maintaining the operational readiness of your Kubernetes cluster immediately following a system restart.

Initialize the Kubernetes cluster on the master node:

Initiating the Kubernetes control plane with kubeadm results in the deployment of several key components that are essential for the cluster’s management and orchestration. Among these components are the kube-apiserver, kube-controller-manager, kube-scheduler, etcd, and kube-proxy. To ensure these components are available for your cluster, it’s necessary to download their images. This is accomplished by executing the command:

sudo kubeadm config images pull

This step is crucial for preparing the environment for the successful initialization of your Kubernetes cluster

Now we are ready to initialize the master node of your Kubernetes cluster. Utilize the –pod-network-cidr flag during this process to designate the IP address range for the pod network. This setting is critical for network communication within the cluster, ensuring that pods can interact with each other efficiently.

sudo kubeadm init --pod-network-cidr=

To proficiently manage your cluster, initializing kubectl on the master node is critical. Start with the creation of a .kube directory in your home directory. Next, move the cluster’s admin configuration file to your .kube directory. Afterward, modify the file’s ownership to ensure you have the appropriate rights to use this configuration for interacting with the cluster. These steps equip you with the capabilities needed for smooth navigation and control of your Kubernetes setup.

mkdir -p $HOME/.kube

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

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

In the screenshot above, one can also see the kubeadm join command. It facilitates the expansion of the cluster to include additional nodes. Please note the command.

Crucial Configuration: Choosing the Right Network Plugin:

Configuring the network plugin is a pivotal step in setting up a Kubernetes cluster, one that many find challenging. This stage is essential for ensuring your cluster’s pods can communicate efficiently. While there are several network plugins available, such as WaveNet, Flannel, and Cilium, I’ll guide you through setting up Calico. My choice of Calico is based on personal experience and familiarity, having worked with it extensively in the past. However, it’s important to explore and understand the wide array of network plugins available to find the one that best fits your cluster’s needs.

Execute this command on the master node to initiate the deployment of the Calico operator:

kubectl create -f

Proceed to download the Calico custom resources file, which encompasses the definitions for the diverse resources Calico employs:

curl -O

The -O option in the curl command instructs curl to download files while retaining their original names as specified on the web server. For instance, when downloading custom-resources.yaml using this flag, curl saves it in the current directory under the same name, custom-resources.yaml. This feature simplifies file management, making it straightforward to identify and use the file, particularly for specific configurations or scripts.

Lastly, instruct kubectl to establish the resources outlined in the custom-resources.yaml file by executing the command:

kubectl create -f custom-resources.yaml

Adding worker nodes to the cluster:

After setting up the master node, you can proceed to incorporate worker nodes into the cluster. During the master node’s initialization with kubeadm, a token is generated, which is essential for linking worker nodes to the cluster.

To integrate the worker nodes into the Kubernetes cluster, employ the kubeadm join command. This command should be constructed with specific variable values obtained from the initial setup steps.

If you didn’t note down the token or it has expired, you can generate a new token and retrieve the full join command for the worker nodes by executing:

kubeadm token create --print-join-command

This command not only creates a new token but also outputs the complete command necessary for worker nodes to join the cluster, including the token and the discovery-token-ca-cert-hash required for secure connection to the master node.

To confirm the successful creation of our cluster, execute the command:

kubectl get nodes

to display all nodes within the cluster, providing a snapshot of its structure.


Setting up Kubernetes on Ubuntu involves meticulous preparation and configuration to ensure a seamless and robust container orchestration environment. From initial system housekeeping tasks like hostname changes and disabling swap to the critical steps of installing Docker and Kubernetes components, every stage is crucial for a successful setup. The guide has walked you through expanding your cluster with kubeadm join, configuring network plugins like Calico, and managing file downloads with curl. Embracing these practices will empower you to deploy a scalable, efficient Kubernetes cluster, ready to handle your containerized applications with ease and reliability. This journey into Kubernetes not only enhances your infrastructure but also prepares you for the dynamic demands of modern software development.

🔥Subscribe to the channel:🔥

🚨Read my blog:

👊My Podcast:

Please leave the comment