Raspberry PI 4 and Docker Series Part I - Setting up the core requirements

Brandon Showers
6 min readFeb 4, 2021

Over the past several months I've been setting up a Raspberry Pi 4 to be both a dev environment as well as a host for a few sites and services I run. I wanted to write a detailed series that will go through what I’ve learned and hopefully help others that are starting their Docker/Kubernetes/Raspberry PI journey.

The Raspberry PI 4 and Docker Series will consist of five parts:

  • Setting up the core requirements (Part I this article)
  • Deploy and configure Traefik reverse proxy (Part II)
  • Deploy and configure Wordpress and MySql (Part III)
  • Deploy and configure Portainer (Part IV)
  • Deploy and configure Kerberos.io NAV for home security (Part V)

In this article I’m going to cover the following:

  • Image the Raspberry PI 4 with x64 OS
  • Install Docker for Arm64
  • Install K3S (Rancher Kubernetes Light)
  • Deploy a simple app to verify functionality

Image the Raspberry PI with x64 OS

The first thing we need to do is get the right OS on our RPi 4. Worth noting, the reason I want to install an x64 OS version of Raspbain is because I’ve found that there are several Docker images that just don’t support ARM32. Now, you may have your reasons for sticking with x32 and that’s fine, but for the sake of my use cases I need x64.

Step 1: Download the Raspberry PI imaging tool from here.

Step 2: Download the latest x64 version of Raspberry OS (as of this writing it’s 2020–08–20-raspios-buster-arm64) from here.

Step 3: Install Raspberry Pi Imager to a computer with an SD card reader. Put the SD card you’ll use with your Raspberry Pi into the reader and run Raspberry Pi Imager.

Select “CHOOSE OS” and choose “Use Custom” from the last option in the dropdown.

Select the 2020–08–20-raspios-buster-arm64.zip that you downloaded in step 2.

Now Select “CHOOSE SD” and choose the card you inserted into the reader. (this card will be erased and overwritten)

Finally, press the “WRITE” button to begin the process of imaging your SD card.

This process takes several minutes!

Once the imaging is complete you will need to insert the SD card into your Raspberry PI and power on. You will need to connect the RPi to your computer monitor, keyboard, and mouse then follow the setup instructions for first use. (the default username is pi and password is password)

To finalize your setup make sure you enable ssh.

Install Docker for Arm64

Now that our Raspberry Pi 4 is setup with the x64 OS lets install Docker!

Install Docker:

curl -sSL https://get.docker.com | sh
sudo usermod -aG docker pi
reboot

Install Dependencies:

sudo apt-get update
sudo apt-get install -y libffi-dev libssl-dev
sudo apt-get install -y python3 python3-pip
sudo apt-get-remove python-configparser

Install Docker Compose:

sudo pip3 -v install docker-compose

Test:

docker --help
docker-compose --help

credit: https://dev.to/rohansawant/installing-docker-and-docker-compose-on-the-raspberry-pi-in-5-simple-steps-3mgl

Install K3S (Rancher Kubernetes Light)

Installing Kube on your Pi is optional as the orchestration benefits of Kubernetes running on a single node is not entirely useful. However, I wanted to have Kube available for testing and learning purposes.

While researching how to get Kubernetes running on my Pi I came accross several approaches. After trial and error with kubeadm, minikube, microk8s, manuall k8s install, and K3S. I found K3S to be the easiest and cleanest approach. K3S is a trimmed down deployment of Kubernetes developed and owned by Rancher Labs.

Following the docs at Rancher.com here are the steps:

Run the install script

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

After running this installation:

  • The K3s service will be configured to automatically restart after a node reboots or if the process crashes or is killed
  • Additional utilities will be installed, including kubectl, crictl, ctr, k3s-killall.sh, and k3s-uninstall.sh
  • A kubeconfig file will be written to /etc/rancher/k3s/k3s.yaml and the kubectl installed by K3s will automatically use it

To enable running the kubectl command without needing sudo:

sudo chown -R $USER /etc/rancher/k3s/k3s.yaml

Finally, you need to setup your docker hub secret for K3S if you plan to leverage the public docker hub repository:

kubectl create secret docker-registry dockersecret --docker-server=https://index.docker.io/v1/ --docker-username=yourusername --docker-password=yourpassword --docker-email=youremailaddress

substitute yourusername, yourpassword, and youremailaddress in the above command with the appropriate information for your docker hub account.

Since this is really a tutorial on a single node deployment, I’m not covering the clustering aspect. However, to add nodes:

Install on worker nodes and add them to the cluster, run the installation script with the K3S_URL and K3S_TOKEN environment variables. Here is an example showing how to join a worker node:

curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken sh -

Setting the K3S_URL parameter causes K3s to run in worker mode. The K3s agent will register with the K3s server listening at the supplied URL. The value to use for K3S_TOKEN is stored at /var/lib/rancher/k3s/server/node-token on your server node.

Once you’re finished you can verify K3S is setup by using kubectl

kubectl get nodes
kubectl cluster-info

Kubernetes and Docker are now installed on your Raspberry Pi!

Deploy a simple app to verify functionality

Now lets test our new environment out and make sure it’s functioning.

I’m going to deploy the NGINX image using both a Kubernetes deployment and service as well as a Docker container.

Kubernetes

First lets create a simple deployment file called demo.yaml and save it locally:

apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
labels:
role: webserver
spec:
replicas: 1
selector:
matchLabels:
role: webserver
template:
metadata:
labels:
role: webserver
spec:
containers:
- name: demo
image: nginx
ports:
- containerPort: 80

Now we can create the deployment using kubectl apply

kubectl apply -f demo.yaml

To check the status:

kubectl get all

Here is a screenshot of my system

Now lets create a service and test it out

kubectl expose deployment demo --type=NodePort --port=80 --target-port=80kubectl get all

We can see that the demo service is exposed on NodePort 31802. So lets test from a browser.

Success!

Docker

Now lets test using vanilla Docker commands.

Run a new NGINX container with docker run

docker run -dit --name demoDocker -p 80:8080 nginxdocker ps

You should see the following output

Now lets test again from our browser

Success!!!

You now have a fully functioning Docker/Kubernetes environment running on your Raspberry Pi! I hope this tutorial was useful and my intent was to pull in all of the required steps to a single tutorial to save you some time meandering across the internet.

Stay tuned for Part II as I’ll walk you through deploying and configuring a phenomenal dynamic reverse proxy (Traefik) for your new Raspberry Pi Docker/Kube environment to make exposing your services/ports seamless.

Until then, take care and be well.

--

--

Brandon Showers

Technology fanatic, leadership enthusiast, and all around decent human being. *The statements and opinions on this site are my own and only my own.