Raspberry Pi — Install Docker and Kubernetes

Andrea Scanzani
Digital Software Architecture
7 min readDec 10, 2020
Photo by CHUTTERSNAP on Unsplash

Raspberry Pi was born as a single-board computer for hobby projects, but in recent years many important big-companies have ported their products to the Enterprise level also on the ARM technology of the Raspberry Pi; especially for IoT (Internet of Things) projects.

In this guide we will go to see how it is possible to install Docker and Kubernetes on our Raspberry Pi. For this purpose it is advisable to have the Raspberry Pi 4 Model B with 4 or 8Gb RAM.

Install Docker

Docker

Docker is a software platform that simplifies the deployment process, and their life cycle, of the applications developed. Docker provides applications in units which are called containers. To all intents and purposes, it provides a virtualization of applications with everything necessary for their correct execution (libraries, disk space, networking, etc ..).

Each container on Docker is isolated, independent from the others, and portable to any Docker installation (whatever the host OS).

Below is an illustration of how Docker is composed and how it differs from Virtual Machines:

Container vs Macchine Virtuali
Container vs Virtual Machine

To install Docker on our Raspberry Pi, we can run the automatic installer:

curl -fsSL https://get.docker.com -o get-docker.shsudo sh get-docker.sh

Once the commands have been executed we will have Docker installed on our RPi with 64Bit ARM architecture.

Let’s add our user to the “docker” linux group to execute the commands and proceed with the installation verification:

sudo usermod -aG docker MYUSERsu - MYUSER

Let’s check which version of Docker is installed with the command:

docker --version

And let’s run our first container!

docker run hello-world

and we will have a console output similar to this:

Hello Docker!
Hello from Docker!

To stop and restart Docker the commands are:

systemctl stop dockersystemctl status dockersystemctl start docker

Our Docker installation is complete! We refer to the official documentation for a detailed description of the commands.

Kubernetes — Introduction

Kubernetes logo

Kubernetes is an open-source system that allows you to orchestrate containers and simplify their management. It was initially developed by Google, and works with many containerization systems, including the most famous which is Docker.

As the Official Documentation reports, using Kubernetes has many advantages:

Service discovery and load balancing Kubernetes can expose a container using the DNS name or using their own IP address. If traffic to a container is high, Kubernetes is able to load balance and distribute the network traffic so that the deployment is stable.

Storage orchestration Kubernetes allows you to automatically mount a storage system of your choice, such as local storages, public cloud providers, and more.

Automated rollouts and rollbacks You can describe the desired state for your deployed containers using Kubernetes, and it can change the actual state to the desired state at a controlled rate. For example, you can automate Kubernetes to create new containers for your deployment, remove existing containers and adopt all their resources to the new container.

Automatic bin packing You provide Kubernetes with a cluster of nodes that it can use to run containerized tasks. You tell Kubernetes how much CPU and memory (RAM) each container needs. Kubernetes can fit containers onto your nodes to make the best use of your resources.

Self-healing Kubernetes restarts containers that fail, replaces containers, kills containers that don’t respond to your user-defined health check, and doesn’t advertise them to clients until they are ready to serve.

Secret and configuration management Kubernetes lets you store and manage sensitive information, such as passwords, OAuth tokens, and SSH keys. You can deploy and update secrets and application configuration without rebuilding your container images, and without exposing secrets in your stack configuration.

To install K8s (Kubernetes) on our Raspberry Pi we have 3 options:

  1. MicroK8s
  2. Minikube
  3. k3s

The options we’re going to see are:

  1. k3s → Perfect for running on Raspberry Pi, as it is optimized for IoT devices. It can also be installed on the 32bit version of Raspberry Pi OS. This installation continues our Raspberry Pi configuration started in this article “Raspberry Pi — Headless Configuration”.
  2. MicroK8s → Developed by Canonical, the same company as Ubuntu, and is perfectly integrated with it. It can only run on 64bit machines, and at the time of writing it is only possible to use it with Raspberry Pi running Ubuntu 20.04 LTS at 64bit.

Install Kubernetes with K3s

Before starting the installation we need to make a change and enable cgroups, which is not enabled by default on ARM machines.

Let’s edit the file:

sudo nano /boot/cmdline.txt

and add the following string to the end of the file:

cgroup_enable=memory cgroup_memory=1

Now we can proceed with the installation:

sudo curl -sfL https://get.k3s.io | sh -s - --docker

With the “-docker” statement we are telling K3s to use Docker as the runtime system for Containers.

Once the installation is complete, check the status of the K3s service:

sudo systemctl status k3s

and we can see our node on the Kubernetes cluster:

sudo kubectl get nodes

And we will have an output similar to this:

Nodi Kubernetes
Kubernetes Nodes

The commands to stop and start K3s are as follows:

sudo systemctl stop k3ssudo systemctl status k3ssudo systemctl start k3s

Configuring the Kubernetes Dashboard (K3s)

To enable the Kubernets dashboard we need to run the following commands:

GITHUB_URL=https://github.com/kubernetes/dashboard/releasesVERSION_KUBE_DASHBOARD=$(curl -w '%{url_effective}' -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||')sudo k3s kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/${VERSION_KUBE_DASHBOARD}/aio/deploy/recommended.yaml

The mode to access the interface is via Bearer Token, to retrieve it we must first create an RBAC (Role-based access control) configuration for the Dashboard.

Let’s create the dashboard.admin-user.yml file with the following content:

apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard

Let’s create the dashboard.admin-user-role.yml file with the following content:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard

and then deploy the configurations:

sudo k3s kubectl create -f dashboard.admin-user.yml -f dashboard.admin-user-role.yml

Now to get the Token we can use the command:

sudo k3s kubectl -n kubernetes-dashboard describe secret admin-user-token | grep ^token

We enable the port-forwarding to expose the dashboard to the outside:

sudo k3s kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 10443:443 --address 0.0.0.0

Now the dashboard will be visible from the following url: https://<IP>:10443/

Choosing the Token Authentication mode, and inserting the recovered Token from the command line we will have full access to the Dashboard:

Kubernets Dashboard
Kubernets Dashboard

Note: If you have enabled the ufw firewall on the ubuntu machine, you need to give the following instruction to enable traffic on the cni0 interface (created and used by K3s):

sudo ufw allow in on cni0 && sudo ufw allow out on cni0

Install Kubernetes with MicroK8s

We will continue the installation with MicroK8s, as it was developed by Canonical which is the same company as Ubuntu, and is more integrated into our Operating System that we are using.

To install MicroK8s you must have Ubuntu> = 20.04 64bit installed.

Before installing MicroK8s, we check that we have the ubuntu snap tool available for installing packages:

snap list

Before starting the installation we need to make a change and enable cgroups, which is not enabled by default on ARM machines.

Let’s edit the file:

sudo nano /boot/firmware/cmdline.txt

and add the following string to the end of the file:

cgroup_enable=memory cgroup_memory=1

Now we can proceed with the installation:

sudo snap install microk8s --classic

And as we did for docker we add our user to the microk8s group:

sudo usermod -a -G microk8s MYUSERsudo chown -f -R MYUSER ~/.kubesu - MYUSER

Now running the command to see the Kubernetes Cluster nodes:

microk8s kubectl get nodes

We will see that our node is in a NotReady state, but after a few seconds of waiting it will become in a Ready state.

The commands to stop and start MicroK8s are as follows:

microk8s stopmicrok8s statusmicrok8s start

Configuring the Kubernetes Dashboard (MicroK8s)

o enable the standard Kubernetes Dashboard we must first enable the addons:

microk8s enable dashboard

The mode to access the interface is via Token, which we will recover through the following commands:

token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)microk8s kubectl -n kube-system describe secret $token

Now let’s enable port-forwarding to expose the dashboard to the outside:

microk8s kubectl port-forward -n kube-system service/kubernetes-dashboard 10443:443 --address 0.0.0.0

Now the dashboard will be visible from the following url: https://<IP>:10443/

Choosing the Token Authentication mode, and inserting the recovered Token from the command line we will have full access to the Dashboard:

Kubernets Dashboard
Kubernets Dashboard

Note: If you have enabled the ufw firewall on the ubuntu machine, you need to give the following instruction to enable traffic on the cni0 interface (created and used by MicroK8s):

sudo ufw allow in on cni0 && sudo ufw allow out on cni0

--

--

Andrea Scanzani
Digital Software Architecture

IT Solution Architect and Project Leader (PMI-ACP®, PRINCE2®, TOGAF®, PSM®, ITIL®, IBM® ACE).