Running Ansible + Docker Swarm + OpenFaaS on Raspberry Pi3 Cluster - Part 2

OK, we have Ansible configured on our Raspberry Pi Cluster, we have copied the SSH keys to each of the Pi's in our stack and we are ready to continue.

If you missed Part 1, please start there; come back once you have Ansible setup and ready to talk to your target devices.

First, let's explore a few very useful ad-hoc commands that can simply be run from the command line.

ansible -m ping all  
ansible -m shell -a 'hostname' all  
ansible -m shell -a 'df -h' all  
ansible -m shell -a 'df -h' all  
ansible -m shell -a 'whoami' all  
ansible -m shell -a "docker ps" all  

A few important ad-hoc command line arguments you need to know about.

-a ad-hoc command, not part of a playbook
-m name of shell module, i.e. shell, gzip, git or virtualenv

When you need to run a command on the hosts as sudo; you must use the command line argument -b (i.e. become sudo).

Add the -b argument. Run commands as sudo

ansible -b -m shell -a 'sudo apt update' all  
ansible -b -m shell -a 'sudo apt install -y redis-server' 192.168.1.252  
ansible -b -m shell -a 'sudo reboot' all  
ansible -b -m shell -a 'sudo apt update' all  
ansible -b -m shell -a 'sudo apt list --upgradable' all  
ansible -b -m shell -a 'sudo apt update' all  
ansible -b -m shell -a 'sudo apt upgrade' all  
ansible -b -m shell -a 'sudo apt upgrade -y' all  

The final part of the ad-hoc command is the target machine (or machines) in which to run the command. Can be 'all' or a specific host or list of hosts, or a group in a hosts file.

We can also run a bit more sophisticated ad-hoc commands for other regular tasks .
Like deploying a Git repo to your any of your targets.

ansible gargantua -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"  

Ansible is very powerful for controlling many servers simultaneously. Definitely makes my life easier each month when it comes time for monthly package updates and server maintenance.

Setting Up Docker Swarm.

Use Ansible to install Docker Community Edition on our stack.

$ ansible -b -m shell -a curl -fsSL get.docker.com -o get-docker.sh && sh get-docker.sh

This simple command will download and install Docker Community Edition on each of our Raspberry Pis.

Once all of the targets have completed downloading and installing Docker CE and reported back to our Director that the commands completed successfully, we are ready to run our first Docker command to ensure the Docker Engine is running correctly.

First, make sure the Docker installer created a docker group.

$ sudo addgroup docker
(this should tell you the group already exists)

Now add our current user to the docker group...

$ sudo gpasswd -a $USER docker

Log in and out and you will be able to run docker without sudo.

Now run "Hello, World" to confirm install.

$ docker run hello-world

The shell will respond with a digest and a Docker message that the Docker Engine is running.

Let's create our Docker Swarm...

$ docker swarm init --advertise-addr 192.168.1.250

The shell should respond with the following...

Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \
    192.168.1.250:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.  

Ok, awesome. This is moving along nicely...

Next, we are ready to have each of the other hosts in the stack join our new Swarm.

Use Ansible to run the following command on Targets B, C and D.

$ ansible -m shell -s "docker swarm join --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx 192.168.1.250:2377" 192.168.1.251 192.168.1.252 192.168.1.253

We now have a working Docker Swarm.

Run docker node ls to see the Swarm members.

pi@Gargantua-A:~/director $ docker node ls  
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION  
ucsvuddlqiex4hude5v3914xj *   Gargantua-A         Ready               Active              Leader              18.06.1-ce  
jn4pm1p825hy2w684d19gqg0l     Gargantua-B         Ready               Active                                  18.06.1-ce  
oopc98fgj3u28o1d9kktnk77b     Gargantua-C         Ready               Active                                  18.06.1-ce  
mgz7vpjx8norub3lfcfuvln0w     Gargantua-D         Ready               Active                                  18.06.1-ce  

In the next chapter, we will create a Docker Service & Docker Network Overlay for our OpenFaaS implementation.

Stay tuned.

Part 1

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