Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active July 29, 2021 18:09
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save alexellis/a7b6c8499d9e598a285669596e9cdfa2 to your computer and use it in GitHub Desktop.
Save alexellis/a7b6c8499d9e598a285669596e9cdfa2 to your computer and use it in GitHub Desktop.
Provision a Raspberry Pi SD card

You'll need

  • Raspberry Pi 3, 3+ or 2 (only)
  • A Linux PC, laptop or Raspberry with SD card reader/slot
  • A number of Raspberry Pis configured with Ethernet

You must have an SSH key, if you don't know what this is then type in ssh-keygen and follow the instructions.

Prepare to provision RPis:

  1. Clone the gist and run chmod +x make-rpi.sh
  2. Only once, run: sudo mkdir -p /mnt/rpi/{boot,root}
  3. Download the Raspbian Lite image and place it in the folder
  4. Populate your public SSH keys in template-authorized_keys on the Linux host. I.e. cp ~/.ssh/id_rsa.pub template-authorized_keys

Flash each Raspberry Pi

  1. Insert an SD card into your SD card reader
  2. Edit make-rpi.sh and update export DEV= to the device you see on lsblk for your SD card reader
  3. Edit template-dhcpcd.conf if your IP range isn't 192.168.0.0/24
  4. Run sudo ./make-rpi node-1 101 to get node-1.local and 192.168.0.101 and so on.

Notes:

  • For security log-in by password is disabled. If you want to log in via password then comment out the lines in make-rpi.sh

Create a cluster

Create a Kubernetes cluster

See my work on k3sup.dev for a fast bootstrap of Kubernetes using k3s.

Create a cluster with Swarm

On each Raspberry Pi run

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

Then on one Raspberry Pi (master) run:

sudo apt update && sudo apt install -qy git
docker swarm init --advertise-addr=eth0

One each of the others run the docker swarm join command that you were issued.

Deploy a distributed system

OpenFaaS provides a way for you to build functions and run them across the capacity of your cluster including metrics, monitoring and auto-scaling with support for many programming languages.

On the master node:

git clone https://github.com/openfaas/faas
cd faas
./deploy_stack.armhf.sh

Now open up the OpenFaaS UI with: http://master-ip:8080/ and click "Deploy Function" to deploy a function from the built-in function store, or follow a tutorial at https://www.openfaas.com.

For help etc contact me via Twitter @alexellisuk or Join @openfaas Slack #arm-and-pi channel.

See also:

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if [ -z $1 ]; then
echo "Usage: ./make-rpi.sh <hostname> <ip-suffix>"
echo " ./make-rpi.sh node-1 101"
echo " ./make-rpi.sh node-2 102"
exit 1
fi
export DEV=sde
export IMAGE=2018-06-27-raspbian-stretch-lite.img
if [ -z "$SKIP_FLASH" ];
then
echo "Writing Raspbian Lite image to SD card"
time dd if=$IMAGE of=/dev/$DEV bs=1M
fi
sync
echo "Mounting SD card from /dev/$DEV"
mount /dev/${DEV}1 /mnt/rpi/boot
mount /dev/${DEV}2 /mnt/rpi/root
# Add our SSH key
mkdir -p /mnt/rpi/root/home/pi/.ssh/
cat template-authorized_keys > /mnt/rpi/root/home/pi/.ssh/authorized_keys
# Enable ssh
touch /mnt/rpi/boot/ssh
# Disable password login
sed -ie s/#PasswordAuthentication\ yes/PasswordAuthentication\ no/g /mnt/rpi/root/etc/ssh/sshd_config
echo "Setting hostname: $1"
sed -ie s/raspberrypi/$1/g /mnt/rpi/root/etc/hostname
sed -ie s/raspberrypi/$1/g /mnt/rpi/root/etc/hosts
# Reduce GPU memory to minimum
echo "gpu_mem=16" >> /mnt/rpi/boot/config.txt
# Set static IP
cp /mnt/rpi/root/etc/dhcpcd.conf /mnt/rpi/root/etc/dhcpcd.conf.orig
sed s/100/$2/g template-dhcpcd.conf > /mnt/rpi/root/etc/dhcpcd.conf
echo "Unmounting SD Card"
umount /mnt/rpi/boot
umount /mnt/rpi/root
sync
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.
# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel
# Inform the DHCP server of our hostname for DDNS.
hostname
# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid
# Persist interface configuration when dhcpcd exits.
persistent
# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit
# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu
# A ServerID is required by RFC2131.
require dhcp_server_identifier
# Generate Stable Private IPv6 Addresses instead of hardware based ones
slaac private
# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1
# It is possible to fall back to a static IP if DHCP fails:
# define static profile
profile static_eth0
interface eth0
static ip_address=192.168.0.100/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8
@flahertyian
Copy link

flahertyian commented May 6, 2020

might want to throw in a mkdir -p /mnt/rpi/{boot,root} to like line 27 of make-rpi.sh or something, script was unable to mount unitl i manually make the dirs

@alexellis
Copy link
Author

Sure, why not @flahertyian?

If you're interested in speaking more and trying netbooting - join OpenFaaS Slack at https://slack.openfaas.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment