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
- Update and Upgrade:
- First, ensure your Raspberry Pi OS is up to date by running:bashCopy code
sudo apt update && sudo apt upgrade -y
- First, ensure your Raspberry Pi OS is up to date by running:bashCopy code
- Enable SSH:
- If you haven’t already, enable SSH to allow remote access to your Raspberry Pis:bashCopy code
sudo raspi-config
- Navigate to
Interface Options
->SSH
and enable it.
- If you haven’t already, enable SSH to allow remote access to your Raspberry Pis:bashCopy code
- 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 code
sudo nano /etc/dhcpcd.conf
- Add the following lines at the end, replacing with your network details:javaCopy code
interface eth0 static ip_address=192.168.1.xxx/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1
- Hostname Configuration:
- Assign a unique hostname to each Pi for easier identification:bashCopy code
sudo hostnamectl set-hostname <hostname>
- Assign a unique hostname to each Pi for easier identification:bashCopy code
Step 2: Install MicroK8s
- Install Snapd:
- MicroK8s is distributed via Snap, so you’ll need to install Snapd first:bashCopy code
sudo apt install snapd -y
- After installation, reboot your Pi:bashCopy code
sudo reboot
- MicroK8s is distributed via Snap, so you’ll need to install Snapd first:bashCopy code
- Install MicroK8s:
- Once your Pi is back online, install MicroK8s:bashCopy code
sudo snap install microk8s --classic
- Add your user to the
microk8s
group to avoid needingsudo
for MicroK8s commands:bashCopy codesudo usermod -aG microk8s $USER newgrp microk8s
- Once your Pi is back online, install MicroK8s:bashCopy code
- Verify Installation:
- Check that MicroK8s is running correctly:bashCopy code
microk8s status --wait-ready
- Check that MicroK8s is running correctly:bashCopy code
Step 3: Set Up the Cluster
- Initialize the Master Node:
- On the node you want to act as the master, run:bashCopy code
microk8s add-node
- This will generate a join command for the worker nodes, something like:bashCopy code
microk8s join 192.168.1.xxx:25000/<token>
- On the node you want to act as the master, run:bashCopy code
- Join Worker Nodes:
- On each worker node, execute the join command provided by the master node.
- Verify the Cluster:
- On the master node, check the status of your nodes:bashCopy code
microk8s kubectl get nodes
- You should see all your Raspberry Pis listed as nodes in the cluster.
- On the master node, check the status of your nodes:bashCopy code
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:
- DNS:
- Enable DNS to resolve services within the cluster:bashCopy code
microk8s enable dns
- Enable DNS to resolve services within the cluster:bashCopy code
- Storage:
- Enable the default storage class for persistent volumes:bashCopy code
microk8s enable storage
- Enable the default storage class for persistent volumes:bashCopy code
- Ingress:
- Enable Ingress for managing external access to services in your cluster:bashCopy code
microk8s enable ingress
- Enable Ingress for managing external access to services in your cluster:bashCopy code
- Dashboard:
- Enable the Kubernetes dashboard for a visual overview of your cluster:bashCopy code
microk8s enable dashboard
- Enable the Kubernetes dashboard for a visual overview of your cluster:bashCopy code
Step 5: Deploy Your First Application
Now that your MicroK8s cluster is up and running, it’s time to deploy your first application.
- Create a Deployment:
- Create a simple Nginx deployment:bashCopy code
microk8s kubectl create deployment nginx --image=nginx
- Create a simple Nginx deployment:bashCopy code
- Expose the Deployment:
- Expose the Nginx deployment as a service:bashCopy code
microk8s kubectl expose deployment nginx --port=80 --type=NodePort
- Expose the Nginx deployment as a service:bashCopy code
- Access the Service:
- Find the service’s NodePort:bashCopy code
microk8s kubectl get services
- Access the Nginx service via your browser by entering
<NodeIP>:<NodePort>
.
- Find the service’s NodePort:bashCopy code
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!