Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 23k3s · Kubernetes

Install and set up k3s (small Kubernetes)

k3s is a real, certified Kubernetes in one small binary. It installs in one minute, needs only about 1 GB of RAM, and comes with an ingress controller, a load balancer, local storage and metrics already inside. It is perfect for labs, small production clusters, edge devices and home servers. This guide installs a k3s server and agents on RHEL and Ubuntu, makes the first settings in a config file, opens the firewall, and covers backups, upgrades and fixes.

  • 12 Lessen
  • 13 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

23.1

Know it before you install

A k3s server runs the control plane (and can run workloads too). Agents are worker nodes. Everything (containerd, flannel, CoreDNS, Traefik, ServiceLB, local-path storage, metrics-server) comes in the one k3s binary.

ItemValue
Installthe script from get.k3s.io (binary in /usr/local/bin/k3s)
Servicesk3s.service (server), k3s-agent.service (agent)
Config file/etc/rancher/k3s/config.yaml
Kubeconfig/etc/rancher/k3s/k3s.yaml
Join token/var/lib/rancher/k3s/server/node-token
Data/var/lib/rancher/k3s/
Ports6443/tcp (API), 8472/udp (flannel VXLAN), 10250/tcp (kubelet), 2379–2380/tcp (embedded etcd, HA only)
NetworksPods 10.42.0.0/16, Services 10.43.0.0/16

23.2

First configuration file (before the install)

Write the settings first — the install script reads them:

mkdir -p /etc/rancher/k3s
/etc/rancher/k3s/config.yaml
write-kubeconfig-mode: "0600"
tls-san:
  - "k3s.example.com"
  - "192.0.2.10"
node-name: "k3s-1"
secrets-encryption: true
disable:
  - traefik
SettingWhy
write-kubeconfig-mode: "0600"Only root can read the admin kubeconfig.
tls-sanThe API certificate is valid for your DNS name and IP (for remote kubectl).
secrets-encryption: trueKubernetes Secrets are encrypted in the datastore.
disable: [traefik]Only when you want another ingress (for example ingress-nginx) or already run Nginx on 80/443. Leave Traefik on if you want it.

23.3

Install the server on RHEL, Rocky and Alma

The script also installs the k3s-selinux policy on RHEL. Open the firewall for the k3s networks:

firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --permanent --zone=trusted --add-source=10.42.0.0/16
firewall-cmd --permanent --zone=trusted --add-source=10.43.0.0/16
firewall-cmd --reload
curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=stable sh -
systemctl status k3s --no-pager
k3s kubectl get nodes
NAME    STATUS   ROLES                  AGE   VERSION
k3s-1   Ready    control-plane,master   40s   v1.33.5+k3s1

23.4

Install the server on Ubuntu and Debian

ufw allow 6443/tcp
ufw allow 10250/tcp
ufw allow 8472/udp
ufw allow from 10.42.0.0/16
ufw allow from 10.43.0.0/16
curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=stable sh -
k3s kubectl get nodes

Op het werk

Pin a version for production: INSTALL_K3S_VERSION=v1.33.5+k3s1. Read the script first if you like: curl -sfL https://get.k3s.io -o k3s-install.sh && less k3s-install.sh.

23.5

Add agents (worker nodes)

Read the token on the server, then run the script on each agent with the server address:

cat /var/lib/rancher/k3s/server/node-token
K10c1f…::server:9a7d…
curl -sfL https://get.k3s.io | K3S_URL=https://192.0.2.10:6443 K3S_TOKEN='K10c1f…::server:9a7d…' sh -
k3s kubectl get nodes -o wide

(Open 10250/tcp and 8472/udp on agents too.) For a high-availability control plane, start the first server with cluster-init: true (embedded etcd) and join two more servers with server: https://first:6443 and the token.

23.6

Use kubectl from your laptop

cat /etc/rancher/k3s/k3s.yaml
scp root@192.0.2.10:/etc/rancher/k3s/k3s.yaml ~/.kube/k3s
sed -i 's/127.0.0.1/k3s.example.com/' ~/.kube/k3s
chmod 600 ~/.kube/k3s
export KUBECONFIG=~/.kube/k3s
kubectl get pods -A

23.7

Daily use: the commands and the real uses

k3s kubectl get nodes
k3s crictl ps
k3s crictl images
journalctl -u k3s -f
k3s check-config
kubectl get storageclass
kubectl get svc -n kube-system

Built-in parts you can use at once:

  • local-path storage: a PVC gets a folder on the node (/var/lib/rancher/k3s/storage).
  • ServiceLB: a Service type: LoadBalancer gets the node IPs — no cloud needed.
  • Traefik: an Ingress works on ports 80/443 (unless disabled).
  • Auto-deploy: YAML files in /var/lib/rancher/k3s/server/manifests/ are applied by themselves.

Real uses at work:

  • Labs and CKA/CKS practice on one small VM.
  • Small production clusters, branch offices, edge and IoT devices.
  • CI test clusters that start in seconds.

23.8

Back up and upgrade

With embedded etcd (cluster-init), take snapshots (k3s also takes them every 12 hours by default):

k3s etcd-snapshot save --name before-upgrade
k3s etcd-snapshot list

With the default SQLite datastore, back up /var/lib/rancher/k3s/server/db/ and the token while k3s is stopped. Upgrade one minor version at a time by running the script again with the new version — servers first, then agents:

curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.34.1+k3s1 sh -

For many clusters use the system-upgrade-controller, which upgrades nodes from a Plan in the cluster.

23.9

Make it safe

Security checklist:

  • k3s.yaml and node-token are master keys: 600, never in Git, rotate if they leak.
  • secrets-encryption: true; follow the k3s CIS hardening guide for production.
  • Firewall 6443 to admin networks only; agents and servers only talk to each other.
  • Flannel enforces NetworkPolicies in k3s (kube-router policy controller is built in) — start with default-deny per namespace.
  • Pin versions; upgrade at least every few months; back up before each upgrade.

23.10

Remove k3s

The install script also writes uninstall scripts:

/usr/local/bin/k3s-uninstall.sh
/usr/local/bin/k3s-agent-uninstall.sh

23.11

Fix common problems

ProblemCauseFix
Agent stays NotReady or Pods can not talk across nodesFirewall blocks 8472/udp or the Pod/Service networks.Open the ports and trusted sources above; journalctl -u k3s-agent.
Unable to connect to the server: x509: certificate is valid for 127.0.0.1, …Your DNS name/IP is not in the certificate.Add it to tls-san, restart k3s.
permission denied reading k3s.yamlMode is 0600 (safe).Use sudo, or copy it to ~/.kube/config with your owner.
Traefik Pods Pending / ports 80/443 busyNginx or Apache already uses 80/443.Stop the other server, or disable: [traefik].
Agent join fails: token errorWrong token or server URL.Copy the full node-token; use https://IP:6443.
Pods have no DNSCoreDNS not running or firewall.kubectl -n kube-system get pods; open the Pod network in the firewall.

23.12

Next steps

1Which file holds the admin login of a k3s cluster?

/etc/rancher/k3s/k3s.yaml.

2An agent joined but its Pods can not reach Pods on the server. What do you check first?

The firewall: 8472/udp (flannel VXLAN) and the Pod network 10.42.0.0/16.

Alle commando's in dit hoofdstuk