How to Install Kubernetes Using K3s On Ubuntu 22.04

Easily set up a lightweight Kubernetes cluster on Ubuntu 22.04 using K3s, simplifying the deployment of containerized applications with minimal resources.

Kubernetes, the de facto standard for container orchestration, is widely adopted for managing containerized applications. However, its full-fledged setup can be resource-intensive. Enter K3s, a lightweight Kubernetes distribution designed for resource-constrained environments, making it ideal for edge computing, IoT devices, and even development setups. In this guide, we'll walk you through installing K3s on Ubuntu 22.04, providing you with a streamlined Kubernetes experience.

Table of Contents

  1. Prerequisites
  2. Updating Your System
  3. Installing K3s
  4. Configuring K3s
  5. Deploying a Sample Application
  6. Managing Your Cluster
  7. Troubleshooting Tips
  8. Conclusion

1. Prerequisites

Before diving into the installation, ensure your system meets the following prerequisites:

  • An Ubuntu 22.04 server (minimum 2GB RAM and 2 CPU cores recommended)
  • Root or sudo access to the server
  • Basic knowledge of Linux command line

2. Updating Your System

First, ensure your system is up to date. Open your terminal and run the following commands:

sudo apt update sudo apt upgrade -y

This will update your package list and upgrade all your installed packages to their latest versions.

3. Installing K3s

K3s simplifies the Kubernetes installation process significantly. To install K3s, follow these steps:

  1. Download and Install K3s:

    Use the official installation script provided by Rancher Labs:

    curl -sfL https://get.k3s.io | sh -

    This script will download and install K3s, start the K3s service, and set up your cluster.

  2. Verify Installation:

    After the installation script completes, verify the K3s service status:

    sudo systemctl status k3s

    You should see output indicating that K3s is active and running.

4. Configuring K3s

K3s is designed to be easy to use out-of-the-box, but you might need to customize certain configurations:

  1. Accessing K3s Configuration:

    The K3s configuration file is located at /etc/rancher/k3s/k3s.yaml. To access your cluster using kubectl, copy this file to your home directory and set the KUBECONFIG environment variable:

    mkdir -p ~/.kube sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config sudo chown $(id -u):$(id -g) ~/.kube/config export KUBECONFIG=~/.kube/config

  2. Installing kubectl:

    If kubectl is not installed on your system, install it using the following commands:

    sudo snap install kubectl --classic

  3. Verify Cluster Access:

    Check the nodes in your cluster to ensure everything is working correctly:

    kubectl get nodes

    You should see your node listed with a Ready status.

5. Deploying a Sample Application

To verify that your K3s setup is functioning correctly, let's deploy a simple Nginx application:

  1. Create a Deployment:

    kubectl create deployment nginx --image=nginx

  2. Expose the Deployment:

    Create a service to expose the Nginx deployment:

    kubectl expose deployment nginx --type=LoadBalancer --port=80

  3. Verify the Deployment:

    Check the status of your pods and services:

    kubectl get pods kubectl get services

    You should see your Nginx pod running and a service with an external IP address.

6. Managing Your Cluster

K3s provides several utilities for managing your cluster:

  1. Helm:

    Helm is a package manager for Kubernetes. Install Helm using the following script:

    curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

  2. K3s Upgrades:

    K3s can be upgraded easily by rerunning the installation script:

    curl -sfL https://get.k3s.io | sh -

  3. Backup and Restore:

    Backup your K3s data directory regularly. The data directory is typically located at /var/lib/rancher/k3s.

    To backup, you can simply copy this directory to a safe location:

    sudo cp -r /var/lib/rancher/k3s /path/to/backup/

    To restore, stop the K3s service, replace the data directory with your backup, and start the service again:

    sudo systemctl stop k3s sudo cp -r /path/to/backup/k3s /var/lib/rancher/ sudo systemctl start k3s

7. Troubleshooting Tips

  1. Check Logs:

    If you encounter issues, check the logs for clues:

    sudo journalctl -u k3s

  2. Network Issues:

    Ensure your firewall rules allow traffic for the required ports (6443, 2379-2380, 10250-10252, 30000-32767).

  3. Pod Issues:

    If pods are not starting, check their logs:

    kubectl logs <pod-name>

8. Conclusion

Installing K3s on Ubuntu 22.04 provides a lightweight, efficient Kubernetes solution perfect for development, testing, and edge environments. By following this guide, you've set up a Kubernetes cluster, deployed a sample application, and learned essential management techniques. K3s simplifies Kubernetes deployment without sacrificing functionality, making it a great choice for various use cases.

For further reading and advanced configurations, refer to the official K3s documentation.

By following these steps, you'll have a fully functional K3s-based Kubernetes cluster up and running on your Ubuntu 22.04 server. Whether you're exploring Kubernetes for the first time or looking for a more resource-efficient setup, K3s offers a powerful and simplified approach to container orchestration.

References: