๐Ÿชถ K3S Cheat-Sheet

K3S is a lightweight Kubernetes distribution designed for IoT and edge computing environments. Itโ€™s production-ready and easy to install with a binary size under 100 MB, consuming less memory compared to traditional Kubernetes setups.


๐Ÿ” Overview

  • Product Type: Lightweight Kubernetes Distribution

  • Focus: K3S is tailored for environments that need a small footprint and minimal resource consumption, such as IoT, edge computing, and small clusters.

    • Single-Node Setup: Ideal for development and testing

    • Multi-Node Setup: High Availability (HA) configurations with embedded or external databases

    • Ease of Installation: Simple and quick installation with automated scripts


๐Ÿ”ง Installation

You can install K3S using several methods: with an external database, embedded database, or as a single-node setup.

๐Ÿ“Š K3s with External DB

Set up a High Availability (HA) K3s cluster backed by an external database like MySQL, PostgreSQL, or etcd.

๐Ÿ—ƒ๏ธ Install Database

  • First, install MariaDB or your preferred database.

๐Ÿ–ฅ๏ธ Install Servers

To install K3s with an external database:

curl -sfL https://get.k3s.io | sh -s - server \
--token=YOUR-SECRET \
--datastore-endpoint='mysql://user:pass@tcp(ipaddress:3306)/dbname' \
--node-taint CriticalAddonsOnly=true:NoExecute \
--tls-san your-dns-name --tls-san your-lb-ip-address

Info

The --node-taint flag ensures that your server node wonโ€™t run workloads, only the control plane.

๐Ÿ”’ SSL Certificates

To avoid certificate errors, you should use the --tls-san YOUR_IP_OR_HOSTNAME_HERE option to add extra IPs or hostnames to the TLS certificate.

๐Ÿš€ Install Agents

You can install agents on additional nodes:

curl -sfL https://get.k3s.io | sh -s - agent \
--server https://your-lb-ip-address:6443 \
--token YOUR-SECRET

๐Ÿ’พ K3s with Embedded DB

Set up an HA K3s cluster using K3sโ€™s built-in distributed database (etcd).

๐Ÿ–ฅ๏ธ Install the First Server

curl -sfL https://get.k3s.io | sh -s - server \
--token=YOUR-SECRET \
--tls-san your-dns-name --tls-san your-lb-ip-address \
--cluster-init

Tip

The --tls-san option ensures no SSL certificate errors when accessing the server via IP or hostname.

๐Ÿ—๏ธ Install Additional Servers

To add more servers:

curl -sfL https://get.k3s.io | sh -s - server \
--token=YOUR-SECRET \
--tls-san your-dns-name --tls-san your-lb-ip-address \
--server https://IP-OF-THE-FIRST-SERVER:6443

The --cluster-init option initializes the HA cluster with an embedded etcd database. You need at least 3 nodes for fault tolerance.

๐Ÿ—“๏ธ Fault Tolerance Table

Total Number of NodesFailed Node Tolerance
10
20
31
41
52
62

๐Ÿ–ฅ๏ธ Install Agents

To install agent nodes:

curl -sfL https://get.k3s.io | sh -s - agent \
--server https://your-lb-ip-address:6443 \
--token YOUR-SECRET

โš™๏ธ K3s Single Node

Set up K3s as a single-node installation.

Warning

This setup is ideal for testing and development but may not be suitable for production.


๐Ÿ› ๏ธ Manage K3S

๐Ÿ’ป Management on Server Nodes

Use the K3S-specific kubectl to manage your cluster:

k3s kubectl

๐Ÿ“œ Download Kube Config

You can download the kubeconfig file from:

/etc/rancher/k3s/k3s.yaml

๐Ÿ’พ Database Backups

๐Ÿงณ etcd Snapshots

K3S stores etcd snapshots in the following directory:

/var/lib/rancher/k3s/server/db/snapshots

Tip

Regular backups of the etcd database are crucial for disaster recovery.


๐Ÿ“š Resources



๐ŸŒ Explore More