Skip to content

Commit c63fa24

Browse files
committed
Add NATS connector blog
Signed-off-by: Vivek Singh <[email protected]>
1 parent 9fdd23a commit c63fa24

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: "Event driven functions with OpenFaaS and NATS"
3+
description: Vivek outlines how you can make use of NATS to trigger your functions in OpenFaaS through the new connector-sdk component.
4+
date: 2020-07-08
5+
image: /images/kafka-connector/aluminium-building.jpg
6+
categories:
7+
- NATS
8+
- tutorial
9+
- examples
10+
author_staff_member: vivek
11+
dark_background: true
12+
---
13+
14+
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.
15+
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)
16+
17+
* [kafka-connector](https://github.com/openfaas-incubator/kafka-connector) connects OpenFaaS functions to Kafka topics.
18+
* [nats-connector](https://github.com/openfaas-incubator/nats-connector) an OpenFaaS event-connector to trigger functions from NATS. 
19+
* [mqtt-connector](https://github.com/openfaas-incubator/mqtt-connector) MQTT connector for OpenFaaS.
20+
* [cron-connector](https://github.com/openfaas-incubator/cron-connector) triggers OpenFaaS functions based on cron events. 
21+
* [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.
22+
23+
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.
24+
25+
## NATS
26+
27+
[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.
28+
29+
![NATS Publish Subscribe](/images/2020-openfaas-nats/nats-publish-subscribe.png "https://docs.nats.io/nats-concepts/pubsub")
30+
31+
## Pre-requisite Installation
32+
33+
* [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.
34+
* `kubectl` to manage kubernetes cluster.
35+
* `helm` to install `nats-connector` using helm chart
36+
* [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.
37+
* [faas-cli](https://github.com/openfaas/faas-cli) CLI tool to manage OpenFaaS functions.
38+
39+
> Please make sure you have these tools installed before you proceed next.
40+
41+
## Install Components
42+
43+
#### Create a kubernetes cluster
44+
45+
`k3d` is a tool to run `k3s` in docker. Create a single node `k3s` cluster.
46+
47+
```
48+
k3d create
49+
```
50+
51+
Follow instructions returned in the output of this command to configure KUBECONFIG to access the cluster using kubectl.
52+
53+
#### Install OpenFaaS
54+
Install OpenFaaS using `arkade`
55+
56+
```
57+
arkade install openfaas --set basic_auth=true --set functionNamespace=openfaas-fn
58+
```
59+
60+
Follow instructions printed to obtain the gateway password and login with `faas-cli`
61+
62+
#### Install NATS connector using helm chart
63+
`nats-connector` is connector which invokes OpenFaaS function when a message is sent to a NATS topic.
64+
65+
```
66+
helm repo add nats-connector https://openfaas.github.io/faas-netes
67+
68+
helm install nats-connector openfaas/nats-connector --namespace=openfaas
69+
70+
```
71+
72+
> NATS comes with OpenFaaS installation, so we are not setting up NATS here
73+
74+
#### Login to OpenFaaS gateway using CLI
75+
76+
Port forward the gateway service to access it.
77+
78+
```
79+
kubectl port-forward -n openfaas svc/gateway 8080:8080 &
80+
```
81+
82+
Get the password
83+
84+
```
85+
PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
86+
```
87+
88+
Login to the gateway
89+
90+
```
91+
echo -n $PASSWORD | faas-cli login --username admin --password-stdin
92+
```
93+
94+
95+
## Deploy functions
96+
`nats-connector` repo comes with test functions to verify installation. It has two OpenFaas functions.
97+
98+
`receive-message` function subscribes to NATS topic nats-test. On every new message on nats-test topic, receive-message will be invoked.
99+
100+
`publish-message` function publishes message to nats-test topic.
101+
102+
Deploy `receive-message` function
103+
104+
```
105+
faas-cli deploy --name receive-message --image openfaas/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
106+
```
107+
108+
Deploy `publish-message` function
109+
```
110+
faas-cli deploy --name publish-message --image openfaas/publish-message:latest --fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
111+
```
112+
113+
> 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.
114+
115+
Invoke `publish-message` function to publish a test message
116+
117+
```
118+
faas-cli invoke publish-message <<< "test message"
119+
```
120+
121+
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.
122+
123+
```
124+
faas-cli logs receive-message
125+
126+
ARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
127+
Handling connection for 8080
128+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stderr from function.
129+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stdout from function.
130+
2020-05-29T15:41:17Z Forking - ./handler []
131+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 OperationalMode: http
132+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Timeouts: read: 10s, write: 10s hard: 10s.
133+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Listening on port: 8080
134+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Writing lock-file to: /tmp/.lock
135+
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Metrics listening on port: 8081
136+
2020-05-29T15:42:24Z 2020/05/29 15:42:24 stderr: 2020/05/29 15:42:24 received "test message"
137+
2020-05-29T15:42:24Z 2020/05/29 15:42:24 POST / - 200 OK - ContentLength: 28
138+
```
139+
140+
### What Next ?
141+
142+
If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) .
143+
144+
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.
145+
146+
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

0 commit comments

Comments
 (0)