Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functions as a Service with Apache OpenWhisk and OpenShift

Functions as a Service with Apache OpenWhisk and OpenShift

Presented at Red Hat Summit 2018


Brendan McAdams

May 10, 2018

More Decks by Brendan McAdams

Other Decks in Technology


  1. Functions as a Service with Apache OpenWhisk and OpenShift Brendan

    McAdams bmcadams@redhat.com 1
  2. Functions as a Service (aka "serverless") — Deploy code functions

    easily - no server setup1 — Event Driven - respond to events by invoking functions — Functions are loaded and executed On Demand — BYO Programming Language — Use a hosted platform with AWS Lambda, Red Hat Cloud Functions2, etc… — or, be adventurous: build your own OpenWhisk install! 2 Red Hat's OpenShift based version of OpenWhisk 1 Once your serverless platform is running 2
  3. Introducing Apache OpenWhisk — Fully Open Source, Apache 2 Licensed

    — Functions (Actions) are the centerpiece of your code — Deploy on the cloud (public or private!) of your choice 3
  4. Introducing Apache OpenWhisk Functions — Functions can be written in

    any supported language including Node, Java, and Python — Use your functions for cool stuff — Event Triggers (e.g., call it when the contents of an S3 bucket change) — Chain them, composing small functions together for larger behavior 4
  5. OpenWhisk & Red Hat — Red Hat is working to

    improve and expand upon Apache OpenWhisk, including the integration of our software portfolio — Recently contributed Kubernetes support upstream — Needed to run OpenWhisk on OpenShift — Created templates to simplify setup of OpenWhisk on OpenShift https:// github.com/projectodd/openwhisk-openshift — Working on AMQP Feed Support for OpenWhisk 5
  6. Loading OpenWhisk on OpenShift — We provide a template for

    loading OpenWhisk into OpenShift, which installs and configures all of the components: oc process -f https://git.io/openwhisk-template | oc create -f - 6
  7. OpenWhisk Actions — An Action is the basic unit of

    work with OpenWhisk, containing your function — Actions are stateless — Invoking an action generations an "activation id" that can be used to retrieve results — Actions can be chained together using sequences 7
  8. Sample Action hello.py def main(args): name = args.get('name', 'stranger') greeting

    = 'Hello ' + name + '!' return {'greeting': greeting} 8
  9. Sample Action hello.py def main(args): name = args.get('name', 'stranger') greeting

    = 'Hello ' + name + '!' return {'greeting': greeting} 9
  10. Sample Action hello.py def main(args): name = args.get('name', 'stranger') greeting

    = 'Hello ' + name + '!' return {'greeting': greeting} 10
  11. Creating an Action hello.py To create an action, we need

    to give it a name and configure it in OpenWhisk: $ wsk -i action create helloPy hello.py ok: created action helloPy 11
  12. Invoking an Action hello.py Once it has a name, an

    action can be invoked directly: $ wsk -i action invoke --result helloPy --param name "Brendan McAdams" { "greeting": "Hello Brendan McAdams!" } 12
  13. Action Sequences mungeHello Multiple functions can be chained together using

    sequences where the output of a function is the input to a subsequent function*. Let's use this function to change the input name before calling hello… def main(args): name = args.get('name', 'Nosuch Person').split(" ") name.reverse() return {'name': ", ".join(name)} * Make sure your output and input parameter names match up! 13
  14. Action Sequences mungeHello Our new function just splits the input

    name into "Surname, First Name": $ wsk -i action invoke --result alphaName --param name "Brendan McAdams" { "name": "McAdams, Brendan" } 14
  15. Action Sequences mungeHello To create a sequence, the individual actions

    must already be created. Their names are the keys for the new sequence. With this setup, alphaName is called first, with its output being fed to helloPy. The response from OpenWhisk is the output of helloPy. $ wsk -i action create mungeHello --sequence alphaName,helloPy ok: created action mungeHello 15
  16. Action Sequences mungeHello Invoking a sequence is no different than

    a normal action: just call it by the sequence name: $ wsk -i action invoke --result mungeHello --param name "Brendan McAdams" { "greeting": "Hello McAdams, Brendan!" } 16
  17. Triggers & Rules — Triggers are channels for events —

    Triggers can be linked, via rules, to one or more actions — Example - A trigger named uploadPhoto might, when invoked, call several actions: nsfwFilter, generateThumbnail, and parseLocation — When the uploadPhoto trigger is called, the rule forwards the invocation parameters to each of the 3 linked actions. 17
  18. Feeds & Packages — OpenWhisk supports the idea of pre-packaging

    functionality in a package — Packages bundle together Actions and Feeds, allowing us to bundle related code to deploy an event driven workflow — Feeds are similar to Triggers, but act as a stream receiving multiple event types — Packages allow us to bundle together related code to easily deploy a event driven workflow — Prebuilt packages include support for Alarms, RSS Feeds, Kafka Integration, and more. 18
  19. Feeds & Packages Feeds — Packages can also provide feeds,

    which configure external event sources. — Example: OpenWhisk includes a prebuilt 'alarms' package which can let us create cron-like repeating behaviors. wsk trigger create interval \ --feed /whisk.system/alarms/interval \ --param minutes 2 \ --param trigger_payload "{\"name\":\"Odin\",\"place\":\"Asgard\"}" \ --param stopDate "2019-01-31T23:59:00.000Z" 19
  20. Questions? Brendan McAdams <bmcadams@redhat.com> 20