In this tutorial, learn how to install and configure Kubernetes (K8s) on Ubuntu 24.04 with this step-by-step guide. Ensure seamless Kubernetes setup with Docker, kubeadm, and kubectl.
Kubernetes (K8s) has become the leading platform for container orchestration, enabling developers to deploy, manage, and scale applications with ease. As more organizations adopt microservices architectures, mastering Kubernetes is essential for efficient DevOps workflows.
Whether you're a beginner looking to get started with Kubernetes or an experienced developer seeking a reliable reference, this tutorial covers everything from installing Docker, the necessary container runtime, to configuring kubeadm, kubelet, and kubectl.
Install Kubernetes K8s on Ubuntu
Prerequisites
- Ubuntu 24.04 LTS installed on all machines (1 master node and at least 1 worker node).
- Root or sudo access to the machines.
- At least 2GB of RAM per machine (4GB recommended for the master node).
Step 1: Update and Upgrade the System
Start by updating the package list and upgrading the installed packages to the latest versions.
sudo apt update && sudo apt upgrade -y
Step 2: Install Docker
Kubernetes uses Docker as its container runtime. Install Docker on all nodes (master and worker nodes).
sudo apt install -y docker.io
Verify that Docker is installed correctly.
sudo docker --version
Output:
Docker version 24.0.7, build 24.0.7-0ubuntu4
Step 3: Install Kubernetes Components
Install the Kubernetes components: kubeadm, kubelet, and kubectl
on all nodes.
Add the Kubernetes apt 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
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Note: Replace v1.30 with latest version. Check the official release page.
Install the Kubernetes components:
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
Hold the packages at their current version to prevent automatic upgrades:
sudo apt-mark hold kubelet kubeadm kubectl
Step 4: Disable Swap
Kubernetes requires swap to be disabled. Disable swap on all nodes.
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Step 5: Initialize the Master Node
On the master node
, initialize the Kubernetes cluster with kubeadm
.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
After initialization, you'll see a join command. Copy this command as it will be used to join worker nodes to the cluster.
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:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join <master-node-ip>:6443 --token kwngvy.wl6ck69yg62an0ll \
--discovery-token-ca-cert-hash sha256:355e0aa517177aa11ad3fd8293babfc194b0a4771c588d3502cdbe1bf49535c0
Copy the last command starts with kubeadm join
, we need to join worker nodes. This command need to run in worker nodes.
Step 6: Configure kubectl for the Master Node
Set up the kubeconfig file for the root user on the master node.
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
Step 7: Install a Pod Network Add-on on Master node
Install a pod network so that your pods can communicate with each other. We'll use Flannel for this example.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Verify that all nodes are up and running.
kubectl get nodes
Output:
NAME STATUS ROLES AGE VERSION
docker NotReady control-plane 71s v1.30.1
Step 8: Join Worker Nodes to the Cluster
On each worker node
, use the join command obtained from the master node initialization step. The command will look something like this:
sudo kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Verify that the nodes have joined the cluster. Execute following command in master node.
kubectl get nodes
Output:
NAME STATUS ROLES AGE VERSION
docker Ready control-plane 10m v1.30.1
worker NotReady <none> 16s v1.30.1
Step 9: (Optional) Configure kubectl for a Non-Root User
If you want to configure kubectl for a non-root user, run the following commands:
sudo -i
mkdir -p /home/<your-username>/.kube
cp -i /etc/kubernetes/admin.conf /home/<your-username>/.kube/config
chown <your-username>:<your-username> /home/<your-username>/.kube/config
exit
Step 10: Deploy a Test Application
Deploy a simple Nginx application to verify that your Kubernetes cluster is working correctly.
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
(Optional) Disable the firewall or add ports in firewall
In this step, we can disable the firewall for temporary or we can add the node port and Nginx ports in firewall
Get the NodePort assigned to the Nginx service.
kubectl get svc
Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11m
nginx NodePort 10.102.78.125 <none> 80:30711/TCP 7s
You should be able to access the Nginx application by visiting http://<node-ip>:<node-port>
in your web browser.
Note: Replace <node-ip>
with your worker node and you can get the <node-port>
. In the previous command output, the node port is 30711
.
Conclusion
We've learnt how to install and configure Kubernetes (K8s) on Ubuntu 24.04 You can scale your cluster by adding more worker nodes, deploy more complex applications, and explore the vast ecosystem of Kubernetes tools and add-ons. For production environments, consider implementing high availability, persistent storage, and other best practices for Kubernetes clusters.