Tuesday, April 19, 2016

Installing Chef on A Raspberry Pi 2/3


Introduction

So you’ve got 1,103 Raspberry Pis that you need to manage. Two things:
  1. Why?
  2. Wanna hang out Saturday? No? You’re busy managing all your mini computers manually? I can help you with that!
RaspChef_Big

Scope

In this article we’ll cover installing and configuring the Chef version 12 client on a Raspberry Pi. This has been tested on Raspberry Pi versions 2 and 3; in theory it should work on a 1 as well, albeit  slowly.

Assumptions

  • Raspberry Pi with Rasbian, Hypriot, or similar build with connections to interwebs
  • Chef server/org you would like to point clients to
  • Chef workstation capable of bootstrapping clients; if needed see this excellent article by Digital Ocean.

Execution

The main point of this article is really the installation of Ruby, which is the foundation on which Chef is based. Because the Ruby package in the Rasbian locations is out of date (2.1 as of this writing) we need to compile our own from source. Chef 12 requires Ruby 2.0 or greater, but Rack, which is installed with Chef, requires 2.2.2. UPDATE 7/9/2017: Ruby 2.4 or newer is now needed to continue successfully. Thanks Mike (from comments below)!

Step 1: Install Ruby

Clearly this should be scripted for optimal efficiency, but for learning purposes we’ll do it step by step to see exactly what is going on first hand. Log onto your Raspi via SSH and execute the following:
  1. Most commands we’ll be executing require root, so let’s elevate our session:
    sudo su
    
    Where sudo = "super user do" and su ="super user"; using root privs to assume root identity.

  2. Pull the newest package lists from configured repositories to ensure we get the newest packages:
    apt-get update
    
    2016-05-07 00_03_01-_home_toby_
  3. Install pre-requisites for Ruby: gcc, make, and libssl-dev with their dependencies. 
    apt-get install gcc make libssl-dev
    
    2016-05-07 00_06_47-_home_toby_
    Note: On some distros, such as Raspbian, gcc and make are installed by default. It won't hurt to include the in the command line none the less, and including it here will cover most distros.

  4. Download the Ruby source to the /usr/src directory:
    cd /usr/src
    wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.5.tar.gz
    
    Note: 2.2.5 is the newest 2.2 version at the time of this writing, but you should make sure there isn't a newer version avialable. Check the Ruby page here.

  5. Extract the source & navigate to the directory (Make sure you update filenames/directory names for differing versions):
    tar -xvzf ruby-2.2.5.tar.gz
    cd ruby-2.2.5
    
  6. Prepare to compile with configure, omitting unnecessary components:
    ./configure --enable-shared --disable-install-doc --disable-install-rdoc --disable-install-capi
    
    2016-05-07 00_12_03-_home_toby_
    Note: This will take between 2 and 10 minutes depending on which Pi and the speed of your SD card.

  7. Compile it using make!
    make -j4 ; make install
    
    2016-05-07 15_28_19-Window
    Note: "make -j4" will multi-thread the execution, using each of the Raspi's processors. This will take between 15 and 30 minutes depending on your Pi and SD card.
hot_hot_hot

Step 2: Install Chef

Now we’ll use the gem install command to get Chef
  1. Execute gem install chef as root (sudo if not).
    gem install chef
    
    Note: This will take between 5 and 25 minutes depending on which Pi, SD card, and network connection.

    2016-05-07 16_44_05-Window
  2. Relinquish root privileges as they are no longer needed. This should only exit the root session and not the SSH session itself. If you’re logged in directly as root ignore this, but don’t do that next time!
    exit
    
  3. Test the install to ensure it worked
    chef-client --version
    
    2016-05-07 16_46_29-Window

Step 3: Configure Chef

For this step move to your Chef workstation and logon using your account that is configured to manage your organization. 
  1. Use the knife command to boostrap the newly installed client
    knife bootstrap rasdock02.truckchase.lan -N rasdock02.truckchase.lan -x {user} -P {password}
    
    Where: {user} is a user on the target platform with root privs and {password} is the password for that account.

    Note: It is normal to see errors on the first portion on the bootstrap since the Chef ARM client will not be found in the Chef repo, but the second phase should work utilizing the client we just installed.

    2016-05-07 16_50_56-Window

That’s it! For further verification you can check against the Chef server using your workstation (knife client show {node name}) or even better yet, use Chef Manage if you have it available.

2016-05-07 16_53_23-Chef Manage

References

  • Chef Documentation
  • Ruby Documentation
  • 7 comments:

    Howard said...

    Excellent guide; thanks. I've been messing with a Chef Server on AWS and a small number of Raspberry Pi's (Zero's) to experiment and learn in the course of building an IoT project. This guide worked perfectly on the zero's, albeit it took a few hours. It's a real shame / ironic you have to spend 2.5h to get the pi to a chef-controlled state, but I'm glad I did. Nevertheless really appreciate this writeup, it's just what i needed.

    Unknown said...

    Thanks, your guide worked perfectly on my Raspberry Pi 3. Also monitored the temperature of the CPU and it never went above 112 F. Build steps each took about 8 minutes.

    Toby Meyer said...

    Thanks, I'm glad it worked for you!

    Unknown said...

    Your version of Ruby is deprecated and throws an error. Changing to a 2.4+ version fixed it with no other changes to your documentation.

    Toby Meyer said...

    Thanks @Mike! Updated the article with your information.

    Unknown said...

    Have this list of copy-pasta commands kind of defeats the purpose of chef, does it not. You can modify the https://github.com/RPi-Distro/pi-gen repo to make it build the exact base image you want (unbooted, not yet expanded, because everything is done via a QEMU chroot). I have a concise demonstration of this at https://github.com/RichardBronosky/pi-gen-extender

    Anonymous said...

    Great blog post.You can refer to the post and know how to install and configure Chef version on a Raspberry pi case. Thanks for the post.