Step-by-Step Guide to Building Your Raspberry Pi Cluster

This guide will walk you through setting up a Raspberry Pi cluster, including configuring static IPs, disabling Wi-Fi, enabling cgroups, and installing k3s.

This guide will walk you through setting up a Raspberry Pi cluster, including configuring static IPs, disabling Wi-Fi, enabling cgroups, and installing k3s.

Prerequisites:

  • Raspberry Pi boards (3 or 4 recommended)
  • MicroSD cards
  • Power supplies for each Raspberry Pi
  • Ethernet cables
  • Network switch (if using Option 2)
  • A computer to configure the Raspberry Pis
  • Raspberry Pi OS (64-bit Lite version recommended)

image.png

Step 1: Install Raspberry Pi OS

  1. Download the Raspberry Pi OS (64-bit Lite) from the official Raspberry Pi website.
  2. Flash the OS onto each microSD card using a tool like Raspberry Pi Imager, BalenaEtcher, or dd.

Step 2: Initial Setup (per Pi)

  1. Insert the microSD card into a Raspberry Pi.
  2. Connect a keyboard, mouse, and monitor (or use SSH after enabling it).
  3. Boot up the Raspberry Pi.
  4. Follow the initial setup prompts (change the default password, set the hostname, etc.). If you plan to use SSH, you can create an empty file named ssh in the boot partition of the SD card before booting it up for the first time. This will enable SSH.
  5. Open a terminal.
  6. Update and upgrade the system:
    sudo apt update
    sudo apt upgrade
    
  7. Install dhcpcd if it's not already installed:
    sudo apt-get install dhcpcd
    

Step 3: Set Static IP Address for the Master Node

Choose one of the following options based on your network setup:

  • Option 1: Each Raspberry Pi Connected to the Router
    1. Open the dhcpcd.conf file:
      sudo nano /etc/dhcpcd.conf
      
    2. Add the following lines to the end of the file, replacing the placeholders:
      interface INTERFACE_NAME
      static ip_address=YOUR_STATIC_IP/24
      static routers=YOUR_GATEWAY_IP
      static domain_name_servers=YOUR_GATEWAY_IP # DNS same as router IP
      
      • INTERFACE_NAME: e.g., eth0 for wired, wlan0 for Wi-Fi. You'll be disabling Wi-Fi later for the master, so use eth0.
      • YOUR_STATIC_IP: e.g., 192.168.1.100. Choose an IP address outside of your router's DHCP range.
      • YOUR_GATEWAY_IP: Your router's IP address (e.g., 192.168.1.1).
    3. Save and exit: Ctrl+X, then Y, then Enter.
    4. Restart dhcpcd:
      sudo systemctl restart dhcpcd
      
    5. Verify the IP address:
      ip addr
      
  • Option 2: Using a Network Switch
    • Master Node Configuration:
      1. Open dhcpcd.conf:
        sudo nano /etc/dhcpcd.conf
        
      2. Add the following lines:
        interface eth0
        static ip_address=192.168.50.1/24
        
      3. Save and exit.
      4. Install iptables (if not installed):
        sudo apt install iptables
        
      5. Enable internet forwarding from eth1 (your internet-connected interface) to eth0 (the switch):
        sudo iptables -t nat -A POSTROUTING -o eth1 -s 192.168.50.0/24 -j MASQUERADE
        
      6. Install iptables-persistent:
        sudo apt install iptables-persistent -y
        
      7. Save current IPv4 rules (and likely No for IPv6):
      8. To save iptables rules in the future:
        sudo netfilter-persistent save
        
      9. Restart the network service:
        sudo systemctl restart dhcpcd
        
      10. Reboot the Raspberry Pi:
        sudo reboot
        
    • Node Configuration:
      1. Open dhcpcd.conf:
        sudo nano /etc/dhcpcd.conf
        
      2. Add the following lines:
        interface eth0
        static ip_address=192.168.50.2/24 # Use .2 for the first node, .3 for the second, etc.
        static routers=192.168.50.1      # Gateway is the master node
        static domain_name_servers=192.168.50.1 8.8.8.8 # Master IP and Google DNS
        
      3. Save and exit.
      4. Check network connections:
        ip r
        

Step 4: Disable Wi-Fi on the Master and then Nodes (optional but improves network bandwidth and security and declutters the routers network)

  1. Open the config.txt file:
    sudo nano /boot/config.txt 
    
    # 64 bit version -  sudo nano /boot/firmware/config.txt
    
    <aside> 💡My file on the 64bit version moved to - sudo nano /boot/firmware/config.txt</aside>
  2. Add the following line:
    dtoverlay=pi3-disable-wifi
    
  3. Save and exit.
  4. Reboot the Raspberry Pi:
    sudo reboot
    

Step 5: Enable Cgroups (Master & Nodes)

  1. Open the cmdline.txt file:
    sudo nano /boot/cmdline.txt 
    
    64 bit version - sudo nano /boot/firmware/cmdline.txt
    
    <aside> 💡My file on the 64bit version moved to - sudo nano /boot/firmware/cmdline.txt</aside>
  2. Add the following to the end of the line:
    cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory
    
  3. Save and exit.
  4. Reboot the Raspberry Pi:
    sudo reboot
    

Step 6: Install k3s

  1. Install k3sup:
    curl -sLS <https://get.k3sup.dev> | sh
    sudo install k3sup /usr/local/bin/
    
  2. Master Node:
    • Set the MASTER_IP environment variable:
      export MASTER_IP=YOUR_MASTER_IP
      
      e.g., export MASTER_IP=192.168.50.1 ( .1 as first on the subnet if using network switch)
    • Install k3s:
      k3sup install --ip $MASTER_IP \\
      --user pi \\ # Replace with your username
      --k3s-channel v1.24 \\ # Or the version you want
      --local-path /tmp/config
      
  3. Node(s):
    • Set the AGENT_IP environment variable:
      export AGENT_IP=YOUR_NODE_IP
      
      e.g., export AGENT_IP=192.168.50.2
    • Join the node to the cluster:
      k3sup join --ip $AGENT_IP \\
      --user pi \\ # Replace with your username
      --server-ip $MASTER_IP \\
      --k3s-channel v1.24 \\ # Or the version you want
      --server-user pi # Replace with the master node username
      

SSH Key Issues:

  • If you get an SSH error, generate SSH keys on the master (if needed):
    ssh-keygen
    
  • Copy the SSH key to the target Raspberry Pi:
    ssh-copy-id -i /home/{username}/.ssh/id_rsa.pub <target_ip_address>
    
    Replace username with your Raspberry Pi’s username Replace target_ip_address with the Targe Raspberry Pi’s IP Address
  1. Configure kubectl:
    • On the master node, set the KUBECONFIG environment variable:
      export KUBECONFIG=/tmp/config
      
    • Use the default context:
      kubectl config use-context default
      

Step 7: Verify the Cluster

  1. On the master node, check the cluster status:
    kubectl get node -o wide
    
    or
    sudo kubectl cluster-info
    

What's Next?

Congratulations! You've built your own Raspberry Pi cluster. Here are some ideas for what you can do with it:

  • Run containerized applications.
  • Deploy a web server or a database.
  • Learn about distributed computing and orchestration.
  • Set up a home automation system.

A Raspberry Pi cluster is a versatile tool for learning and experimentation. Have fun exploring the world of distributed computing!

logo_header_dark_bg.png

Written by Matthew Betts

Meet Matt Betts: Your Outsourced Tech Wizard

Founder - Lead Developer - Technical Consultant

Over 12 years of experience building software and guiding development teams. I'm Matt Betts, the driving force behind Wizard Dev House. Based in South Africa, my passion lies in leveraging technology to solve complex problems and create truly impactful solutions. I thrive on turning innovative ideas into reality. I offer not just expert development across front-end, full-stack, mobile, web, and IoT, but also strategic technical guidance to shape your project's future. My commitment is to deliver custom, high-performing software that genuinely empowers your business.

Read Other Blog Posts

Ditch the Desktop, Embrace the Pi: Your New Favori...

Let's talk web development. For years, the drill has been pretty standard: whip up some files, open 'em in Chrome, maybe fire up an Apache, Node, or Vite server to get your page live. All fr...

Read More

Lego for Geeks: Building a Raspberry Pi Kubernetes...

My journey into the world of tiny computers and lightweight Kubernetes. It was messy, occasionally frustrating, but ultimately incredibly rewarding. I learned a ton, didn't (miraculously) fr...

Read More

Vibe Coding: Shortcut to Genius or Highway to Hell...

Alright, digital architects and code-slingers, let's talk about the latest shiny object distracting developers: "Vibe Coding." You know the drill – punch in a prompt, let the AI genie do its...

Read More

Working on a project that needs a little magic? Let's Talk

hello@wizarddevhouse.co.za

©2024, Built by Wizard Dev House