DEV Community

Cover image for Build a Raspberry Pi K8s Server For Your Side Projects
Harsha Vaidya
Harsha Vaidya

Posted on • Edited on

Build a Raspberry Pi K8s Server For Your Side Projects

Have you ever thought of shipping that side project of yours but felt like it's not worth the money to spend on cloud costs?

This was my prime motivation to build this project: A hosting solution for all my side projects. So let me share the solution I've come up with.

The goals for this project are:

  1. Accessible to anyone on the local network or wifi in my case.
  2. Low maintenance.
  3. Easy to deploy or make changes.

To achieve these you'll be needing:

  1. A Raspberry Pi ( I've used the new 4B model ) with at least 4GB of memory and 4 cores in total.
  2. At least 20 GB of free storage on your Pi.
  3. Your local network credentials if you are using wifi.
  4. A USB keyboard, Monitor with HDMI slot, a microHDMI cable.

Alright, once those are in place, let's start.

  • Install the Raspberry Pi Imager
  • Install Ubuntu 20.04.02 LTS (64-bit version) on your microSD card.
  • Edit the network config ( only if you are using wifi )
  • Place your SD card in the slot in the Pi. Connect the keyboard and Monitor to the Pi and connect the Pi to power.
  • Once it boots up it will ask for login and password. Use ubuntu for both and it will prompt you to change the password. Set a password then.
  • Reboot the server. sudo reboot
  • Install net-tools package. sudo apt-get install net-tools
  • Get the IP address of the server on your network. For this we can do ifconfig and find it out. If you have used wifi for this setup, look under the wlan0 header.
  • Setup SSH. On your computer run the command ssh-copy-id ubuntu@<ip_of_your_server> . It will prompt for the password. Supply it and you are done. Additionally update your ssh config file with a custom hostname for the pi for ease of use.
  • Now you can remotely access the server without needing a monitor or a keyboard connected to your Pi.

Now the setup is complete. To deploy your apps there are many ways. In this post I will be using MicroK8s

  • SSH into your Pi.
  • Enable cgroups on your Pi. Append cgroup_enable=memory cgroup_memory=1 to the end of /boot/firmware/cmdline.txt in the Pi.
  • Reboot. sudo reboot
  • Install microk8s. sudo snap install microk8s --classic
  • Add your current user to the microk8s group and gain access to the .kube caching directory:
sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube
Enter fullscreen mode Exit fullscreen mode
  • Start the cluster. microk8s start
  • Check status. microk8s status
  • Enable DNS, Ingress for exposing your apps on your local network. microk8s enable dns ingress
  • Get the kubectl config for the cluster. microk8s config
  • Copy the kubectl config you got in the above step to your computer to enable access to the control plane from your computer. This eliminates the need to SSH into the server when you want to access the cluster.

Now our single-node cluster is setup and ready to be used. You can now package your projects as containers and deploy them to this setup.

Alternatively, if you don't know K8s or don't want to use it, you can still deploy your projects as simple docker containers or even as plain-old services and this setup would still work.

So, did we meet our goals?

  1. We made something which we can access from our local network. ✅
  2. The server would be fine if you keep in mind the resource limits it has and always connect it power and network. ✅
  3. Since, I've used k8s deploying and making changes is a breeze. I also use Github actions for my projects which automate the building of docker images. So, effectively only one command (IDE takes care of git push 😉) I have to run to apply new changes to the deployed project. kubectl rollout restart deployment/<deployment_name>

Happy hacking! Hope you learnt something new today 😄.

Top comments (0)