基于OpenWhisk的无服务器计算,第1部分


这是Priti Desai的一系列翻译笔记。 由于注释本身很短,因此将一次出版多个出版物。 将考虑带有示例的OpenWhisk应用程序部署的详细信息,并在本周期结束时,将介绍使用基于OpenWhisk的无服务器计算的应用程序的体系结构和代码。


自动化OpenWhisk的应用程序部署


您是否已经安装了OpenWhisk并在工作中测试了简单的hello.js函数


cat hello.js function main() { return {payload: 'Hello World'}; } 

您是否要自动部署此最简单的功能? 如果是这样,请继续阅读本说明。 我将展示如何使用openwhisk-wskdeploy来自动化简单功能的部署。


所需组件



第一步


创建具有以下内容的清单文件(manifest.yaml):


 packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 outputs: payload: type: string description: a simple greeting message, Hello World. 

第二步


创建用于部署的文件(deployment.yaml):


 application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: 

实际上,交付helloworld不需要此文件,只需第一步中的清单即可。


第三步


创建目录结构。 看起来像这样:


 # ls -1R ~/SampleHelloWorldApp/ deployment.yaml manifest.yaml src/ ./src: hello.js 

第四步


部署HelloWorld函数:


 ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully. 

第五步


检查性能:


 # wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello World" } 

第六步


欢喜!


部署:功能,条件和规则


让我们看一下自动化中需要的其他东西:



将参数传递给函数


修改代码:


 function main(params) { return {payload: 'Hello, ' + params.name + ' from ' + params.place}; } 

第一步


创建清单文件:


 packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! 

您可以在清单文件中指定参数值,并跳过创建文件进行部署:


 packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: Amy place: Paris outputs: payload: type: string description: a simple greeting message, Hello World! 

第二步


创建用于部署的文件:


我们通过添加嵌套在“ helloworld”函数中的“ inputs”部分来设置默认值:


 application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris 

检查目录结构自上次以来是否未更改。


第三步


扩展功能:


 # ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully. 

第四步


检查性能:


 wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello, Amy from Paris" } 

通过使用--param参数传递值来覆盖默认值:


 wsk action invoke --blocking --result helloworld/helloworld --param name Mark --param place Barcelona { "payload": "Hello, Mark from Barcelona" } 

创建条件和约束规则


第一步


在清单中添加两个部分:


 packages: helloworld: actions: helloworld: location: src/helloworld.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: locationUpdate: rules: helloworldOnLocationUpdate: action: helloworld trigger: locationUpdate 

第二步


展开功能,条件和绑定规则:


 ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: locationUpdate bindings: Rules * rule: helloworldOnLocationUpdate - trigger: locationUpdate - action: helloworld Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger locationUpdate ... Done! Deploying rule helloworldOnLocationUpdate ... Done! Deployment completed successfully. 

第三步


检查性能:


  • 我们看一下工作功能:

     wsk activation poll Enter Ctrl-c to exit. Polling for activation logs 
  • 打开另一个终端并输入命令以激活条件响应:

     wsk trigger fire locationUpdate ok: triggered locationUpdate with id 4c3a8b1792d546a68ac58538c3f5d637 
  • 在第一个终端查看结果:

     Activation: helloworld (d545c458f3d34d6fbf5c29173be3d29e) [] Activation: locationUpdate (4c3a8b1792d546a68ac58538c3f5d637) [] Activation: helloworldOnLocationUpdate (c099355c1f1f4d6d8d30f54e8dac2b84) [] 
  • 确定条件响应的ID并检查函数的结果:

     wsk activation get d545c458f3d34d6fbf5c29173be3d29e ok: got activation d545c458f3d34d6fbf5c29173be3d29e { ... "activationId": "d545c458f3d34d6fbf5c29173be3d29e", "start": 1489444142544, "end": 1489444142598, "response": { "status": "success", "statusCode": 0, "success": true, "result": { "payload": "Hello, Amy from Paris" } }, ... } 

公告公告


基于OpenWhisk的无服务器计算,第1部分
基于OpenWhisk的无服务器计算,第2部分
基于OpenWhisk的无服务器计算,第3部分
基于OpenWhisk的无服务器计算,第4部分

Source: https://habr.com/ru/post/zh-CN477336/


All Articles