Kubernetes has become the de facto standard for container orchestration, but it can be complex and resource-intensive. K3s, a lightweight Kubernetes distribution, is an excellent alternative for developers, small-scale deployments, and edge computing. In this guide, we’ll walk you through the step-by-step process of installing K3s on Ubuntu 24.04.
Prerequisites
Before installing K3s, ensure you have:
- Ubuntu 24.04 LTS installed on your server.
- A user with sudo privileges.
- A stable internet connection.
Step 1: Update Your System
The first step is to update the package list and upgrade any outdated packages to ensure a stable environment.
sudo apt update && sudo apt upgrade -y
Step 2: Install K3s (Single Node)
K3s provides an easy installation script that sets up a lightweight Kubernetes cluster.
curl -sfL https://get.k3s.io | sh -
This command will:
- Download and install K3s.
- Start the K3s service automatically.
- Set up kubectl to interact with your cluster.
Once installed, verify the K3s service is running:
systemctl status k3s
If it’s running correctly, you should see output similar to this:
● k3s.service - Lightweight Kubernetes
Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)
Active: active (running)
Step 3: Verify the Kubernetes Cluster
To check if K3s is installed and running correctly, use the following command:
kubectl get nodes
You should see an output similar to:
NAME STATUS ROLES AGE VERSION
ubuntu Ready control-plane,master 2m v1.28.0+k3s1
This confirms that your Kubernetes control plane is up and running.
To list all running pods across all namespaces:
kubectl get pods -A
Step 4: Enable and Start K3s (If Needed)
If K3s is not running, you can manually enable and start the service:
sudo systemctl enable k3s
sudo systemctl start k3s
Step 5: Configure kubectl (Optional)
By default, kubectl
is configured to use K3s. However, if you need to run commands from a non-root user, set up your kubeconfig
file:
mkdir -p $HOME/.kube
sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
Now, test kubectl
again:
kubectl get nodes
Step 6: Installing K3s in a Multi-Node Cluster (Master + Worker Nodes)
If you want to set up a multi-node cluster with one master node and multiple worker nodes, follow these steps:
Step 6.1: Install K3s on the Master Node
Run the following command on the master node:
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode=644
Now, retrieve the node token from the master node:
sudo cat /var/lib/rancher/k3s/server/node-token
Copy the token, as you’ll need it for worker nodes.
Step 6.2: Install K3s on Worker Nodes
Run the following command on each worker node, replacing <MASTER_IP>
with the master node’s IP and <TOKEN>
with the token you copied:
curl -sfL https://get.k3s.io | K3S_URL="https://<MASTER_IP>:6443" K3S_TOKEN="<TOKEN>" sh -
After all worker nodes are added, verify the cluster:
kubectl get nodes
You should see the worker nodes listed along with the master.
Step 7: Uninstalling K3s (If Needed)
If you need to remove K3s, run the following command:
sudo /usr/local/bin/k3s-uninstall.sh
For worker nodes, use:
sudo /usr/local/bin/k3s-agent-uninstall.sh
Conclusion
Setting up K3s on Ubuntu 24.04 is a quick and efficient way to deploy Kubernetes without the overhead of a full-fledged K8s installation. Whether you’re using it for development, testing, or lightweight production environments, K3s provides a powerful yet simple solution for running containerized applications.
If you found this guide helpful, share it with others and subscribe for more DevOps and Kubernetes tutorials! 🚀