K3s Setup
Install
To install K3s on a Linux machine, you can follow the official installation guide found on the K3s Documentation
Disable swap
In order to effectively proceed with the installation and setup of K3s, it's recommended to disable swap on your Linux machine. Kubernetes does not recommend using swap due to potential issues with resource management. Here are the commands to disable swap:
Disabling swap can affect memory pressure handling on the host. Verify the correct /etc/fstab entry before editing it, and ensure the system has sufficient RAM for its workloads.
- Temporarily disable swap:
sudo swapoff -a
- To permanently disable swap, edit the
/etc/fstabfile and comment out (add#at the beginning) the line that contains the swap partition:
#/swapfile swap swap defaults 0 0
The above is an example line. Adjust it according to your system's configuration. After editing, reboot the system. You can now continue with the K3s installation.
Setup
Install K3s using the script provided by Rancher. This will install and configure a lightweight Kubernetes cluster.
This command downloads and executes an installation script from the network. Review the script and pin a specific K3s version before using it in a production environment.
curl -sfL https://get.k3s.io | sh -
To specify an alternate version or additional options, you can customize the above command as needed.
For example, to install K3s without the default traefik ingress controller, you can use the following command:
curl -sfL https://get.k3s.io | sh -s - server \
--node-name k3s-master \
--tls-san YOUR_SERVER_IP \
--node-external-ip YOUR_EXTERNAL_IP \ # Optional, if you want to connect to the server from outside directly
--disable traefik \
--disable servicelb \
--cluster-init \
--write-kubeconfig-mode 644 \
--flannel-iface tailscale0 # Optional, if you have Tailscale installed and want to use it for pod networking
Verify Installation
After the installation is complete, you can verify the status of the K3s service using the following command:
sudo systemctl status k3s
If the service is running, you should see an output indicating that the service is active and running.
Check Pods
You can also check the status of the pods in the kube-system namespace to ensure that the core components of K3s are running correctly:
kubectl get pods -n kube-system
Work with firewall
Disable UFW (Uncomplicated Firewall)
For a straightforward setup of K3s, it is common to disable UFW to avoid any issues with network traffic and port access. If you choose this approach, execute the following:
Disabling UFW permits all inbound traffic that is not restricted elsewhere. Prefer the firewall rules in the next section for internet-connected or production systems.
sudo ufw disable
Disabling UFW will allow all network traffic, facilitating K3s without additional configuration.
Configure UFW for K3s
If you prefer to keep UFW enabled for added security, you must ensure that K3s can communicate using specific ports. Here are the essential steps and rules to enable:
-
Allow SSH Traffic:
Before enabling UFW, ensure that SSH traffic is allowed to maintain remote access:
sudo ufw allow OpenSSHThis command allows SSH on the default port
22.
If your SSH uses a different port, be sure to specify it accordingly (e.g.,sudo ufw allow 2222/tcpfor port2222). -
Enable UFW:
After ensuring SSH is allowed, you can safely enable UFW:
sudo ufw enable -
Allow K3s Default Ports:
K3s typically uses several ports for cluster communication and applications. Make sure these ports are open:
# Allow standard Kubernetes API server portsudo ufw allow 6443/tcp #apiserversudo ufw allow from 10.42.0.0/16 to any #podssudo ufw allow from 10.43.0.0/16 to any #servicesOther ports that may be required include:
# Required only if using HA with embedded etcdsudo ufw allow 2379/tcp # etcd server client APIsudo ufw allow 2380/tcp # etcd server communication# Enable kubelet metrics traffic between all nodessudo ufw allow 10250/tcp# Flannel VXLAN - required only if using the Flannel VXLAN backendsudo ufw allow 8472/udp# Required only for Flannel WireGuard backendsudo ufw allow 51820/udp # For Flannel WireGuard with IPv4sudo ufw allow 51821/udp # For Flannel WireGuard with IPv6# Required only for embedded distributed registry (Spegel)sudo ufw allow 5001/tcpAdjust the rules based on your specific requirements and configurations.
-
Verify UFW Status:
After configuring the rules, verify the status to ensure the correct ports are allowed:
sudo ufw status
By selectively allowing these ports, K3s can function while maintaining UFW’s protection for other unnecessary traffic. Always ensure that only the necessary ports and services are exposed to avoid security risks.
Use K3s Without Sudo
Configuring your system to use kubectl commands without sudo involves setting up the KUBECONFIG environment variable.
-
Set the
KUBECONFIGEnvironment Variable:First, set up the
KUBECONFIGenvironment variable in your current shell session to point to your local configuration file:export KUBECONFIG=~/.kube/config -
Generate the Local Configuration File:
Create the
.kubedirectory and generate the configuration file using the following command:mkdir -p ~/.kubesudo k3s kubectl config view --raw > "$KUBECONFIG"chmod 600 "$KUBECONFIG"This ensures that your Kubernetes configuration file is only readable by your user, maintaining security.
-
Persist Configuration Across Reboots:
To ensure the
KUBECONFIGenvironment variable is automatically set in every new terminal session, add it to your~/.profileor~/.bashrcfile:echo 'export KUBECONFIG=~/.kube/config' >> ~/.bashrcsource ~/.bashrcIf you use a different shell (e.g., zsh), make similar additions in your
~/.zshrc.
By setting it up this way, you can run kubectl without needing sudo, while maintaining proper security for your k3s.yaml file.