Setting Up a Lightweight Kubernetes Cluster with K3s

Photo by Growtika on Unsplash

Intro

K3s is a highly available, certified Kubernetes distribution designed for production workloads in unattended environments, making it a top-tier choice for self-managed Kubernetes. Packaged as a single binary of less than 70MB, K3s is also well-suited for edge computing and IoT applications.

The goal of this post is to create a Kubernetes cluster using the default K3s configuration. For a more advanced setup with improved reliability, see my guide on Scaling K3s: Boost High Availability of K3s.

Although I am using a VM with Rocky Linux (a Red Hat variant), the important commands in this guide are applicable to most Linux distributions.

Implementation Steps

  • Turn off the firewall
    K3s requires several open ports to function. To simplify the initial setup, we will temporarily disable the firewall. After the installation is complete, you can re-enable it with the necessary rules. Note that this command may differ depending on your Linux distribution.
systemctl disable firewalld --now
  • Install k3s on the master node
    Execute the official installation script to set up the K3s server on your master node.
curl -sfL https://get.k3s.io | sh -
  • Give sudo access to kubectl
    To manage the cluster without requiring sudo for every command, follow these steps to copy the K3s configuration file to your user's directory and set the correct permissions.
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown k3s:k3s ~/.kube/config
echo 'export KUBECONFIG=$HOME/.kube/config' >> ~/.bashrc
source ~/.bashrc

#test kubectl
kubectl get ns
  • Get token from Master Node
    You'll need a secret token to allow worker nodes to join the cluster. Retrieve this token from the master node.
sudo cat /var/lib/rancher/k3s/server/node-token
  • Install K3s on the Agent (Worker) Node
    On each agent node, run the installation script with special environment variables to connect it to the master. Replace , , and with your actual values.
curl -sfL https://get.k3s.io | K3S_URL=https://<MASTER IP>:6443 K3S_TOKEN="<TOKEN>" INSTALL_K3S_EXEC="--node-ip=<NODE IP>" sh -
  • Verify the Cluster
    Finally, return to your master node and verify that all agent nodes have successfully joined the cluster.
kubectl get node

You should see all your master and agent nodes listed with a Ready status.

About the Author

Data & Platform Engineer with 3+ years of experience in cloud, DevOps, and data solutions. I love turning complex data into clear, actionable insights and am passionate about building efficient, scalable systems, especially with Kubernetes (which even runs this blog!).

Let's connect on Linkedin