❤️ ABS is one of the five Certified Kubernetes Service Providers in India ❤️

Building a Kubernetes Cluster with Raspberry Pi for Home Projects

Introduction:

Kubernetes, the open-source container orchestration platform, is renowned for its ability to manage and deploy containerized applications. In this guide, we’ll walk you through the process of setting up a Kubernetes cluster using Raspberry Pi devices. This cluster is ideal for home projects, testing, and learning about container orchestration.

Hardware Requirements:

Before we begin, make sure you have the following hardware:

  1. Raspberry Pi boards (preferably Raspberry Pi 4 with 2GB or 4GB RAM)
  2. MicroSD cards (16GB or larger) for each Raspberry Pi
  3. Power supplies for the Raspberry Pi devices
  4. Ethernet cables
  5. A network switch (optional, if you have more than one Pi)
  6. Cooling fans (optional but recommended, especially for Raspberry Pi 4)

Setting up Raspberry Pi Devices

  1. Install Raspberry Pi OS: Download and install Raspberry Pi OS on each microSD card using a tool like “Raspberry Pi Imager.”

  2. Enable SSH: To enable SSH access, create a file named “ssh” (without an extension) in the boot partition of the microSD card.

  3. Headless Setup: For a headless setup (without a monitor or keyboard), create a file named “wpa_supplicant.conf” in the boot partition to configure Wi-Fi settings.

  4. Initial Boot: Insert the microSD cards, connect the Raspberry Pi devices to power, and connect them to your network switch (if applicable).

  5. Find IP Addresses: Use a network scanning tool (e.g., “Advanced IP Scanner”) to discover the IP addresses assigned to your Raspberry Pi devices.

Installing Kubernetes

Now, let’s set up Kubernetes on your Raspberry Pi cluster.

Master Node Setup

     1. Access the Master: Connect to the Raspberry Pi you intend to use as the master node via SSH. Use the IP address found in the previous step.

     2. Update and Upgrade: Run the following commands to update and upgrade the system:

				
					sudo apt update
sudo apt upgrade -y
				
			

    3. Install Docker: Install Docker using the following commands

				
					sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker

				
			

     4. Install kubeadm, kubectl, kubelet:

				
					sudo apt install kubeadm kubectl kubelet -y

				
			
Initializing the Kubernetes Cluster

     1. On the master node, initialize the cluster using kubeadm:

				
					sudo kubeadm init --pod-network-cidr=192.168.0.0/16
				
			

    2. After the initialization is complete, follow the instructions provided to join worker nodes to the cluster.

Setting Up Worker Nodes
  1. Access each worker node via SSH and update/upgrade the system as you did with the master node.

  2. Install Docker, kubeadm, kubectl, and kubelet, just as you did on the master node.

  3. Use the join command obtained from the master node to join each worker node to the cluster.

Configuring kubectl

To use kubectl, you need to copy the cluster’s configuration file:

     1. On the master node, run:

				
					mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
				
			

    2. Now you can use kubectl to interact with your Raspberry Pi Kubernetes cluster.

Deploying Your First App

    1. Create a simple pod definition file (e.g., “my-pod.yaml”) for your app:

				
					apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx:latest

				
			

    2. Deploy the pod to the cluster:

				
					kubectl apply -f my-pod.yaml
				
			

    3. Expose the pod as a service:

				
					kubectl expose pod my-pod --type=NodePort --port=80
				
			

    4. Access your app through the NodePort or LoadBalancer service.

Conclusion:

Congratulations! You’ve successfully set up a Kubernetes cluster using Raspberry Pi devices. This cluster is ideal for home projects and experimenting with container orchestration. You can now explore various Kubernetes features and deploy applications on your Raspberry Pi cluster. Enjoy your Kubernetes journey!