OpenFaaS® makes it simple to deploy both functions and existing code to Kubernetes.
- moleculer: 0.14.0-beta6
- moleculer-web: 0.8.5
- APP_RABBITMQ_URI: amqp://username:password@localhost:5672 (default)
- APP_RABBITMQ_PREFETCH: 1 (default)
- APP_MOLECULER_LOGGER: true (default)
- APP_MOLECULER_METRICS: true (default)
I choose rabbitmq as broker, over nats ; Because of prefetch, the webui admin, the replay, the deadletter, etc.
Usually I take nats but not this time!
- First you need to know how works openfass
- Second you need to know how work moleculer and moculer-web
Configuration of moleculer with services:
// Load all domains as services
await broker.loadServices()
Configuration of moculer-web service:
// Load API Gateway
broker.createService({
mixins: [ApiService],
settings: {
path: '/',
routes: [{
mappingPolicy: 'restrict',
mergeParams: true,
aliases
}]
}
})
- Create a dummy service with two actions
- Open two routes from moleculer gateway, who match the two actions
- Create the dockerfile
- Create the openfaas deployment
/**
filename: dummy.service.js
**/
module.exports = {
name: 'Dummy',
actions: {
HelloWorldCommand: require('./actions/HelloWorldCommand'),
NotHelloWorldQuery: require('./actions/NotHelloWorldQuery')
}
}
Property metadata.aliases is from moleculer-web, it will expose the actions you want.
/**
filename: dummy.service.js
**/
module.exports = {
name: 'Dummy',
metadata: {
aliases: {
'POST dummy/hello-world': [
'Dummy.HelloWorldCommand'
],
'GET dummy/not-hello-world': 'Dummy.NotHelloWorldQuery'
}
},
actions: {
HelloWorldCommand: require('./actions/HelloWorldCommand'),
NotHelloWorldQuery: require('./actions/NotHelloWorldQuery')
}
}
A very simple thing to do, because moleculer is configure to load all services in services directory, just do this:
# Dockerfile
FROM gperreymond/openfaas-node-moleculer
COPY . services
# stack.yaml
version: 1.0
provider:
name: openfaas
gateway: [url of the openfass admin]
functions:
dummy-service:
lang: dockerfile
image: [docker hub image name]
handler: [path of you service in the repository]
# some env to overwrite
environment:
APP_RABBITMQ_URI: amqp://admin:password@rabbitmq.docker.localhost:5672
APP_RABBITMQ_PREFETCH: 2
# mandatory for blue/green
annotations:
com.openfaas.health.http.path: "/hc"
com.openfaas.health.http.initialDelay: "30s"
[dummy]
[actions]
HelloWorldCommand.js
NotHelloWorldQuery.js
.dockerignore
Dockerfile
dummy.service.js
stack.yaml