Running Ansible + Docker Swarm + OpenFaaS on Raspberry Pi3 Cluster

I built a small 4 x Raspberry Pi3 cluster in the Home Lab last weekend. I want to explore several technologies on this platform. I thought I would share my experience.

Technologies:

  1. Ansible
  2. Docker Swarm, Docker Network Overlay and Docker Service
  3. OpenFaaS, a serverless function based architecture.

Parts List:

4 x Raspberry Pi3
1 x Stackable Dog Bone 4 Layer case Link
4 x 32 GB SD Card
1 x 8 port 60W/12A USB Powered Hub Link
1 x USB network switch Link
5 x 5" Cat5 cables
5 x 6" USB-to-Mini-USB cables

I assembled the Raspberry Pi's in the stackable case.

Connected all the cables.

I powered on the stack and the lights started glowing as the Bramble came to life.

Craig's Raspberry Pi3 Cluster

Starting with the bottom of the stack, I connected the HDMI cable, connected the last open port on the USB powered switch to my primary office switch and finally, connected the keyboard.

I have already extracted NOOBS to each of the 32 GB SD Cards. As I power on the monitor, the NOOBS Welcome Message greets my screen with a friendly glow.

Click Install Raspbian. Once Raspbian is installed, I configure each Raspberry Pi with SSH, update their hostnames and set them to run headless.

Next, I edit the /etc/network/interfaces and set each Pi's static IP.

pi@gargantua-1a:~$ sudo nano /etc/network/interfaces  

In Nano, I edit the file...

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo  
iface lo inet loopback

auto eth0  
iface lo inet static  
    address 192.168.1.250
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.254
    gateway 192.168.1.1
    dns-nameservers 192.168.1.1 8.8.8.8 4.4.4.4

Save the file and reboot the Pi. Repeat these steps for each of the remaining Pi's in the stack. My IP network for the Pi's will be .250, .251, 252 and .253

Now we are ready to deploy Ansible. Ansible will allow me to control the entire stack with one command. This comes in handy when dealing with clusters of servers.

pi@gargantua-1a:~$ sudo apt-get update  
pi@gargantua-1a:~$ sudo apt-get install software-properties-common  
pi@gargantua-1a:~$ sudo apt-add-repository --yes --update ppa:ansible/ansible  
pi@gargantua-1a:~$ sudo apt-get install ansible  

You only need to install Ansible on the Primary Target.

Later, we'll copy SSH keys to our target Pi's and update our Ansible inventory in the config file.

First, I create a working directory to place my ansible.cfg and hosts file. I will run Ansible from this folder. I call mine director. Eventually, my Ansible playbooks will run from this locatioon as well.

pi@gargantua-1a:~$ mkdir director && cd director  
pi@gargantua-1a:~/director $ nano hosts  

Enter the IP's of the Target Pi's in the stack.

[gargantua]
    192.168.1.250
    192.168.1.251
    192.168.1.252
    192.168.1.253

Save the HOSTS file. The name above the IP addresses is the Ansible Group name.

Copy the default Ansible config file

pi@gargantua-1a:~/director $ nano ansible.cfg  

Paste the contents of the clipboard to Nano and Save.

Next, export your SSH public key to Pi's.

pi@gargantua-1a:~/director $ ssh-copy-id pi@192.168.1.251  

Follow the prompts, enter your password and repeat this for each of the IPs in our cluster.

Now we can control our target devices through SSH.

Now, use Ansible ad-hoc command to ping all of your nodes.

pi@gargantua-1a:~$ ansible all -m ping  
192.168.1.250 | SUCCESS => {  
    "changed": false, 
    "ping": "pong"
}
192.168.1.251 | SUCCESS => {  
    "changed": false, 
    "ping": "pong"
}
192.168.1.252 | SUCCESS => {  
    "changed": false, 
    "ping": "pong"
}
192.168.1.253 | SUCCESS => {  
    "changed": false, 
    "ping": "pong"
}

In the next post, we will continue exploring Ansible commands and get Docker Swarm setup.

Part 2

Craig Derington

Espousing the virtues of Secular Humanism, Libertarianism, Free and Open Source Software, Linux, Ubuntu, Terminal Multiplexing, Docker, Python, Flask, Django, Go, MySQL, PostgreSQL, MongoDB and Git.

comments powered by Disqus