How to Set Up a Two-Node Raspberry Pi K3s Cluster with Portainer and MetalLB

Why Two-Node ? due to some Resources constraints  🙃

The Raspberry Pi has become a go-to platform for learning and building small, powerful projects. One popular use case
is running a lightweight Kubernetes cluster with K3s, which is ideal for constrained environments like Raspberry Pi.
In this guide, we’ll go through setting up a two-node K3s cluster on Raspberry Pi, followed by deploying Portainer
to simplify cluster management.

Why K3s?

K3s is a lightweight Kubernetes distribution optimized for small environments, making it perfect for Raspberry Pi. It
uses fewer resources than standard Kubernetes while keeping all the features needed to run applications effectively.

What You’ll Need

  • Two Raspberry Pi devices (Pi 4 recommended for better performance)
  • MicroSD cards (at least 16 GB)
  • USB-C power adapters
  • Network connection (either Wi-Fi or Ethernet)
  • A laptop/desktop to set up and configure the cluster

Let’s get started!


Step 1: Set Up the Raspberry Pi OS

  1. Flash Raspberry Pi OS onto each SD card using Raspberry Pi Imager.
  2. Enable SSH by placing an empty file named ssh in the boot directory
    of each SD card.
  3. Configure Network: If you’re using Wi-Fi, add your Wi-Fi configuration in a file named
    wpa_supplicant.conf in the boot directory.
  4. Boot the Raspberry Pi: Insert the SD cards into each Pi, connect them to power, and boot up.
  5. Update Configuration to run K8S:  on all Raspy  Pis Edit /boot/firmware/cmdline.txt and add in the end of line cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memorysave and reboot.

Step 2: Install K3s on the Primary Node

We’ll start by installing K3s on the first Raspberry Pi, which will act as the primary node (server) in the cluster.

  1. SSH into the primary Pi:
    ssh pi@<primary-pi-ip>
    
    1. Install K3s:
      bash
      curl -sfL https://get.k3s.io | sh -
  2. Verify the Installation:
    After the installation, check if K3s is running:

    sudo kubectl get nodes
    

    The output should show the primary node as “Ready.”

    Step 3: Add the Secondary Node to the Cluster

    Now, let’s set up the second Raspberry Pi as a worker node.
    1. Retrieve the Token from the primary node:

    sudo cat /var/lib/rancher/k3s/server/node-token
    

Save this token; it’s needed to join the worker node to the cluster.

  1. Install K3s on the Worker Node:
    SSH into the second Pi:

    ssh pi@<secondary-pi-ip>
    

    Install K3s on the worker node, using the IP address of the primary node and the token:

    curl -sfL https://get.k3s.io | K3S_URL=https://<primary-node-ip>:6443 K3S_TOKEN=<your-token> sh -
    
  2. Verify Cluster Nodes:
    Back on the primary node, check the status of all nodes:

    sudo kubectl get nodes
    

    You should see both the primary and secondary nodes listed and marked as “Ready.”

    Step 4: Install MetalLB for Load Balancing

    Since we’re running a bare-metal setup, we’ll need a way to manage LoadBalancer services. MetalLB provides an
    easy way to assign IPs to services in your network.
    1. Install MetalLB:

    kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.10/config/manifests/metallb-native.yaml
    
  3. Configure MetalLB with an IP Pool:
    Create a ConfigMap specifying the range of IPs you want MetalLB to use. Choose an IP range in your network
    that won’t interfere with other devices. Replace 192.168.1.240-192.168.1.250 with your desired
    range.
   kubectl apply -f - <<EOF
   apiVersion: v1
   kind: ConfigMap
   metadata:
     namespace: metallb-system
     name: config
   data:
     config: |
       address-pools:
       - name: default
         protocol: layer2
         addresses:
         - 192.168.1.240-192.168.1.250
   EOF
  1. Verify MetalLB:
    Check that MetalLB is running:

    kubectl get pods -n metallb-system
    

    Step 5: Deploy Portainer to the K3s Cluster

    Portainer is a powerful UI that simplifies Kubernetes management. Let’s deploy it on our new K3s cluster.
    1. Create a Namespace for Portainer:

    kubectl create namespace portainer
    
  2. Add the Portainer Helm Repository:
    Install Helm if you haven’t already, then add the Portainer repository:

    helm repo add portainer https://portainer.github.io/k8s/
    helm repo update
    
    1. Deploy Portainer Using Helm:
      Install Portainer in the portainer namespace with LoadBalancer enabled:
      bash
      helm install portainer portainer/portainer -n portainer --set service.type=LoadBalancer
  3. Get the External IP for Portainer:
    Check the external IP assigned by MetalLB:

    kubectl get svc -n portainer
    

    Use the IP address listed under EXTERNAL-IP to access Portainer.
    5. Access Portainer:
    Open your browser and go to http://<External-IP>:9000. You’ll be prompted to set up an
    admin user and password. Once completed, you can start using Portainer to manage your cluster!

    Wrapping Up

    You now have a two-node Raspberry Pi Kubernetes cluster with Portainer installed, providing a user-friendly
    interface to manage and deploy applications. This setup is an excellent way to learn Kubernetes, experiment
    with deployments, and explore what’s possible with small devices like Raspberry Pi.
    Let me know if you have questions or issues as you go through the setup!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *