Containers are useful in many computing projects, be it hosting FOSS services, developing programs inside isolated environments, or experimenting with fun packages and repositories. But when you've deployed an army of containers, managing them can become quite a hassle.
That's where container orchestration platforms like Kubernetes come in handy with their monitoring, automation, auto-scaling, and load-balancing provisions. Better yet, you can put together a reliable Kubernetes server using inexpensive Raspberry Pi boards!
Related
This is how you build a Proxmox cluster
Why restrict yourself to a single node when you can build an entire cluster from your Proxmox servers?
Posts
What you'll need for the Raspberry Pi Kubernetes cluster
Although Kubernetes has fairly low system requirements, you'll want to go with at least a Raspberry Pi 3 for the ideal performance when working with containerized services. For a simple cluster, you can go with two Raspberry Pis. But if you're planning to run a high-availability cluster, you'll need at least three nodes to maintain quorum.
To maximize performance, I recommend choosing a CLI-based distro like the Raspberry Pi OS Lite. We have a guide on setting up the Raspberry Pi OS, and the overall procedure is more-or-less the same for the Lite version. If you're using the Raspberry Pi Imager to prepare the microSD card, I recommend enabling the SSH option from the Services tab.
(Optional) Connecting to the Raspberry Pi via SSH
If you're using Raspberry Pi OS or another non-GUI distribution, you can run an SSH client to execute commands on the SBC from another PC. PuTTY is our favorite app for this purpose, and here's how you can configure it on your system:
Download the setup.exe file for PuTTY from the official website and use it to install the tool on your PC. Run PuTTY.exe as an admin. Type the IP address of your Raspberry Pi under the Host name field, make sure the number 22 appears under Port, and tap Open.
If you don't know the IP address of your RPi, you can do so with the help of a network scanning utility such as
Fing. Accept the SSH fingerprint when PuTTY displays the Security Alert pop-up.
Log in to the Raspberry Pi with your Username and Password.
Modifying the cmdline.txt file for the Kubernetes cluster
Before you can start installing the Kubernetes packages, you'll have to add some extra flags to the cmdline.txt configuration file to prevent the Raspberry Pi Linux kernel from causing issues when setting up the cluster.
Use the nano editor to open the /boot/firmware/cmdline.txt file. sudo nano /boot/firmware/cmdline.txt
Enter the following flags at the end of the first line: cgroup_memory=1 cgroup_enable=memory
Hit Ctrl+X followed by Y and Enter to save and exit the file. Use the reboot command to restart your Raspberry Pi. sudo reboot
Setting up MicroK8s on the Raspberry Pi
There are plenty of ways to deploy a Kubernetes server on your Raspberry Pi, but we'll go with the easy-to-install MicroK8s. Once your RPi has finished rebooting,
Install the snapd package by executing this command: sudo apt install snapd -y
Use Snap to install MicroK8s on your SBC. sudo snap install microk8s --classic
Grant full access to the MicroK8s package to your normal user with the usermod command. sudo usermod -a -G microk8s $USER
Run the mkdir command to create a new directory for your MicroK8s packages and use chmod to let your main user access this directory. mkdir -p ~/.kube chmod 0700 ~/.kube
Use the su command to refresh the permissions. su - $USER
Deploying the MicroK8s Dashboard
While you can manage your Kubernetes server with just terminal commands, MicroK8s supports a dashboard extension to help you access your containers inside a convenient web UI. To set it up,
Start all the MicroK8s pods with this command: microk8s start
Run the following command to enable the MicroK8s dashboard. microk8s enable dashboard
Use the proxy argument to make the dashboard accessible. microk8s dashboard-proxy
Open your favorite browser and type the IP address of your Raspberry Pi followed by a colon (:) and the port number 10443.
Use the auto-generated Token from the terminal to gain access to the dashboard.
Configuring Docker for the Kubernetes environment
With the Docker Hub possessing a plethora of container images, you can set up the Docker package to grab images for the Raspberry Pi Kubernetes cluster.
Launch a Duplicate Session to execute the rest of the commands.
Download the docker.io package from the apt repository. sudo apt install docker.io -y
Since you're working inside a new terminal instance, run the usermod to grant your user access to the Docker packages before using su to refresh the permissions. sudo usermod -aG docker ${USER} su - ${USER}
Grab your favorite container images using the docker pull command: docker pull image_name
Enable the MicroK8s registry with this command: microk8s enable registry:size=20Gi
Next, you'll have to enable insecure registries inside the daemon.json config file. sudo nano /etc/docker/daemon.json
Add the following code into the file before tapping Ctrl+X, Y, and Enter to save the changes. { "insecure-registries" : ["localhost:32000"] }
Restart Docker to apply the changes. sudo systemctl restart docker
Tag the Docker image and push it to the MicroK8s registry. docker tag image_name localhost:32000/image_name docker push localhost:32000/image_name
If the
push command fails, you try enabling the registry once again using this command: microk8s enable registry
Deploy a container for the Docker image using the create deployment command: microk8s kubectl create deployment container_name --image=image_name
You can execute the get pods command to check the status of your containers. microk8s kubectl get pods
Creating a cluster for Kubernetes
So far, you've only set up Kubernetes on your Raspberry Pi and have yet to build a cluster for the container orchestration platform. If you're planning to use the current SBC as the master node, you'll need to repeat the entire procedure on every other Raspberry Pi board. Once you've set everything up, here's how you can add the other SBCs as extra nodes for your Kubernetes environment:
Switch to your master Raspberry Pi node and run this command: microk8s add-node
(Optional) For a high-availability cluster, you can enable the ha-cluster extension before adding the other nodes: microk8s enable ha-cluster
Execute the join command followed by the parameters given by the master node on each SBC: microk8s join IP_address_of_master_node:25000/random_string1/random_string2
You can confirm whether the connection was successful using the get nodes keyword. microk8s kubectl get nodes
Managing your containers like a pro with a Raspberry Pi Kubernetes cluster
With that, your Kubernetes cluster is fully operational, and you're free to tinker with containers to your heart's content inside the Dashboard. While a full-fledged K8s cluster may seem rather overkill for a simple self-hosting workstation, it's a great way to manage the finer aspects of your containers. Plus, it can grant some invaluable hands-on experience that will come in handy on your DevOps and sysadmin journey.
Related
I tried emulating PS3 games on the Raspberry Pi
While the Raspberry Pi 5 can technically run PS3 games, calling them playable would be too generous
Posts 1