serverless笔记

serverless笔记

1. Serverless Overview

whatisserverless

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?

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

OpenWhisk github Repo

IBM Cloud Function Doc

System Overview

2.1 Key Concept

IBM cloud function terminology

Action
action-1
action-2
action-3
actions
Trigger
trigger
Rule
rules
Packages
packages

System Package ibmcloud fn package list /whisk.system

packages
/whisk.system/slack shared
/whisk.system/samples shared
/whisk.system/utils shared
/whisk.system/websocket shared
/whisk.system/weather shared
/whisk.system/cloudant shared
/whisk.system/cos-experimental shared
/whisk.system/alarms shared
/whisk.system/messaging shared
/whisk.system/pushnotifications shared
/whisk.system/watson-textToSpeech shared
/whisk.system/github shared
/whisk.system/combinators shared
/whisk.system/watson-speechToText shared
/whisk.system/watson-translator shared

2.2 IBM cloud function Tooling

cloudfn_cli

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


ibmcloud fn action create handler handler.js

ibmcloud fn action list

ibmcloud fn action invoke --result handler

create trigger


ibmcloud fn trigger create every-10-seconds \
--feed /whisk.system/alarms/alarm \
--param cron "*/10 * * * * *"

ibmcloud fn trigger list

ibmcloud fn trigger get every-10-seconds

create rule - binding trigger & action

ibmcloud fn rule create invoke-periodically every-10-seconds handler

ibmcloud fn rule list

ibmcloud fn rule get invoke-periodically

Clean up

ibmcloud fn rule disable invoke-periodically

ibmcloud fn rule delete invoke-periodically

ibmcloud fn action delete handler

ibmcloud fn trigger delete every-10-seconds

#### 2.3.2 cloudantDB Trigger

https://github.com/apache/incubator-openwhisk-package-cloudant

ibmcloud fn package get --summary /whisk.system/cloudant

DB Binding

One time work

source db.env

ibmcloud fn package bind /whisk.system/cloudant "cloudant-zhenghb" \
--param username "$CLOUDANT_USERNAME" \
--param password "$CLOUDANT_PASSWORD" \
--param host "$CLOUDANT_USERNAME.cloudant.com"

ibmcloud fn package get cloudant-zhenghb parameters

ibmcloud fn package get --summary cloudant-zhenghb


ibmcloud fn action invoke /_/cloudant-zhenghb/write --blocking --result --param dbname testdb --param doc "{\"_id\":\"NO1\",\"name\":\"haibei\"}"

ibmcloud fn action invoke /_/cloudant-zhenghb/read --blocking --result --param dbname testdb --param id NO1

Create the trigger

ibmcloud fn trigger create cloudant-trigger --feed /_/cloudant-zhenghb/changes \
--param dbname testdb

Using an action sequence and a change trigger to process a document from a Cloudant database


ibmcloud fn action create db_handler db_handler.js

ibmcloud fn action create actionSequence --sequence /_/cloudant-zhenghb/read,db_handler

ibmcloud fn rule create cloudantChangeRule cloudant-trigger actionSequence

Test

Update doc. then sequence actionSequence should be activated:

  1. read doc
  2. 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


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"}'


update

ibmcloud fn action update hellorest --web-secure secret


curl --verbose -X POST \
-H "Content-Type: application/json" \
-H "Accept:application/json" \
-H "X-Require-Whisk-Auth:secret" \
-k https://us-south.functions.cloud.ibm.com/api/v1/web/zhenghb_test/default/hellorest \
-d '{"name": "haibei"}'


3. local env setup

Local Dev env settingup using vagrant (https://github.com/apache/incubator-openwhisk/tree/master/tools/vagrant)

4. Limitation & Restriction

System Limits

serverless_systemlimit

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...