Zachi Nachshon
Zachi Nachshon Software Architect and DevOps Engineer. Passionate technologist, OSS enthusiast and Raspberry Pi addict.

Install Rancher K3s on Raspberry Pi Cluster

Install Rancher K3s on Raspberry Pi Cluster

Install a Rancher Labs Kubernetes distribution (k3s) on a Raspberry Pi cluster.


Prerequisites


rpi-cluster-numbered


Master Server

What is a Kubernetes master node? A master node is a server that controls and manages a set of worker nodes, in our case it is the Raspberry Pi that controls the rest of the Raspberry Pi(s) on our cluster.

Install

  1. SSH into the Raspberry Pi server that is intended to operate as the Kubernetes master. It should be the one named kmaster as instructed by this post

    1
    2
    
    # Connect to RPi server that operates as the k8s master
    ssh pi@kmaster
    
  2. Run the following command to install a plain version of k3s without traefik load balancer and k8s-dashboard

    1
    
    curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC=" --no-deploy traefik --no-deploy kubernetes-dashboard"  sh -
    
  3. Verify that k3s was installed successfully. Run the following commands from within the RPi master server

    1
    2
    3
    4
    5
    6
    7
    8
    
    # Check for status - active (running)
    sudo systemctl status k3s
       
    # Check for status - Ready
    sudo kubectl get nodes
       
    # Optional - check that there are no error logs
    tail -f /var/log/syslog
    
  4. (Optional): Run when in need to restart k3s

    1
    2
    
    # Restart k3s
    sudo systemctl restart k3s
    

Uninstall

  1. SSH into the k3s master server

    1
    
    ssh pi@kmaster
    
  2. Uninstall k3s by executing the following script:

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


Worker Node

What is a Kubernetes worker node? These are Raspberry Pi servers that act as workload runtimes i.e. run our applications, jobs, whatever we require them to run but they aren’t the ones that manage the cluster, just the ones that “get the job done”.

Join a Cluster

  1. Extract the k3s join cluster token

    1
    2
    3
    4
    5
    6
    7
    8
    
    # SSH to the RPi master server
    ssh pi@kmaster
       
    # Extract the join token
    sudo cat /var/lib/rancher/k3s/server/node-token
       
    # Alternatively, you can run this on-liner directly from a client machine
    ssh pi@kmaster "sudo cat /var/lib/rancher/k3s/server/node-token"
    
  2. Find the k3s master server IP address that is assigned to kmaster, either from the server itself or from a client machine if you’ve followed this post

    1
    2
    3
    4
    5
    
    # From the RPi master server
    ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1
       
    # Alternatively, from a client machine
    cat /etc/hosts | grep kmaster | awk '{print $1}'
    
  3. SSH into a Raspberry Pi server intended to be used as a Kubernetes worker node. It would be the one named knode<number> as instructed on this post

    1
    2
    
    # Connect to a k3s worker node
    ssh pi@knode<number>  
    
  4. Run the following command to install k3s-agent and join the worker node to an existing cluster

    1
    2
    3
    4
    
    # Replace MASTER-IP-ADDRESS with the master server IP address from previous step
    # Replace JOIN-TOKEN with the join token from previous step
    curl -sfL http://get.k3s.io | K3S_URL=https://<MASTER-IP-ADDRESS>:6443 \
    K3S_TOKEN=<JOIN-TOKEN> sh -
    
  5. Verify that k3s-agent was installed successfully. Run the following commands from within the RPi worker server

    1
    2
    3
    4
    5
    
    # Check for status - active (running)
    sudo systemctl status k3s-agent
       
    # Optional - check that there are no error logs
    tail -f /var/log/syslog
    
  6. Repeat the above steps for every Raspberry Pi board intended to be used as a Kubernetes worker node

Uninstall

  1. SSH into the k3s worker node

    1
    
    ssh pi@knode<number>
    
  2. Uninstall k3s-agent by executing the following script:

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


Utilities

These are common utilities that should get installed on client machines to interacts with k3s master server.

Why do I need to install them? You’ll want to interacts with Kubernetes in order to deploy services, execute Helm charts and/or use utilities that grant you cluster visibility.


kubectl

Install kubectl, a command-line-interface tool that allows you to run commands against a remote k3s cluster.

  1. On a client machine, create a new empty k3s config file

    1
    2
    3
    
    mkdir -p $HOME/.kube/k3s 
    touch $HOME/.kube/k3s/config
    chmod 600 $HOME/.kube/k3s/config  # Set limited user permissions
    
  2. Copy the k3s cluster configuration from the RPi master server

    1
    
    ssh pi@kmaster "sudo cat /etc/rancher/k3s/k3s.yaml" > $HOME/.kube/k3s/config
    
  3. Edit the k3s config file on the client machine and change the remote IP address of the k3s master from localhost/127.0.0.1 to kmaster

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # Edit master config
    vim $HOME/.kube/k3s/config
       
    # Search for the 'server' attribute located in - 
    # clusters:
    # - cluster:
    #   server: https://127.0.0.1:6443 or https://localhost:6443
    #
    # Change 'server' value to https://kmaster:6443
    
  4. Install kubectl as described in the official docs

    1
    2
    3
    4
    5
    
    # tl;dr - macOS only
    brew install kubectl
       
    # Verify client version
    kubectl version --client
    
  5. Export k3s config file path as KUBECONFIG environment variable and by doing that set the kubectl context to use the RPi k3s cluster

    1
    
    export KUBECONFIG=$HOME/.kube/k3s/config
    
  6. Verify that kubectl was installed properly and can communicate with the RPi master server

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    kubectl get nodes
       
    # Expect the following respones as success:
    #
    # NAME      STATUS   ROLES                  AGE   VERSION
    # knodeX    Ready    <none>                 10m   v1.20.4+k3s1
    # ...
    # knode1    Ready    <none>                 23m   v1.20.4+k3s1
    # kmaster   Ready    control-plane,master   52m   v1.20.4+k3s1
    
  7. (Optional): read here for additional information about kubectl


k9s

Install k9s, a terminal UI that interacts with the k3s cluster, increase velocity by saving you from typing repetitive commands and/or the need to alias common ones. It allows easy navigation, observation and management - all in one package.

  1. Install as instructed on the official repository docs

    1
    2
    
    # tl;dr for macOS
    brew install k9s
    
  2. In case you are working on a single cluster and it is the default one, move to the next step, otherwise, if you are working on multiple clusters and/or using a cluster which isn’t named default, change the currentContext and currentCluster attributes on the k9s config file to the proper cluster values.

  3. Run k9s on a fresh shell session and verify that you can connect the k3s cluster successfully

  4. (Optional): read here for additional information about k9s


Summary

Well done for successfully installing a Kubernetes cluster on top of your Raspberry Pi cluster ! :clap:

What now? Check back for future posts explaining how to install a load balancer, certificate manager and a private docker registry on that cluster.

Please leave your comment, suggestion or any other input you think is relevant to this post in the discussion below.

Thanks !


Credits: Logo by cncf-branding

comments powered by Disqus