Skip to content

Commit

Permalink
Update the blog to address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Singh <[email protected]>
  • Loading branch information
viveksyngh committed Jul 13, 2020
1 parent c63fa24 commit ed863d7
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions _posts/2020-07-08-event-driven-functions-with-openfaas-and-nats.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Event driven functions with OpenFaaS and NATS"
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
Expand All @@ -12,7 +12,7 @@ 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). 
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.

* [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. 
Expand All @@ -24,25 +24,22 @@ There are several other connectors which allows you to trigger OpenFaaS function

## 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](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).

![NATS Publish Subscribe](/images/2020-openfaas-nats/nats-publish-subscribe.png "https://docs.nats.io/nats-concepts/pubsub")

## 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.
* [arkade](https://github.com/alexellis/arkade) to install OpenFaaS, nats-connector, kubectl and faas-cl. `arkade` is simple CLI tool to install helm charts and apps to your cluster in one command.

> 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` is a tool to run `k3s` in docker. Create a single node `k3s` cluster. You can also use other tools like [KinD](https://github.com/kubernetes-sigs/kind)

```
k3d create
Expand All @@ -54,22 +51,29 @@ Follow instructions returned in the output of this command to configure KUBECONF
Install OpenFaaS using `arkade`

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

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

#### Install NATS connector using helm chart
#### Install NATS connector using arkade
`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
arkade install nats-connector
```

> NATS comes with OpenFaaS installation, so we are not setting up NATS here
#### Install kubectl and faas-cli

helm install nats-connector openfaas/nats-connector --namespace=openfaas
install `kubectl` and `faas-cli` using `arkade` if they are not already installed.

```
arkade get kubectl
```

> NATS comes with OpenFaaS installation, so we are not setting up NATS here
```
arkade get faas-cli
```

#### Login to OpenFaaS gateway using CLI

Expand Down Expand Up @@ -102,29 +106,30 @@ echo -n $PASSWORD | faas-cli login --username admin --password-stdin
Deploy `receive-message` function

```
faas-cli deploy --name receive-message --image openfaas/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
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
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.
## Verify the installation
Invoke `publish-message` function to publish a test message

```
faas-cli invoke publish-message <<< "test message"
echo "test message" | faas-cli invoke publish-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 []
Expand All @@ -137,10 +142,11 @@ Handling connection for 8080
2020-05-29T15:42:24Z 2020/05/29 15:42:24 POST / - 200 OK - ContentLength: 28
```

### What Next ?
## What Next ?
* Do you have a cool "event driven" use case you want to share? Let us know and become a guest blogger!

If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) .
* 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 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.
* 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 comments on commit ed863d7

Please sign in to comment.