Setting up redis and nat-connector with FaasD
Storage for your functions
- tags
- redis
- nats
- openfaas
Contents
Lets look at how we expand our faasd
server to include a couple of
services. We'll first add a redis server and some simple ruby code to
add a counter, and then we'll build and add the nats-connector
to be
able publish and subscribe to events.
About /var/lib/faasd/docker-compose.yaml
This looks like a docker-compose
file, and I believe that it actually
does use the same code that parses the file of that name, but it
doesn't use docker
to set up the containers. This is what faasd
does.
The biggest difference I've noticed is how it treats networking. The
short of it is that you need to use the ip address of the main faasd
gateway to access the servers, not the name of the service in the
file. So, instead of pointing your client to redis
or gateway
you
point them to 10.62.0.1
.
Redis
Setting up Redis
First thing is we need to log into our faasd server, and create the storage directory with the right user.
|
|
Then we need to add the redis instance in /var/lib/faasd/docker-compose.yaml
:
|
|
Note that if you get a "Background saving error" this is probably because you have an open redis port and someone is scanning you.
The 10.62.0.1
is the default network that faasd
sets up.
Then we restart:
|
|
We can look at the logs using:
|
|
Writing our function
Back on our machine, lets create a simple ruby function that increments and returns the value from redis.
|
|
Inside of the counter directory, we need to add the redis gem:
|
|
And now we can write our handler:
|
|
And inside of our counter.yml
file we can add the following
environment variable so we don't hard code the address of the redis
instance in our container.
|
|
You'll want to replace that wschenk
with your docker user.
Then we can load it:
|
|
And run it:
|
|
And you should see a number go up and up!
Publishing and subscribing to messages
The nats-connector is a way to have functions called when messages are sent to certain topics in your nats instance. Lets see how that works.
Installing the nats-connector
:
I couldn't find the latest version of nats-connector
on docker, so
here's how to build it:
|
|
Pretty simple. (Change wschenk
to be your username.)
Setting up docker-compose.yaml
Back on your faads
server, lets first open up nats
to our functions
and then add in the nats-connector
:
This section should already be there:
|
|
And then add:
|
|
Be sure to change the image name to your build, if you don't want to use mine.
Publish
|
|
And then a simple handler
|
|
And then in stack.yml
:
|
|
Receive
|
|
And inside of stack.yml
add an annotations section
|
|
We don't need to do anything particular with this handler, we just want to look at the logs to see if it gets triggered.
Running
On your dev machine:
|
|
Then on the faasd
server:
|
|
(Or if you want to be more targeted:)
|
|
And on your client machine:
|
|
References
- https://github.com/openfaas/faasd
- https://gumroad.com/l/serverless-for-everyone-else
- https://github.com/docker-library/redis/issues/128
- https://github.com/docker-library/redis/issues/44#issuecomment-274287156
- https://github.com/openfaas/nats-connector
- https://twitter.com/alexellisuk/status/1318841881439199233/photo/1
Previously
Next