Skip to content

Commit

Permalink
Add NATS connector blog
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Singh <[email protected]>
  • Loading branch information
viveksyngh committed Jul 8, 2020
1 parent 9fdd23a commit 2eaea65
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions _posts/2020-07-08-event-driven-functions-with-openfaas-and-nats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
---
title: "Event driven functions with OpenFaaS and NATS"
description: Vivek outlines how you can make use of NATS to trigger your functions in OpenFaaS through the new connector-sdk component.
date: 2020-07-08
image: /images/kafka-connector/aluminium-building.jpg
categories:
- NATS
- tutorial
- examples
author_staff_member: vivek
dark_background: true
---

In this blog post, I will show how you can invoke OpenFaaS function in response to messages sent on NATS topics in publish-subscribe model.
OpenFaaS functions are accessible over HTTP endpoints via gateway service but OpenFaaS provides several other way to invoke OpenFaaS functions with help of [connector-sdk](https://github.com/openfaas-incubator/connector-sdk)

* [kafka-connector](https://github.com/openfaas-incubator/kafka-connector) connects OpenFaaS functions to Kafka topics.
* [nats-connector](https://github.com/openfaas-incubator/nats-connector) an OpenFaaS event-connector to trigger functions from NATS. 
* [mqtt-connector](https://github.com/openfaas-incubator/mqtt-connector) MQTT connector for OpenFaaS.
* [cron-connector](https://github.com/openfaas-incubator/cron-connector) triggers OpenFaaS functions based on cron events. 
* [VMware vCenter connector](https://github.com/openfaas-incubator/openfaas-vcenter-connector) an OpenFaaS event-connector built to consume events from vCenter and to trigger functions.

There are several other connectors which allows you to trigger OpenFaaS functions based on events, are written and managed as a third party project. Please refer to this [link](https://docs.openfaas.com/reference/triggers/) for full list.

## NATS

[NATS](https://nats.io) is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures. In this blog post, I will be using NATS publish-subscribe concept where publishers publishes a message on a topic/subject and subscribers consumes that message by subscribing to that topic/subject.

![NATS Publish Subscribe](/images/2020-openfaas-nats/nats-publish-subscribe.png)

## Pre-requisite Installation

* [k3d](https://github.com/rancher/k3d) to create a [k3s](https://github.com/rancher/k3s) cluster. k3s is lightweight distribution of kubernetes from Rancher Labs.
* `kubectl` to manage kubernetes cluster.
* `helm` to install `nats-connector` using helm chart
* [arkade](https://github.com/alexellis/arkade) to install OpenFaaS. `arkade` is simple CLI tool to install helm charts and apps to your cluster in one command.
* [faas-cli](https://github.com/openfaas/faas-cli) CLI tool to manage OpenFaaS functions.

> Please make sure you have these tools installed before you proceed next.
## Install Components

#### Create a kubernetes cluster

`k3d` is a tool to run `k3s` in docker. Create a single node `k3s` cluster.

```
k3d create
```

Follow instructions returned in the output of this command to configure KUBECONFIG to access the cluster using kubectl.

#### Install OpenFaaS
Install OpenFaaS using `arkade`

```
arkade install openfaas --set basic_auth=true --set functionNamespace=openfaas-fn
```

Follow instructions printed to obtain the gateway password and login with `faas-cli`

#### Install NATS connector using helm chart
`nats-connector` is connector which invokes OpenFaaS function when a message is sent to a NATS topic.

```
helm repo add nats-connector https://openfaas.github.io/faas-netes
helm install nats-connector openfaas/nats-connector --namespace=openfaas
```

> NATS comes with OpenFaaS installation, so we are not setting up NATS here
#### Login to OpenFaaS gateway using CLI

Port forward the gateway service to access it.

```
kubectl port-forward -n openfaas svc/gateway 8080:8080 &
```

Get the password

```
PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
```

Login to the gateway

```
echo -n $PASSWORD | faas-cli login --username admin --password-stdin
```


## Deploy functions
`nats-connector` repo comes with test functions to verify installation. It has two OpenFaas functions.

`receive-message` function subscribes to NATS topic nats-test. On every new message on nats-test topic, receive-message will be invoked.

`publish-message` function publishes message to nats-test topic.

Deploy `receive-message` function

```
faas-cli deploy --name receive-message --image openfaas/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
```

Deploy `publish-message` function
```
faas-cli deploy --name publish-message --image openfaas/publish-message:latest --fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
```

> Note: You can also build and deploy this function using the stack.yml and code present in nats-connector repository. But for simplicity, I am using pre built and published images of these functions.
Invoke `publish-message` function to publish a test message

```
faas-cli invoke publish-message <<< "test message"
```

When `publish-message` was invoked, it would have pushed `test-message` to the `nats-test` topic on NATS, which would have invoked `receive-message`. We can verify that by checking logs of `receive-message` function.

```
faas-cli logs receive-message
ARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stderr from function.
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stdout from function.
2020-05-29T15:41:17Z Forking - ./handler []
2020-05-29T15:41:17Z 2020/05/29 15:41:17 OperationalMode: http
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Timeouts: read: 10s, write: 10s hard: 10s.
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Listening on port: 8080
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Writing lock-file to: /tmp/.lock
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Metrics listening on port: 8081
2020-05-29T15:42:24Z 2020/05/29 15:42:24 stderr: 2020/05/29 15:42:24 received "test message"
2020-05-29T15:42:24Z 2020/05/29 15:42:24 POST / - 200 OK - ContentLength: 28
```

### What Next ?

If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) .

If you don't find connector for messaging platform you are using, checkout the [connector-sdk](https://github.com/openfaas-incubator/connector-sdk) which allows you to build event-connectors for OpenFaaS.

If you are looking to contribute to open source project, please join OpenFaaS community [slack channel](https://docs.openfaas.com/community/) and start contributing.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2eaea65

Please sign in to comment.