|
| 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-08-31 |
| 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 the [connector-sdk](https://github.com/openfaas-incubator/connector-sdk).The connector sdk provides a reusable and tested interface and implementation that allows developers to quickly create a new event source. The code that is unique to OpenFaaS is standardized so that the develop can focus on the details of the receiving message from the event source. |
| 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 subject (sometimes called topics in other event systems). |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +Before we start we need a couple of tools to help us quickly set up our environment: |
| 34 | + |
| 35 | +* [docker](https://docs.docker.com/get-docker/) - required to use KinD |
| 36 | +* [kind](https://kind.sigs.k8s.io/docs/user/quick-start/) - a tool for running local Kubernetes clusters using Docker |
| 37 | +* [arkade](https://github.com/alexellis/arkade) - official installer for OpenFaaS, that supports many other applications for Kubernetes |
| 38 | + |
| 39 | +> Please make sure you have these tools installed before you proceed next. |
| 40 | +
|
| 41 | +## Install Components |
| 42 | + |
| 43 | +#### Create cluster |
| 44 | + |
| 45 | +First thing we need is running Kubernetes cluster: |
| 46 | + |
| 47 | +```bash |
| 48 | +kind create cluster |
| 49 | +``` |
| 50 | + |
| 51 | +Wait for the installation to finish run this command to verify that the cluster is ready: |
| 52 | + |
| 53 | +```bash |
| 54 | +kubectl -n kube-system rollout status deployment/coredns |
| 55 | +``` |
| 56 | + |
| 57 | +#### Install OpenFaaS |
| 58 | +Install OpenFaaS using `arkade` |
| 59 | + |
| 60 | +``` |
| 61 | +arkade install openfaas |
| 62 | +``` |
| 63 | + |
| 64 | +#### Install NATS connector using arkade |
| 65 | +`nats-connector` is connector which invokes OpenFaaS function when a message is sent to a NATS topic. |
| 66 | + |
| 67 | +``` |
| 68 | +arkade install nats-connector |
| 69 | +``` |
| 70 | + |
| 71 | +> NATS comes with OpenFaaS installation, so we are not setting up NATS here |
| 72 | +
|
| 73 | +#### Install kubectl and faas-cli |
| 74 | + |
| 75 | +install `kubectl` and `faas-cli` using `arkade` if they are not already installed. |
| 76 | + |
| 77 | +``` |
| 78 | +arkade get kubectl |
| 79 | +``` |
| 80 | + |
| 81 | +``` |
| 82 | +arkade get faas-cli |
| 83 | +``` |
| 84 | + |
| 85 | +#### Login to OpenFaaS gateway using CLI |
| 86 | + |
| 87 | +Port forward the gateway service to access it. |
| 88 | + |
| 89 | +``` |
| 90 | +kubectl port-forward -n openfaas svc/gateway 8080:8080 & |
| 91 | +``` |
| 92 | + |
| 93 | +Get the password |
| 94 | + |
| 95 | +``` |
| 96 | +PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo) |
| 97 | +``` |
| 98 | + |
| 99 | +Login to the gateway |
| 100 | + |
| 101 | +``` |
| 102 | +echo -n $PASSWORD | faas-cli login --username admin --password-stdin |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Deploy functions |
| 107 | +`nats-connector` repo comes with test functions to verify installation. It has two OpenFaas functions. |
| 108 | + |
| 109 | +`receive-message` function subscribes to NATS topic nats-test. On every new message on nats-test topic, receive-message will be invoked. |
| 110 | + |
| 111 | +`publish-message` function publishes message to nats-test topic. |
| 112 | + |
| 113 | +Deploy functions |
| 114 | + |
| 115 | +``` |
| 116 | +faas-cli deploy -f https://raw.githubusercontent.com/openfaas-incubator/nats-connector/master/contrib/test-functions/stack.yml --read-template=false |
| 117 | +``` |
| 118 | + |
| 119 | +> Note: You can also build and deploy this function using the stack.yml and code present in nats-connector repository. |
| 120 | +
|
| 121 | +## Verify the installation |
| 122 | +Invoke `publish-message` function to publish a test message |
| 123 | + |
| 124 | +``` |
| 125 | +echo "test message" | faas-cli invoke publish-message |
| 126 | +``` |
| 127 | + |
| 128 | +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. |
| 129 | + |
| 130 | +``` |
| 131 | +faas-cli logs receive-message |
| 132 | +
|
| 133 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stderr from function. |
| 134 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stdout from function. |
| 135 | +2020-05-29T15:41:17Z Forking - ./handler [] |
| 136 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 OperationalMode: http |
| 137 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Timeouts: read: 10s, write: 10s hard: 10s. |
| 138 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Listening on port: 8080 |
| 139 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Writing lock-file to: /tmp/.lock |
| 140 | +2020-05-29T15:41:17Z 2020/05/29 15:41:17 Metrics listening on port: 8081 |
| 141 | +2020-05-29T15:42:24Z 2020/05/29 15:42:24 stderr: 2020/05/29 15:42:24 received "test message" |
| 142 | +2020-05-29T15:42:24Z 2020/05/29 15:42:24 POST / - 200 OK - ContentLength: 28 |
| 143 | +``` |
| 144 | + |
| 145 | +## What Next ? |
| 146 | +* Do you have a cool "event-driven" use case you want to share? Let us know and become a guest blogger! |
| 147 | + |
| 148 | +* If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) . |
| 149 | + |
| 150 | +* 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. |
| 151 | + |
| 152 | +* If you are looking to contribute to open source project, please join OpenFaaS community [slack channel](https://docs.openfaas.com/community/) and start contributing. |
0 commit comments