Homelab Kubernetes Setup

Introduction

Kubernetes has become the go-to platform for managing containerized applications at scale. However, setting up a full Kubernetes cluster can be resource-intensive and complex, especially if you’re experimenting in a homelab environment. Enter MicroK8s, a lightweight, zero-configuration Kubernetes distribution that is perfect for smaller environments, like a Raspberry Pi cluster. In this blog post, we’ll walk through the steps to set up MicroK8s on a Raspberry Pi homelab cluster.


Why Choose MicroK8s for Your Homelab?

MicroK8s is designed to be a minimalistic and lightweight Kubernetes distribution, making it an excellent choice for running on Raspberry Pis. It simplifies the setup process while still offering all the core features of Kubernetes. Some of the key benefits of using MicroK8s on a Raspberry Pi cluster include:

  • Simplicity: Easy to install and requires minimal configuration.
  • Efficiency: Lightweight, optimized for devices with limited resources.
  • Modularity: Add-ons for additional features like DNS, Ingress, and storage.
  • Upstream Kubernetes: Fully compliant with upstream Kubernetes, ensuring compatibility with other Kubernetes tools and resources.

Prerequisites

Before we dive into the setup process, make sure you have the following:

  • Raspberry Pi Cluster: A minimum of 2 Raspberry Pis (preferably Pi 4 or Pi 3) with at least 2GB of RAM each.
  • MicroSD Cards: One for each Raspberry Pi, at least 16GB.
  • Power Supply: Ensure each Pi has a reliable power supply.
  • Network: All Raspberry Pis should be connected to the same local network, either via Ethernet or Wi-Fi.
  • Operating System: Raspberry Pi OS Lite (64-bit) installed on each Pi.

Step 1: Prepare Your Raspberry Pis

  1. Update and Upgrade:
    • First, ensure your Raspberry Pi OS is up to date by running:bashCopy codesudo apt update && sudo apt upgrade -y
  2. Enable SSH:
    • If you haven’t already, enable SSH to allow remote access to your Raspberry Pis:bashCopy codesudo raspi-config
    • Navigate to Interface Options -> SSH and enable it.
  3. Static IP Addresses:
    • Assign static IP addresses to each Raspberry Pi to make it easier to manage and identify them in your cluster.
    • Edit the DHCP configuration:bashCopy codesudo nano /etc/dhcpcd.conf
    • Add the following lines at the end, replacing with your network details:javaCopy codeinterface eth0 static ip_address=192.168.1.xxx/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1
  4. Hostname Configuration:
    • Assign a unique hostname to each Pi for easier identification:bashCopy codesudo hostnamectl set-hostname <hostname>

Step 2: Install MicroK8s

  1. Install Snapd:
    • MicroK8s is distributed via Snap, so you’ll need to install Snapd first:bashCopy codesudo apt install snapd -y
    • After installation, reboot your Pi:bashCopy codesudo reboot
  2. Install MicroK8s:
    • Once your Pi is back online, install MicroK8s:bashCopy codesudo snap install microk8s --classic
    • Add your user to the microk8s group to avoid needing sudo for MicroK8s commands:bashCopy codesudo usermod -aG microk8s $USER newgrp microk8s
  3. Verify Installation:
    • Check that MicroK8s is running correctly:bashCopy codemicrok8s status --wait-ready

Step 3: Set Up the Cluster

  1. Initialize the Master Node:
    • On the node you want to act as the master, run:bashCopy codemicrok8s add-node
    • This will generate a join command for the worker nodes, something like:bashCopy codemicrok8s join 192.168.1.xxx:25000/<token>
  2. Join Worker Nodes:
    • On each worker node, execute the join command provided by the master node.
  3. Verify the Cluster:
    • On the master node, check the status of your nodes:bashCopy codemicrok8s kubectl get nodes
    • You should see all your Raspberry Pis listed as nodes in the cluster.

Step 4: Enable Useful Add-ons

MicroK8s comes with several useful add-ons that can enhance your cluster’s capabilities. Here are a few recommended ones:

  1. DNS:
    • Enable DNS to resolve services within the cluster:bashCopy codemicrok8s enable dns
  2. Storage:
    • Enable the default storage class for persistent volumes:bashCopy codemicrok8s enable storage
  3. Ingress:
    • Enable Ingress for managing external access to services in your cluster:bashCopy codemicrok8s enable ingress
  4. Dashboard:
    • Enable the Kubernetes dashboard for a visual overview of your cluster:bashCopy codemicrok8s enable dashboard

Step 5: Deploy Your First Application

Now that your MicroK8s cluster is up and running, it’s time to deploy your first application.

  1. Create a Deployment:
    • Create a simple Nginx deployment:bashCopy codemicrok8s kubectl create deployment nginx --image=nginx
  2. Expose the Deployment:
    • Expose the Nginx deployment as a service:bashCopy codemicrok8s kubectl expose deployment nginx --port=80 --type=NodePort
  3. Access the Service:
    • Find the service’s NodePort:bashCopy codemicrok8s kubectl get services
    • Access the Nginx service via your browser by entering <NodeIP>:<NodePort>.

Conclusion

Setting up MicroK8s on a Raspberry Pi homelab cluster provides a powerful and flexible platform for learning and experimenting with Kubernetes. With MicroK8s, you can manage containerized applications efficiently, even in a resource-constrained environment. Whether you’re looking to explore Kubernetes, host services, or build a distributed system, MicroK8s on Raspberry Pi offers a practical and hands-on approach to mastering modern IT infrastructure.


Have you set up a Kubernetes cluster on Raspberry Pis? Share your experiences, challenges, and favorite tips in the comments below!