serverless笔记
serverless笔记
1. Serverless Overview

Serverless computing allows you to build and run applications and services without thinking about servers.
Building a serverless application allows you to focus on your application code instead of managing and operating infrastructure.
1.1 Why serverless?

Pros
No need to explicitly provision/maintenance for servers
Got billed by millisecond of execution time (rounded up to the nearest 100 ms)
Inherent auto-scaling , high availability out of the box, all that you need to do is to provide the code you want to execute
Cons
Vendor lock-in
Testing
Cold start Latency
Decisions about how small (granular) the function should be, take the time to assess, implement and test. It gets cumbersome to manage too many functions, and ignoring granularity will end up creating mini-monoliths.
Vendor
- AWS Lambda
- Google Cloud Functions
- Microsoft Azure Functions
- IBM Cloud function (OpenWhisk)
2. IBM Cloud Function/Openwhisk
2.1 Key Concept
IBM cloud function terminology
Action




Trigger

Rule

Packages

System Package ibmcloud fn package list /whisk.system
packages |
2.2 IBM cloud function Tooling
GUI - helloworld (No suitable for non-trivial tasks)

2.3 IBM cloud function Features
ibmcloud fn activation poll
real-time tailing events/actiavation
2.3.1 Timer
ibmcloud fn package get --summary /whisk.system/alarms
create action named handler
create trigger
ibmcloud fn action create handler handler.js
ibmcloud fn action list
ibmcloud fn action invoke --result handler
|
create rule - binding trigger & action
ibmcloud fn rule create invoke-periodically every-10-seconds handler |
Clean up
#### 2.3.2 cloudantDB Trigger
ibmcloud fn rule disable invoke-periodically
ibmcloud fn rule delete invoke-periodically
ibmcloud fn action delete handler
ibmcloud fn trigger delete every-10-seconds
https://github.com/apache/incubator-openwhisk-package-cloudant
ibmcloud fn package get --summary /whisk.system/cloudant |
DB Binding
One time work
source db.env |
|
Create the trigger
ibmcloud fn trigger create cloudant-trigger --feed /_/cloudant-zhenghb/changes \ |
Using an action sequence and a change trigger to process a document from a Cloudant database
|
Test
Update doc. then sequence actionSequence
should be activated:
- read doc
- db_handler: print doc andc return value succeed:true
Cloud trigger with filter
https://github.com/apache/incubator-openwhisk-package-cloudant#listening-for-changes-to-a-cloudant-database
https://github.com/apache/incubator-openwhisk-package-cloudant#create-the-trigger-using-the-filter-function
cleanup
ibmcloud fn rule disable cloudantChangeRule
ibmcloud fn rule delete cloudantChangeRule
ibmcloud fn trigger delete cloudant-trigger
ibmcloiud fn action delete db_handler
ibmcloiud fn action delete actionSequence
2.3.3 Web Action - HTTP
https://github.com/apache/incubator-openwhisk/blob/master/docs/webactions.md
create
update
ibmcloud fn action create hellorest hello_rest.js --web true
ibmcloud fn action get hellorest --url
curl --verbose -X POST \
-H "Content-Type: application/json" \
-H "Accept:application/json" \
-k https://us-south.functions.cloud.ibm.com/api/v1/web/zhenghb_test/default/hellorest \
-d '{"name": "haibei"}'
ibmcloud fn action update hellorest --web-secure secret |
3. local env setup
Local Dev env settingup using vagrant (https://github.com/apache/incubator-openwhisk/tree/master/tools/vagrant)
4. Limitation & Restriction

No Silver Bullet!
The serverless code can be used in conjunction with code written in traditional server style, such as microservices. For example, part of an application could be written as microservices using compute instances and another part could be written as serverless code. Alternatively, very few application could be written that uses no provisioneds ervers at all, being completely serverless
Vendor-lockin, Cold start time; No suitable for long running task. et...