How to Install Kubernetes Cluster Using K3s on RHEL 9 | 8

Learn to effortlessly set up a lightweight Kubernetes cluster on RHEL 9 | 8 using K3s, perfect for resource-constrained environments and edge computing.

Kubernetes has become the industry standard for orchestrating containerized applications. However, its installation and resource requirements can be daunting. K3s, a lightweight Kubernetes distribution by Rancher Labs, addresses these challenges by offering a simplified and resource-efficient setup, ideal for edge computing and development environments. This guide will walk you through installing K3s on Red Hat Enterprise Linux (RHEL) 9 and 8, enabling you to deploy and manage your Kubernetes clusters with ease.

Table of Contents

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

1. Prerequisites

Before you begin, ensure you have the following:

  • A RHEL 9 or 8 server with at least 2GB of RAM and 2 CPU cores.
  • Root or sudo access to the server.
  • Basic understanding of Linux command-line operations.

2. Preparing Your System

Start by updating your system packages and installing necessary dependencies:

sudo dnf update -y sudo dnf install -y curl iptables

Ensure your firewall settings allow necessary ports for Kubernetes to function properly:

sudo firewall-cmd --add-port=6443/tcp --permanent sudo firewall-cmd --add-port=2379-2380/tcp --permanent sudo firewall-cmd --add-port=10250-10252/tcp --permanent sudo firewall-cmd --add-port=30000-32767/tcp --permanent sudo firewall-cmd --reload

3. Installing K3s

K3s can be installed easily using an installation script provided by Rancher Labs. Follow these steps to install K3s on your RHEL server:

  1. Download and Install K3s:

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

    This script downloads and installs K3s, starts the K3s service, and sets up a single-node Kubernetes cluster.

  2. Verify Installation:

    Check the status of the K3s service to ensure it is running properly:

    sudo systemctl status k3s

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

4. Configuring K3s

Once K3s is installed, you may need to perform some basic configurations to get your cluster ready for use:

  1. Accessing K3s Configuration:

    The K3s configuration file is located at /etc/rancher/k3s/k3s.yaml. To use kubectl to manage your cluster, 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 already installed on your system, you can install it using the following command:

    sudo dnf install -y kubernetes-client

  3. Verify Cluster Access:

    Test your cluster access by listing the nodes:

    kubectl get nodes

    You should see your node listed with a Ready status.

5. Deploying a Sample Application

To ensure your K3s setup is functioning correctly, 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. Cluster Management

Managing your K3s cluster involves using various tools and performing regular maintenance tasks:

  1. Helm:

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

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

  2. K3s Upgrades:

    Upgrading K3s is straightforward. Simply rerun the installation script:

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

  3. Backup and Restore:

    Regularly back up your K3s data directory, typically located at /var/lib/rancher/k3s. Use the following command to create a backup:

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

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

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

7. Troubleshooting Tips

Here are some common troubleshooting tips to help resolve issues you might encounter:

  1. Check Logs:

    If you encounter issues, check the logs for more details:

    sudo journalctl -u k3s

  2. Network Issues:

    Ensure your firewall settings allow the necessary ports and your network configuration is correct.

  3. Pod Issues:

    If your pods are not starting, check their logs for error messages:

    kubectl logs <pod-name>

  4. Node Issues:

    If nodes are not showing as Ready, check the status and logs of the K3s service on those nodes.

8. Conclusion

By following this guide, you have successfully installed K3s on RHEL 9 | 8, set up a Kubernetes cluster, and deployed a sample application. K3s offers a simplified and lightweight Kubernetes distribution, making it an excellent choice for development, testing, and production environments with limited resources. For more advanced configurations and further reading, refer to the official K3s documentation.

References:

By following these steps, you'll have a fully functional K3s-based Kubernetes cluster up and running on your RHEL 9 | 8 server. Whether you're exploring Kubernetes for the first time or seeking a resource-efficient setup, K3s provides a robust solution for container orchestration in various environments.