Animesh Singh
STSM IBM Cloud Platform
Cloud Expo, Santa Clara, November 2016
@AnimeshSingh
How to build a Distributed Serverless Polyglot Microservices IoT Platform
using Docker and OpenWhisk
@AnimeshSingh
Cloud Computing Evolution
Bare Metal
IaaS
Container Orchestrators
PaaS
@AnimeshSingh
Problem: It’s expensive to scale microservices, even
on a PaaS
Explosion in number of
containers / processes:
1.  Increase of infrastructure
cost footprint
2.  Increase of operational
management cost and
complexity
Region B
Region A
Break-down into microservices
Make each micro service HA
Protect against regional outages
Monolithic application
@AnimeshSingh
Problem: the programming and cost model doesn’t
help
•  Continuous polling needed in the absence of an 

event driven programming model.
•  Charged for resources, even when idle.
•  Worries persist about capacity management.
Swift
Application
Container
 VM
CF
2
Polling
1b
Request
1a
@AnimeshSingh
Bare Metal
IaaS
Container Orchestrators
PaaS
Enter Serverless!
Serverless
@AnimeshSingh
a cloud-native platform
for
short-running, stateless computation
and
event-driven applications
which
scales up and down instantly and automatically
and
charges for actual usage at a millisecond granularity
event
handlersevents
What is Serverless?
@AnimeshSingh
Why is Serverless attractive?
7
Short answer:
Besides
a)  making app development & ops dramatically faster, cheaper, easier (but different J ), it
b)  Drives infrastructure cost savings
(yes – this has been true for most IT innovations over the last decades, while serverless potentially
introduces another step-function change)
@AnimeshSingh
Key factors for infrastructure cost savings
Traditional models (CF,
containers, VMs)
Serverless
High Availability At least 2-3 instances of
everything
No incremental infrastructure
Multi-region deployment One deployment per region No incremental infrastructure
Cover delta between short
(<10s) load spikes and
valleys (vs average)
~2x of average load No incremental infrastructure
Example incremental costs 2 instances x 2 regions x 2 =
8x
1x
8
@AnimeshSingh
What is Serverless good for?
9
Introducing OpenWhisk,
a fabric and platform for
the serverless, event driven
programming model
@AnimeshSingh
What is OpenWhisk?
an open Beta offering in IBM’s Bluemix cloud
@AnimeshSingh
What is OpenWhisk?
an open-source project on github
@AnimeshSingh
Build your apps, your way.
Use a combination of the most prominent open-source compute
technologies to power your apps. Then, let Bluemix handle the rest.
Ease of getting startedFull stack Control
FunctionsPaaSContainersIaaS
What is OpenWhisk?
another way to build apps….
@AnimeshSingh
What is OpenWhisk?
a distributed compute service to execute application logic in response
to events.
@AnimeshSingh
OpenWhisk: How does it work?
Incoming HTTP request, e.g. HTTP GET
openwhisk.ng.bluemix.net/api/v1/<namespace>/actions/getCustomers
1
Browser
Mobile App
Web App
OpenWhisk
2 Invoke associated
OpenWhisk action
„getCustomers“
Swift! DockerJS! Python! Java*!
Variety of
languages
* work in progress
@AnimeshSingh
OpenWhisk: How does it work?
-  API Gateway takes care of…
-  security (authenticate, authorize, threat protect, validate)
-  control (rate limiting, response caching)
-  mediation
-  parameter mapping
-  schema validation
-  … and supports e.g. different verbs (Get, Post, Put, Delete, …)
Incoming HTTP request, e.g. HTTP GET
api-gw.mybluemix.net/…/getCustomers
1
Browser
Mobile App
Web App
APIGateway
2
OpenWhisk
3 Invoke associated
OpenWhisk action
„getCustomers“
Swift! DockerJS! Python! Java*!
* work in progress
Coming
soon...
The OpenWhisk
programming model
What is OpenWhisk?
a high-level serverless programming model
Trigger
Rule
Action
Package
What is OpenWhisk?
a high-level serverless programming model
Trigger
Rule
Action
Package
language support to
encapsulate, share, extend code
first-class
event-driven
programming
constructs
first-class functions
compose via sequences
docker
containers as
actions
all constructs first-class
— powerful extensible
language
@AnimeshSingh
Trigger: A class of events that can happenT
Programming model
@AnimeshSingh
Actions: An event-handler, i.e. code that runs in response to an
event
A
Programming model
@AnimeshSingh
Programming model
Actions: Multi-runtime support, e.g. JavaScriptA
function	main(msg)	{	
	return	{	message:	'Hello,	'	+	msg.name	+	'	from	'	+	msg.place	};	
};
@AnimeshSingh
Actions: Multi-runtime support, e.g. SwiftA
func	main(params:[String:Any])	->	[String:Any]	{		
	var	reply	=	[String:Any]	()		
	if	let	name	=	params[“name”]	as?	String	{	
	 	print(“Hello	(name)”)		
	 	reply[“msg”]	=	“Goodbye	(name)”	
	}		
	return	reply		
}
Programming model
@AnimeshSingh
Actions: In any other language by packaging with Docker
A
Programming model
@AnimeshSingh
Rules: An association of a trigger and an actionR
R := T A
Programming model
@AnimeshSingh
Actions: Can be chained to create sequences to increase
flexibility and foster reuse
A
AA
:= A1
+ A2
+ A3
AB
:= A2
+ A1
+ A3
AC
:= A3
+ A1
+ A2
Programming model
@AnimeshSingh
Packages: A shared collection of triggers and actionsP
A
A read
write
T changes A translate A forecast
A post
T topic
Open
Source A myAction
T myFeed
Yours
T commit
Third
Party
Programming model
OpenWhisk
backend architecture
@AnimeshSingh
github.com
openwhisk/openwhisk
!
!
!
!
core
runtime
CLIpackages
security
features
persistent
store
loggingmonitoring billing
authenticati
on
OpenWhisk on Bluemix
@AnimeshSingh
Edge!
VM!
Edge!
VM!
!
Edge!
!
!
!
!
Edge!
VM!
Edge!
VM!
Master!
!
!
!
!
!
!
!
!
controller
Edge!
VM!
Edge!
VM!
!
Slave!
!
!
!
!
!
!
!
!
!
!
invoker
•  microservices deployed in docker containers!
•  open-source system middleware!
•  NoSQL (CouchDB) persistence!
action
container!
action
container!
action
container!
action
container!
action
container!
action
container!
action
container!
action
container
OpenWhisk Open Source Core Runtime
Why
Docker?
@AnimeshSingh
controller
invoker
Deploying and managing
traditional microservices
1
Why Docker?
@AnimeshSingh
Slave VM
Lightweight isolated execution environment
for arbitrary user code
action container
action container
action container
action container
action container
action container
action container
action container
2
Why Docker?
@AnimeshSingh
Portable description of arbitrary
binary user actions (Dockerfile)
Docker file
3
Why Docker?
@AnimeshSingh
% wsk action invoke hello
in 8 easy steps
How it all works?
POST /api/v1/namespaces/myNamespace/actions/myAction
36
Controller
…
Invoker InvokerInvoker
1!
2!
3,4!
5!
6!
7!
8!
OpenWhisk Topology
@AnimeshSingh
Cloudant
ELK-Stack
Controller
Integration
Service
…InvokerInvokerInvoker
Monitoring
OpenWhisk Topology in Bluemix
@AnimeshSingh
OpenWhisk: Usage of Docker
•  Isolation of actions
•  Control over consumed resources of each container
38
A =
wsk action invoke docker run
≈
@AnimeshSingh
OpenWhisk: Container startup
39
Start container
docker run
Initialize
/init
Run
/run
cold container
@AnimeshSingh
Start container
docker run
Initialize
/init
Run
/run
OpenWhisk: Container startup
40
pre-warmed container
@AnimeshSingh
Start container
docker run
Initialize
/init
Run
/run
OpenWhisk: Container startup
41
warm container
@AnimeshSingh
OpenWhisk: Container startup optimization
•  Performance is king.
42
cold container pre-warmed container warm container
faster
IoT Usecase
Node-RED and MQTT
@AnimeshSingh
CLIENTS
BROKER
@AnimeshSinghSUBSCRIBE
PUBLISH
{…}
{…}
{…}
{…}
@AnimeshSingh
COMMERCIALOPEN-SOURCE
MOSQUITTO
MOSCA
ACTIVEMQ
RABBITMQ
MESSAGESIGHT
HIVEMQ
THINGMQ
CLOUDMQTT
MQTT Brokers
@AnimeshSingh
MQTT NODES IN NODE-RED
@AnimeshSingh
DEVICES NODE-RED
BROKER
@AnimeshSinghDEVICES
NODE-RED
BROKER
OpenWhisk Feeds
and creating MQTT feeds
@AnimeshSingh
Trigger
Action
Action
Action
push, fork, comment, ..
CUSTOM FEED EXAMPLE
@AnimeshSingh
anybody can create a Package with a feed
/whisk.system/github /mynamespace/github
MQTT feeds for OpenWhisk
•  Bridging MQTT messages to OpenWhisk Actions can be achieved by creating a new Feed provider. This provider
would subscribe to message topics and execute registered Triggers with incoming messages.
•  The custom feed provider would need to establish and maintain long-lived MQTT connections, waiting for
messages to arrive. This requirements means the Feed provider needs an external service to handle managing
these connections, it won’t be possible within the Feed Action.
•  This feed provider service is implemented using Node.js, using Cloudant for the database. The service listens for
HTTP requests, with Trigger registration details, from the Feed Action. The Node.js MQTT library is used to
subscribe to registered topics. When messages are received, the OpenWhisk client library is used to invoke the
Trigger remotely, passing the message contents as event parameters.
•  This service provider is packaged using Docker. Pushing this image into the IBM Containers registry, the Feed
provider can be started on IBM Bluemix using the Containers service.
http://jamesthom.as/blog/2016/06/15/openwhisk-and-mqtt/
MQTT feeds for OpenWhisk
•  This service provider is packaged using Docker.
•  Pushing this image into the IBM Containers registry, the Feed provider can be started on IBM
Bluemix using the Containers service.
Registering Feeds
http://jamesthom.as/blog/2016/06/15/openwhisk-and-mqtt/
Node-RED for Serverless
OpenWhisk
@AnimeshSingh
Node-RED and OpenWhisk
•  Community effort to integrate with open tools: NodeRED
•  NodeRED is all about automating flows to orchestrate calls to
different service APIs
•  Usually triggered by calls from external systems or devices
•  Runs within a long-running single node process, with a dedicated amount of CPU & memory
being allocated
•  OpenWhisk is all about executing code (custom logic) in response to events on a scalable platform,
with a dedicated amount of CPU & memory being allocated per-request
•  NodeRED and OpenWhisk complement each other
•  Use NodeRED to graphically create flows, automating a series of tasks in a kind of workflow
•  Use OpenWhisk to execute custom logic (requiring some kind of CPU- or memory bound
operation) triggered from within NodeRED
@AnimeshSingh
•  Use Node-RED to create Multi cloud Serveless Applications!
Use Node-RED to compose Serverless Applications
$ wsk property set --apihost
openwhisk.ng.bluemix.net --auth 96294c7b-
e6f4-4ccf --namespace “animeshsingh"
$ wsk action invoke /whisk.system/utils/
echo -p message hello --blocking --result
{
"message": "hello"
}
@AnimeshSingh
Devices
Node-RED & OpenWhisk
MQTT
Broker Trigger
Action
Action
Action
Bringing it all together – Serverless IoT Applications
Summary
@AnimeshSingh
What you learned today
•  We’re in the early days of an evolution that is empowering developers
to write cloud native applications better, faster, and cheaper
•  OpenWhisk provides an open source platform to enable cloud native,
serverless, event driven applications
•  OpenWhisk, MQTT and Node-RED can be used together to create
IoT Serverless applications
@AnimeshSingh
Experiment with OpenWhisk on Bluemix
bit.ly/ow-bm
@AnimeshSingh
Join us to build a cloud native platform for the future!
OpenWhisk.org
dwopen.slack.com #openwhisk
bluemix.net
OpenWhisk and MQTT http://jamesthom.as/blog/2016/06/15/openwhisk-and-mqtt/
Thank You

How to build a Distributed Serverless Polyglot Microservices IoT Platform using Docker and OpenWhisk