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

In Part 1 and Part 2 of this series, we have:

  • Built a 4x Raspberry Pi3 cluster
  • Installed Raspbian on each Pi and setup our mini-network
  • Installed Ansible on our Director machine and setup SSH
  • Used Ansible to run package updates and install Docker CE on our Pi's
  • Created a Docker Swarm on our Leader Pi
  • Used Ansible to join our new Swarm

The Swarm (console screenshot)

DockerSwarm

Now in Part 3, we are going to create a Docker Network Overlay and install our OpenFaaS implementation as a Docker Service on our Swarm.

First, let's setup the network overlay so our Swarm members have a local back channel in which to communicate with each other.

pi@gargantua-1a:~$ docker network create --driver overlay --subnet 20.0.1.1/24 bramblenet  

Docker will respond with a message that the network was setup successfully. The command calls the docker network service and passes in the driver, subnet and the name of the network, which I called bramblenet

Let's take a quick look at the network configuration for our overlay using Inspect.

pi@Gargantua-A:~ $ docker network inspect bramblenet  
[
    {
        "Name": "bramblenet",
        "Id": "tsjt7oivdn2ethtcxisfe7kat",
        "Created": "2018-09-01T01:31:12.167599069Z",
        "Scope": "swarm",
        "Driver": "overlay",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "20.0.1.1/24",
                    "Gateway": "20.0.1.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": null,
        "Options": {
            "com.docker.network.driver.overlay.vxlanid_list": "4098"
        },
        "Labels": null
    }
]

Perfect. This looks good so far. Now that we have defined our network overlay, Docker Swarm and Docker Service will utilize this network for services attached to the Swarm automatically.

OK, let's move on now to a awesome service that is an open source version of AWS Lambda.

OpenFaaS

Alex Ellis is the founder of OpenFaas, a framework for building and deploying scalable, serverless functions on Docker and Kubernetes.

There are several pre-built Docker images for our Raspberry Pi3 ARM architecture already available. Let's deploy to the Swarm...

The first thing I do is clone the OpenFaas repo. This is not necessary for the example project, but if you want to deploy serverless functions from a template using the faas-cli, you will need to have a local copy of the repository.

We are going to install a stack of functions from the project stack.yml file in the faas repo.

pi@Gargantua-A:~ $ git clone https://github.com/openfaas/faas  
pi@Gargantua-A:~ $ cd faas  
pi@Gargantua-A:~/faas $ faas deploy  

This command will download the appropriate images for our architecture and deploy the OpenFaaS stack to our Docker Swarm. Once finished, run docker service ls for details.

Docker Service List Open FaaS

From our docker service list, we deployed a Redis Counter, Prometheus (app monitor), nslookup, guid-generator and node info. In the screen shot above, you will notice I have deployed additional serverless functions using Python and Flask.

To get started...

Install the OpenFaaS command line interpreter.

pi@Gargantua-A:~ $ curl -sSL https://cli.openfaas.com | sudo sh  

Deploy a function from a template.

Create a folder.

pi@Gargantua-A:~ $ mkdir myfunctions && cd myfunctions  
pi@Gargantua-A:~ $ faas-cli new --land python-arm hello-python  

Three files are created for you...

~/hello-python/handler.py
~/hello-python/requirements.txt
~/hello-python/hello-python.yml 

To make this super simple, our Python function will simply echo the input.

Edit function handler

def handle(req):  
    print('Hello, you said: {}'.format(str(req))

OK, now let's edit our function's yaml config file...

pi@Gargantua-A:~ $ nano hello-python.yml

provider:  
  name: faas
  gateway: http://localhost:5580

functions:  
  hello-python:
    lang: python-arm
    handler: ./hello-python
    image: faas-hello-python-armhf:latest

And now build the image...

pi@Gargantua-A:~ $ faas-cli build -f ./hello-python.yml  
Successfully tagged faas-hello-python-armhf:latest  
Image: hello-python built.  

Once the function has been deployed, call it with curl:

pi@Gargantua-A:~ $ curl 10.0.1.11:8080/function/hello-python -d "Docker Swarm Rules!"  
Hello, you said: Docker Swarm Rules!  

Or use the OpenFaaS UI.

OpenFaaS User Interface

Further Study:

Play around with the command line in the console. Create these services manually; modify the service, remove the serviced and re-deploy with a updated image. Attach the service to the network.

# Commands to manually create a network service with replica set

pi@Gargantua-A:~ $ docker service create --name redis --replicas=1 alexellis2/redis-arm:v6

pi@Gargantua-A:~ $  docker service create --name counter --replicas=3 --publish 3000:3000 alexellis2/arm_redis_counter

pi@Gargantua-A:~ $  docker service create --name guid1 --replicas=4 --publish=9000:9000 alexellis2/guid-generator-arm:0.1 

# Commands to manage the Docker Service

pi@Gargantua-A:~ docker service update func_gateway  
pi@Gargantua-A:~ docker service ps func_gateway  
pi@Gargantua-A:~ docker node inspect Gargantua-D  
pi@Gargantua-A:~ docker service rm func_prometheus  
pi@Gargantua-A:~ docker node rm Gargantua-D  
pi@Gargantua-A:~ docker push craigderington/python-flask:latest  

In case the network overlay does not get mapped to a specific network service, you can use the docker service update command to add the network to the service later.

docker service update --network-add bramblenet redis  
Wrapping Up

This was a super-fun project to build, install and deploy in our Home Lab. Several important technologies were setup, deployed and tested.

First, Ansible, the automation magician. Control hundreds, or thousands of autonomous minions through SSH.

Next, we set up Docker Community Edition with Docker Swarm, a solid auto-scaling network application architecture that can be deployed practically anywhere; on any hardware; in any environment.

Finally, we explored OpenFaas, the first 100% Open Source serverless functions as a service framework. We deployed several OpenFaas functions as network services on our Docker Swarm using the faas-cli.

I will be using Docker and Ansible in many future projects.

Part 2

Craig Derington

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

comments powered by Disqus