How to Build a Raspberry Pi Kubernetes Cluster Using MicroK8s?

Photo of author

By admin

The uses of Kubernetes and building clusters have completely changed the scenario of application development and took it to a whole new level. If you are an application developer, using Kubernetes will help you make portable and safe software programs. If you are looking to use MicroK8s to build a Raspberry Pi in Kubernetes, let me make this clear to you that this is not something that beginners can accomplish with ease.

However, if you have some knowledge and experience working with Kubernetes, you should definitely follow this tutorial. Firstly, you need to know why you should build a Raspberry cluster with MicroK8s. Afterward, we will discuss the steps for building a Raspberry Pi Kubernetes cluster using MicroK8s.

MicroK8s is a fast, lightweight, enterprise-grade Kubernetes that saves time and space in your system. MicroK8s setup uses an HDMI screen and USB keyboard to manage the nodes on your clusters in Kubernetes. But what’s the reason for building Raspberry Pi on the Kubernetes cluster? Let’s find out!

Why Should You Develop a Raspberry Pi MicroK8s Cluster?

Using a MicroK8s will offload computation resources from your device which will save both of your time and spendings that are generally required to build a cluster. You can use the cluster to reallocate resources or you can also use it as a different device.  For example, if you are a photographer, uploading and storing several photos in one place and accessing them later can be tiresome. But if you offload those photos on Raspberry Pi, this would let you perform many tasks at once as the system is running multiple processes separately. 

Moreover, it helps you to save the storage space of your device. You can do the same things with videos and offload the videos on the Raspberry Pi MicroK8s cluster.

Why Should You Use a Raspberry Pi?

Raspberry Pi is a combination of tiny, single-board computers that were first introduced by a UK-based company. The main reason for Raspberry Pi was to make a revolution in the world of technology. As the Raspberry Pi Foundation said in an interview that they intended to make digital technology available all over the world. 

Raspberry PiRaspberry Pi is cheap; however, it has a wide range of features and hardware capabilities. You can use the Raspberry MicroK8s cluster as anything you want. You can use it as a display, a weather monitor, server, or anything. 

If you are well-educated with Kubernetes, Docker, and its factors, you can easily build a cluster using MicroK8s in Kubernetes. So, if you have a purpose for building a Raspberry cluster on your Kubernetes, such as for storing user data, go on with the same MicroK8s.

What is MicroK8s?

If you know about Kubernetes (K8s), you should know that it is a fast, secure, and reliable platform. MicroK8s is the mini version of Kubernetes (K8s), which is also the fastest snap. It helps developers manage the resources of Kubernetes clusters and also enjoy the standard K8s cluster features. The microK8s are optimized with thousands of codes that help you manage devices. 

You can deploy Kubernetes single-(master)-node cluster deployments easily with MicroK8s. MicroK8 lets you test everything in the cluster before you start scaling your distribution. Devices that work with Kubernetes bring troubles every now and then. Therefore troubleshooting each of those devices individually is more troublesome. If a master node does not work, there is no standard way to fix the master device from where you are building the clusters. However, a few more updates will be rolled out, so that the developers and organizations, who use MicroK8s can manage their devices effectively. 

What is Cluster Computing?

You will have to connect a series of nodes over a network to make computing and sharing of resources easier. At the same time, this will help to execute work quickly and more precisely. For the distribution of resources, developers use three types of cluster computers: Highly available or fail-over, Load balancing, and High-performance computers.

The clusters that come with high efficiency are known as a supercomputer; these clusters pool resources that can manage high computational workloads. The high-dominant clusters work against the failures that could come up during the development process. 

Since a cluster computer groups other computers to make them appear in one system, it’s the most important scaling solution for enterprises. Cluster computers are one of the biggest resources in the application development process, and K8s lets you manage every feature of clusters with ease.

Steps For Building a Raspberry Pi Kubernetes Cluster using MicroK8s

Here are the simple steps that you need to follow:

Step 1: Things That You Will Need to Build Raspberry Pi Cluster

Now that you have developed knowledge about MicroK8s and Kubernetes cluster, it’s time to learn how to build the Raspberry Pi using MicroK8s. To do that, you need the following items:

Requirements

  • One 16.04 LTS (or later) Ubuntu desktop 
  • At least 2 Raspberry Pis
  • USB power supply
  • Micro-USB power cables for each Pi (USB-C for Pi4)
  • microSD cards per Pi (8GB recommended), each of which should be flashed with an Ubuntu Server image 

Optional

  • A monitor with an HDMI interface
  • HDMI cable if you are using 2-3 Raspberry Pi devices and MicroHDMI cable if you are using 4
  • One Cat5/6 network cable or a Wi-Fi module for each board 
  • One USB keyboard
  • One cluster rack

Make sure all the boards are in the same network and a terminal window that will help you connect with Pi through SSH.

Step 2: Build the Cluster

If you have collected all the essential hardware needed to build a Raspberry Pi Kubernetes cluster, it’s time to set it up. The time for building the cluster depends on how fast you can work. Still, we recommend you set aside at least 45 minutes to build the cluster.

Manage everything before you start building the cluster. Use the PiHut Cluster Case and assemble it. The PiHut Cluster Case will come with a separate instruction guide that will help you assemble it easily. In case you don’t have a rack, connect the Raspberry Pi devices to a power source and the same internet connection. 

Step 3: Setting Up Each Raspberry Pi

For each Raspberry Pi, you will need an Ubuntu server so that you can build a secure connection through SSH. You can follow the whole process from this tutorial which is going to be a huge one. You should follow the guide until you finish installing MicroK8s. 

Once done, come back here and resume following the steps. Also, make sure that MicroK8s is only available for 64-bit Ubuntu images. If your Ubuntu setup is of any other bits, you cannot build a Raspberry Pi cluster with it. When the setup process of Pi devices is done, you should log in to the same devices using the IP address of your network.

Step 4: Installing MicroK8s

You should follow this section for installing MicroK8s for each Raspberry Pi. But before we go any further, SSH into your first Pi, and you should also enable c-groups by modifying the configuration file- /boot/firmware/cmdline.txt:

sudo nano /boot/firmware/cmdline.txt
cgroup_enable=memory cgroup_memory=1

After adding the above two options, you should notice that the full line for a particular Raspberry Pi will look like the below code-

cgroup_enable=memory cgroup_memory=1 net.ifnames=0 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Save the file in your editor and sudo reboot. Once done, you are ready to install the MicroK8s.

sudo snap install microk8s --classic. 

sudo snap install microk8s --classic. You need to know what version of Kubernetes you are installing with this. MicrosK8s will automatically update itself with new releases of the package. Therefore, it follows the upstream Kubernetes releases with attention. While the Kubernetes version is following a specific upstream Kubernetes release, it may select a specific channel during the installation, i.e. to follow the v1.17 series, it may select the below command.

sudo snap install microk8s --classic --channel=1.17/stable

These channels include a track or series, which are stable and include MicroK8s releases (Candidate, Beta, Stable, Edge). If you want to know which MicroK8s releases are running, you can run snap info microK8s. Have a look at the most important MicroK8s command lines:

  • The start command will help you enable the Kubernetes services: microk8s.start
  • The inspect command will show you the status of the services: microk8s.inspect
  • The stop command will then restrict all the ongoing Kubernetes services: microk8s.stop
  • To enable Kubernetes add-ons, you can use the related command lines. For example, to enable “kubedns”, you can use microk8s.enable dns
  • Use the microk8s.kubectl cluster-info command to see the status of the cluster

MicroK8s comes with a great range of Kubernetes add-ons that you can enable and disable at any time with ease. 

Step 5: Master Node and Leaf Nodes

After installing MicroK8s on each board, pick the one that has the potential to be the master node of your cluster. After that, run the following command:

sudo microk8s.add-node

The above command will create a connection string in the <master_ip>:<port>/<token> form.

Run the join command from another Raspberry Pi that you wish to add to the cluster. microk8s.join <master_ip>:<port>/<token>

i.e: microk8s.join 10.55.60.14:25000/JHpbBYMIevZSAMnmjMHmFwanrOYCWZLu

After running the command, you will see the new node in just a few moments on the master. The node will contain the following command:

microk8s.kubectl get node

microk8s.kubectl get nodeYou will have to run the microk8s.add-node command on the master for each new code. Copy whatever the output comes and then run the microk8s.join <master node output>command line on the leaf. If you wish to remove any node for a specific reason, you can run the sudo microk8s remove-node <node name> command. 

You can see the names of the nodes while running the microk8s.kubectl get node command on the master. Or you can leave the cluster from a leaf node by using the sudo microk8s.leave command line.

Now you can modify your Kubernetes cluster as you have set up the Raspberry Pi with MicroK8s. You can add and remove nodes easily, and you can scale up and down whenever you want.

That’s all! Now you’ve got a clear idea regarding how to build a Raspberry Pi Kubernetes Cluster using MicroK8s. Now you can easily build your one and get back to us to share how this guide has helped you. 

 

Leave a Comment