Skip to content

Install k3s cluster

Prerequisites

  • A load balancer IP or hostname.
  • Access to all control plane and worker nodes.

Installation

  1. Install NFS support (optional)

    If your cluster will use NFS for persistent storage, install the nfs-common package on all nodes (both control plane and worker nodes) by running the following command:

    Terminal window
    sudo apt install nfs-common
  2. Install k3s on the first control plane node

    Run the following command to install k3s:

    Terminal window
    curl -sfL https://get.k3s.io | sh -s - server \
    --cluster-init \
    --node-taint CriticalAddonsOnly=true:NoExecute \
    --tls-san <load-balancer-ip-or-hostname>
  3. Retrieve the cluster token

    After the installation, retrieve the <cluster-token> and take note of it:

    Terminal window
    cat /var/lib/rancher/k3s/server/node-token
  4. Install k3s on additional control plane nodes

    Run the following command to install k3s and link it to the cluster:

    Terminal window
    curl -sfL https://get.k3s.io | sh -s - server \
    --node-taint CriticalAddonsOnly=true:NoExecute \
    --tls-san <load-balancer-ip-or-hostname> \
    --server https://<control-plane-01-ip>:6443 \
    --token <cluster-token>
  5. Install k3s on all the worker nodes

    Run the following command to install k3s and link it to the cluster:

    Terminal window
    curl -sfL https://get.k3s.io | sh -s - agent \
    --server https://<control-plane-01-ip>:6443 \
    --token <cluster-token>

Configure local kubectl access

  1. Retrieve the Kubernetes configuration from one of the control plane nodes:

    Terminal window
    sudo cat /etc/rancher/k3s/k3s.yaml
  2. Save the content to a file located at %USERPROFILE%/.kube/<filename-without-extension> on your local machine. Rename the default context to your preferred cluster name.

  3. Add configuration to kubectl. In PowerShell, run:

    Terminal window
    Get-ChildItem -Path "$($env:USERPROFILE)/.kube" -File | ForEach-Object {
    $configFiles += $_.FullName + ";"
    }
    [Environment]::SetEnvironmentVariable("KUBECONFIG", $configFiles, "USER")
  4. Switch to the new context:

    Terminal window
    kubectl config use-context <context-name>