Serverless at the Edge: Up and Running w/ OpenFaaS & Docker on a Raspberry Pi Multi-Node Cluster with PiBakery
This guide will focus on getting you up and running with Serverless Functions as a Service (FaaS) and a Raspberry Pi (RPi) cluster as quickly as possible.
If you want a guide that gives you a bit more experience with the RPi command line and gives you an overview of Serverless as an architectural design, then please visit Alex Ellis’s guide (https://blog.alexellis.io/your-serverless-raspberry-pi-cluster/). My guide focuses on automating the RPi provisioning through PiBakery get you up and running a little quicker.
Also if you want to learn more about OpenFaaS and Serverless please checkout Alex Ellis’ talk from KubeCon this last December.
- Pre-Requisites for Mac and Windows
- Flashing your micro SD cards with PiBakery and Deploying Docker Swarm
- Install OpenFaaS
- Deploy your first function with the Function Store
- Deploy a function with the OpenFaaS CLI program.
Pre-Requisites for Mac and Windows
You will need a few things on your laptop before getting started.
- At Least 2 Raspberry Pi development boards.
- Power supplies for RPis, ethernet cables (if using wired internet access), and micro SD cards for the file system for RPis.
- PiBakery on your laptop. If you are on Mac you can use this guide http://www.pibakery.org/docs/install-mac.html to install. If you are on Windows and you have the Chocolately package manager installed, use the choco install pibakery command from the command line. Or you can use this guide http://www.pibakery.org/docs/install-win.html for Windows setup.
- Git installed on your laptop. If you have the Homebrew package manager installed on Mac or Chocolatey installed for Windows on your laptop can use the command brew install git or choco install git. Or you can follow this guide to install git on the operating system of your choice https://git-scm.com/book/en/v2/Getting-Started-Installing-Git.
- Windows Only: make sure you have a program like Putty or if you are on Windows 10, make sure you have the Linux Subsystem app installed so you can SSH into the Raspberry Pi master. You can use the command choco install putty to install or use this guide https://www.ssh.com/ssh/putty/windows/install. Use the following guides if you want to install the Linux Subsystem app on Windows 10…https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/ and https://docs.microsoft.com/en-us/windows/wsl/install-win10
Flashing your micro SD cards with PiBakery and Deploying Docker Swarm
Open up a command line window and clone the repository with the PiBakery recipes we will need.
Make note of where this directory is being stored on your computer (e.g. /<location>/<of git>/<repo>/openfaas_pibakery_config or c:\<location>\<of git>\<repo>\openfaas_pibakery_config)
Open up your PiBakery program. You will be required to open it up with elevated permissions, in both Windows and Mac. Click the Import button.
Navigate to the file folder where you cloned the openfaas_pibakery_config directory and open the multi_node_swarm folder. Select the OpenFaaS_master.xml file and open it. Below is what the imported file should look like.
This configuration requires you to have internet connectivity as soon as it is booted to install all of the packages on first boot. You can edit this configuration to have WiFi credentials if you wish to not use a wired connection. However, I have not always had the best of luck with RPi clusters over WiFi. So I typically choose to leave the configuration as is and use a wired connection.
Insert your micro SD card into your laptop computer. Click this Write button on PiBakery.
Select the appropriate SD card if you have more than one connected to your computer. Then select Raspbian Lite for the Operating System.
Click Write. This process will take a few minutes to complete.
Remove the SD card from your computer and and insert it into your first RPi. This will end up being your Master node for swarm.
Plug your ethernet cable into your RPi and a network switch/router.
Plug the power cable into the RPi. This is will take a few minutes to complete. After the RPi reboots, we will SSH tunnel into the RPi to check.
SSH into the RPi using the hostname set by PiBakery.
Password is raspberry.
Once you are logged in we can check to make sure docker swarm was setup properly by typing…
sudo docker node ls
Once docker swarm is running it should list our first node as leader.
After we have verified docker swarm is running on our master node, we need to get the join-token for the worker nodes to be connected to our swarm cluster. While still being SSHed into the RPi, type the following command in the terminal window…
sudo docker swarm join-token worker
Your token should look similar to this…
docker swarm join --token SWMTKN-1-0uz05xzumtazh0rmlkro2pxy8e6882jd9wkmm49w4ptcc0ucr8-3pna808fpoawa0axnu4t5a7aa 172.16.99.13:2377
We will use this token to setup our worker node with PiBakery.
In PiBakery click the import button and in the same directory we imported the OpenFaaS_master.xml file, we will import the OpenFaaS_worker.xml file.
Using the join-token command we retrieved from the master node, we will paste that command with sudo in the last run command in PiBakery.
Note: Please make sure the sudo command is inserted in the last command, otherwise PiBakery will not be able to auto join the docker swarm cluster.
sudo docker swarm join --token SWMTKN-1-0uz05xzumtazh0rmlkro2pxy8e6882jd9wkmm49w4ptcc0ucr8-3pna808fpoawa0axnu4t5a7aa 172.16.99.13:2377
Click the Write button again and once the process is complete eject the SD card, insert the SD card into your second RPi, connect the ethernet cable, and last the power cable. Repeat this step as many time as you need for however many worker nodes you will include in your cluster.
It will take a few minutes for your worker(s) to join the swarm. While still being SSHed into the master RPi, you can use the same node command as before to check for when each worker node joins the cluster.
sudo docker node ls
Once your node(s) have joined the cluster, you are ready to install OpenFaaS.
Using the SSH terminal window for the master RPi, change your current current directory to faas.
Then we will type the command to install OpenFaaS on your Docker Swarm Cluster.
Now let’s check to see that all of the OpenFaaS services are up and running.
sudo docker service ls
Open a browser and open the OpenFaaS UI.
Congrats! You now have a Docker Swarm cluster on your RPi with OpenFaaS ready to deploy your Serverless functions.
Deploy your first function with the Function Store
Now we will deploy our first function with the function store. The function store is a collection of community built functions that you can deploy with one click.
On the OpenFaaS web interface in your browser, click on the Deploy New Function button.
Select the figlet and then click the Deploy button.
Select the deployed Figlet function on the left panel, write a message in the request body, then click the Invoke button to execute the function.
You can also make the request use curl on the command line to make the request as well.
curl -X POST \
-d 'Hello Again, From TEAM SERVERLESS'
Deploy a function with the OpenFaaS CLI program.
Now that we have deployed our first function, let’s use the OpenFaaS CLI program to build and deploy a simple Golang function.
The OpenFaaS CLI program was already installed on the RPi master node from PiBakery, so no additional downloads will be required. From the command line we can just call it with the faas-cli command.
Log back into the RPi master node and create a directory called functions and then move into that director.
mkdir functions && cd functions
Then use faas-cli to pull some sample templates for functions.
faas-cli template pull
Next create a new function.
faas-cli new rpi-go --lang go-armhf
Now move into the rpi-go directory.
Open the handler.go file with nano.
Edit the text in the quotes to change the message to include, Welcome to TEAM SERVERLESS!!!
Type control-o to save the file and then control-x to exit.
Move up a directory back to the functions directory.
Now build the function with the sudo command.
sudo faas-cli build -f rpi-go.yml
Now deploy the function to OpenFaaS.
faas-cli deploy -f rpi-go.yml --gateway http://127.0.0.1:8080
Now call the function using curl.
curl -X POST \
-d 'I did it!'
YOU DID IT! You now have what you need to build Serverless functions quickly on your newly minted RPi Cluster.
Please go check out the GitHub repository for OpenFaaS and join the community. And don’t forget to star OpenFaaS on Github!