Raspberry Pi orchestration

In this Instructable I would like to share my personal work-flow for working with headless Raspberry Pi.

If you are interested in automating the setup and deployment process for one Pi or many this is the post for you.

All the code is available on github: Raspberry Pi orchestration

Please giv

e feedback as this is my first post and first python project.

Many thanks to HackBergen club and Verkstedet hackerspace in Bergen, Norway for help, space and hardware to test my project.

Thanks to Instructables for sending Raspery Pi 2 in quantity which allowed to create big network for testing.Raspberry Pi orchestration

Step 1: Short history

link on the cover picture to preserve copyright

I love RPi as a fast way to solve personal problems. Make a media center, maybe an internet radio, NAS or whatever.

I never tried running any desktop on RPi or connected keyboard to it. But to use it this way is not always easy. The fastest way I found was to use Google Coder project. It was so fast. Burn the image to SD card and connect ethernet cable or WiFi dongle and you are good to go. But as all Raspbian solutions it needs 4 Gb sd card. I try to re use old cards or make projects as small as possible.

What I did for myself is a Frankenstein of different projects to make it possible to find all the raspberry pi's on the network, install any distro of your choice to sd card or USB flashdrive and them provision it as you need. And if you have apps to deploy you can do it too.

Step 2: What you need

  • Raspberry pi, any model will do, this script can be changed to run on all of them. But it is much simpler if you don't use A or A+ because they don't have Ethernet port.
  • Internet, and fast one if you don't want to wait long. For first runs all the files will be downloaded from external sources.
  • Any computer running OS X, Linux or Windows. All steps can be reproduced on any of the platforms, some modifications to the code needed.
  • Know basic python.

This tutorial is intended for people who worked with Raspberry Pi before, or is looking for any DevOps solution.

If you are only starting with Raspberry Pi try:

– https://www.raspberrypi.org/

– https://googlecreativelab.github.io/coder/

– http://www.instructables.com/tag/type-id/category-…

for guides for first start.

Step 3: Structure

This is a big project for me, so I'll have to divide it into parts.

here is the first draft of what I wanted to achieve:

  • rpi_detector
    • discovery module, use any way to detect ip by partial mac address
    • use the ip to create ansible inventory
    • optional
      • test for standard login methods # integrated in rpi_ssh
      • ssh with password
      • ssh with key
  • rpi_inventory
    • use detector to get list of RPi
    • rpi_ssh
    • use templates for role definition
    • generate ansible inventory based on a template
  • rpi_ssh
    • test for default ssh login methods
      • ssh with password
        • if true continue to switch to key, else ssh with key
      • ssh with key : if policy mandates switch to key, else exit
      • switch to key
        • use pre-defined root key
        • generate root key
        • generate keys for defined users
        • generate authorised_keys files
        • copy to defined folders
  • rpi_create # will be used to deploy OS on raspberry pi with net-installer
    • use template to generate
      • installer-config.txt
      • post-install.txt
    • copy needed files (authorized-keys, hosts, rules etc.)
    • update/change boot-loader
    • if local repository is used verify accessibility, update and verify needed packages
    • restart all affected machines to start creation process
      • – use base image if available
  • rpi_provission
    • if used as stand alone app:
      • rpi_detector
      • rpi_inventory
    • else
      • run update, upgrade playbook
      • run role provision playbooks
  • rpi_deploy
    • playbook for environment verification
    • playbook for deployment
    • playbook for deployment verification
  • rpi_monitor
    • monitor demon for controlling cluster status
    • failure detection and re-install

Next steps will describe how I managed and sometimes failed in thees steps.

I use OS X as a development platform, but tried to test all modules on Windows and Linux. I down't have full solutions for those platforms right now, hope to get them in the future.

Step 4: OS

To do anything with Raspberry Pi you need to install minimal software.

OSs for Raspberry Pi come in different flavors, you can find some on official website or search for β€œraspberry pi os”.

I use alternative way to get very small and custom os installed : raspbian net installer.

Pros:

  • very minimal install, in minimal server configuration uses ~380 Mb.
  • customizable. You can define packages needed.
  • Simple install, copy files to FAT partition on SD card and plug it to RPi
  • Can install to USB flash drive or other media if needed
  • It is possible, after first run, re-install OS without taking out SD card or touching RPi via ssh.

Cons:

  • Needs internet to get files from repository, if you deploy many machines it will consume lots of traffic. For this case it is recommended to have local clone of repository with necessary packages and use Ansible to add additional ones on per-machine basis.
  • Takes time. If used over Internet speed of install will depend on downlink.

You can run this once and create preferable base installation, create an image of it and use that next time.

NB ! When you create image don forget to clean up. Delete package manager cache and other unnecessary files.

Also, I spent an hour with eth0 unavailable until I figured out that I need to delete /etc/udev/rules.d/70-persistent-net.rules before creating image as it stores MAC address for the Ethernet module.

Yes, it is possible to use base image to deploy os over network.(Will add if there is interest)

To define installer parameters and install additional packages use installer-config.txt. Put it on /boot partition of SD card you plug to your RPi.

If you want to use Ansible next add python to your custom installer-config.txt. It would be a good thing to change root password, just in case.Raspberry Pi orchestration schematic

Step 5: Detector

By now you should have a raspberry pi running, with os of your choice and connected to your network.

There are options how to find Raspberry Pi on the network.

  • nmap – great multi platform tool for network discovery and security auditing. You can scan your network for all connected devices and get ip addresses for RPi. Downside is that you need to install it on OS X and Windows and not all Linux distros have it pre-installed. I use it all the time for different applications and if you like it – use this option for the discovery part.
  • login to your router if possible and look up the ip that was assigned to RPi. Not very scriptable process.
  • if RPi has avahi-daemon running than use Apple zeroconfig, Bonjour in Windows, or avahi-browse in Linux to get ip addresses.
  • Pi Finder.app – Simple Apple script app for discovery.
  • many more

I needed small and fast solution that needs no additional apps, libs for python – β€œarp”. Arp is networking tool available on almost all platforms, definitely on OS X, Windows and Linux. It produces basically same output and readily available through python.

You can see I found 4 RPi's on my network.

There is a caveat with this code. By default arp table is empty and you have to fill it. My monitor server fills it for it constantly in contact with all machines.

Possible solutions:

– use nmap instead, or use it as a solution to fill the arp table .

– add ping loop for the subnet scan

I use arp because nmap needs additional library in python to function properly and parsing nmap output as is not as simple as arp for me.

From her you can pipe it to next stage.

 

For more detail: Raspberry Pi orchestration


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top