adventures in apache openwhisk...2019/01/20  · apache openwhisk • self-hosted. deploy to: -...

44
Adventures in Apache OpenWhisk Rob Allen January 2019 Slides: https://akrabat.com/5507

Upload: others

Post on 01-Oct-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Adventures in ApacheOpenWhisk

Rob Allen  

January 2019 Slides: https://akrabat.com/5507

Page 2: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Apache OpenWhisk

Rob Allen ~ @akrabat

OpenWhisk is a cloud platform that executes code
in response to events
Page 3: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Apache OpenWhisk• OpenSource• http://openwhisk.org• Incubator project working towards graduation• Many contributors from multiple companies

Rob Allen ~ @akrabat

Page 4: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Apache OpenWhisk• Self-hosted. Deploy to:

- Kubernetes- Mesos- OpenShift- Dev: Vagrant & Docker Compose

• Multiple public providers:- IBM- RedHat- Adobe (for Adobe Cloud Platform APIs)

Rob Allen ~ @akrabat

Page 5: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Supported languages• .NET Core • Ballerina• Go • Java• NodeJS • PHP• Python • Ruby• Swift

Also, your own Docker container can be deployed & invoked

Rob Allen ~ @akrabat

Page 6: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Concepts

Rob Allen ~ @akrabat

Terminology
**Trigger:**
an event (e.g. incoming HTTP request)
**Rule:**
Map a trigger to an action
**Action:**
A function (with optional parameters)
**Package:**
Group of actions and parameters
**Sequence:**
A set of actions in a row
Page 7: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Event providers (feeds)• Alarms (scheduled tasks)• CouchDB• GitHub• Kafka• Push notifications• RSS

Rob Allen ~ @akrabat

Page 8: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Getting started withOpenWhisk

Rob Allen ~ @akrabat

Page 9: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Hello World

def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name) return {'body': message}

Rob Allen ~ @akrabat

Page 10: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Hello World

def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name) return {'body': message}

Rob Allen ~ @akrabat

Page 11: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Hello World

def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name) return {'body': message}

Rob Allen ~ @akrabat

Marshall inputs - the args array
args contains a bunch of things
* params that you set (set a deploy time or when called at runtime)
* deployed ones can be set to not override
* runtime ones are usually about the event that has happened
e.g - db row id, or posted data from web hook/API
Page 12: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Hello World

def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name) return {'body': message}

Rob Allen ~ @akrabat

Do some work
Page 13: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Hello World

def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name) return {'body': message}

Rob Allen ~ @akrabat

return a result
Must be an dictionary or hash
if JS, can also be a Promise
Page 14: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Deploy to OpenWhisk

$ zip -q hello.zip hello.py

Rob Allen ~ @akrabat

ZIP because you probably have more than one file
Page 15: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Deploy to OpenWhisk

$ zip -q hello.zip hello.py$ wsk action update --kind python:3.7 hello hello.zipok: updated action hello

Rob Allen ~ @akrabat

I'm using update as it will create if it doesn't exist, so the
same script can be reused to redeploy an existing action
--kind specifies the runtime to use
Page 16: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Run it

$ wsk action invoke hello --result --param name Rob

Rob Allen ~ @akrabat

Page 17: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Deploying your action

$ wsk action invoke hello --result --param name Rob{ "body": "Hello Rob!"}

Rob Allen ~ @akrabat

Page 18: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Segue: How did it do this?!

Rob Allen ~ @akrabat

Page 19: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

OpenWhisk's architecture

Rob Allen ~ @akrabat

Nginx: API endpoints
Controller: does the work
Consul for service discovery
CouchDB for storage
Kafka for queuing
Invoker manages the action container
Action container executes your code
Page 20: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Create an action

Rob Allen ~ @akrabat

Page 21: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Invoke an action

Rob Allen ~ @akrabat

Page 22: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Action container lifecycle• Hosts the user-written code• Controlled via two end points: /init & /run

Rob Allen ~ @akrabat

Page 23: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Action container lifecycle• Hosts the user-written code• Controlled via two end points: /init & /run

Rob Allen ~ @akrabat

Page 24: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

End Segue

Rob Allen ~ @akrabat

Page 25: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Web-enabled actionsAccess your action via HTTP: Perfect for web hooksAdd the --web flag:

$ wsk action update --kind python:3.7 --web true \ demo/hello hello.zip

Rob Allen ~ @akrabat

Let's talk about access via the web as it's trivial and arguably more useful
We just add --web true
Page 26: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Web-enabled actionsGet URL and curl it:$ wsk action get --url demo/hellook: got action hellohttps://192.168.1.17/api/v1/web/guest/demo/hello

$ curl https://192…17/api/v1/web/guest/demo/hello?name=Rob{ "body": "Hello World!"}

Rob Allen ~ @akrabat

We can now ask the action for its URL and call it from the web
In this example, I'm using curl
Page 27: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API GatewayWhen you want to do more with HTTP endpoints

• Route endpoint methods to actions (Open API Spec support)• Custom domains• Rate limiting• Security (API keys, OAuth, CORS)• Analytics

Rob Allen ~ @akrabat

Page 28: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API Gateway

Rob Allen ~ @akrabat

Page 29: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API Gateway

$ wsk api create /myapp /hello GET hello

Rob Allen ~ @akrabat

Page 30: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API Gateway

$ wsk api create /myapp /hello GET hellook: created API /myapp/hello GET for action /_/hello

Rob Allen ~ @akrabat

Page 31: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API Gateway

$ curl https://example.com/myapp/hello?name=Rob

Rob Allen ~ @akrabat

Page 32: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

API Gateway

$ curl https://example.com/myapp/hello?name=Rob{ "message": "Hello Rob!"}

Rob Allen ~ @akrabat

Page 33: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Packages• Group actions together• Set parameters used by all actions

$ wsk package update demo$ wsk action update --kind python:3.7 demo/hello hello.zip

Rob Allen ~ @akrabat

Page 34: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Built-in packages• alarms • cloudant• github • jira• kafka • pushnotifications• pushnotifications • slack• utils • watson• weather • websocket

• & any other publicly shared package

Rob Allen ~ @akrabat

These all provide actions you can call to do operations
Page 35: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

SequencesInvoke a set of actions in turn

Rob Allen ~ @akrabat

Wiring together of actions in a pipe
Supports reusing of actions and stringing them together
Page 36: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Composer: Logic for your actions

Rob Allen ~ @akrabat

Page 37: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Composer: Logic for your actions

Rob Allen ~ @akrabat

Page 38: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Composer: It's just codecomposer.sequence( composer.if( 'binday/authenticate', composer.sequence( 'format_input_from_alexa', composer.if( 'binday/validate', 'binday/get_next_bin_day' 'binday/send_failure' ), ), 'binday/send_failure' ), 'binday/format_output_for_alexa')

Rob Allen ~ @akrabat

It's just code with a pretty Electron front end
If it's not in Version Control, it can't be reproduced
Page 39: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Deployment: Serverless FrameworkSet up:$ npm install --global serverless serverless-openwhisk$ serverless create --template openwhisk-php --path app$ cd app$ npm install

Rob Allen ~ @akrabat

Continuing the theme of if it's not in version control, it can't be reproduced
Follow the Infrastructure As Code practice for your deployment too
Page 40: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

serverless.ymlservice: ow-todo-backend

provider: name: openwhisk runtime: php

plugins: - serverless-openwhisk

Rob Allen ~ @akrabat

Page 41: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

serverless.ymlfunctions: list-todos: handler: "src/actions/listTodos.main" name: "todo-backend/list-todos" events: - http: path: /todos method: get add-todo: handler: src/actions/addTodo.main # etc

Rob Allen ~ @akrabat

Page 42: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Deploy$ serverless deployServerless: Packaging service...Serverless: Compiling Functions...Serverless: Compiling Packages...Serverless: Compiling API Gateway definitions...Serverless: Compiling Rules...Serverless: Compiling Triggers & Feeds...Serverless: Compiling Service Bindings...Serverless: Deploying Packages...Serverless: Deploying Functions...Serverless: Deploying API Gateway definitions...[...]

Rob Allen ~ @akrabat

Page 43: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Resources• http://www.openwhisk.org• https://github.com/apache/incubator-openwhisk-workshop• https://serverless.com/framework/docs/providers/openwhisk

Developing Serverless Applicationsby Raymond Camden

Free via https://akrab.at/openwhiskbook

Rob Allen ~ @akrabat

Page 44: Adventures in Apache OpenWhisk...2019/01/20  · Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers:

Thank you!

Rob Allen ~ @akrabat