使用容器镜像交付是云原生时代的重要特征,这不只体如今企业内部应用中,也体如今公有云的 serverless 函数中。腾讯云 serverless 函很多天前上线了容器镜像交付的功能,相较于传统 serverless,容器镜像交付有着明显的好处:它解决了环境依赖的问题,使 serverless 更加通用,拓展了 serverless 的能力。前端
具体来讲,容器镜像使得 serverless 函数不但能支持预设的 nodejs、Python 这些 runtimes,并且也能支持机器码二进制程序(Native Client or NaCl),或者 WebAssembly 字节码程序。NaCl 与 WebAssembly 对执行环境要求复杂,可是执行性能与效率会比 Nodejs 与 Python 高不少。node
放眼将来,咱们认为 WebAssembly runtime 是一个能够在不少场景替代 Docker 的轻量级软件容器。WebAssembly 在速度(尤为是冷启动)与资源占用上大大优于 Docker(上百倍的改进)。可是,罗马不是一天建成的。今天腾讯云支持的容器镜像只有 Docker 镜像一种,咱们暂时必须在 Docker 里面运行 WebAssembly。git
那么,现阶段在 Docker 容器内部运行 WebAssembly 函数相比于 NaCl 有什么好处呢?WebAssembly 拥有把 Docker 的安全隔离、跨平台可移植、可编排等优势,从应用的颗粒度下沉到函数的颗粒度,为开发者带来了很大便利。具体来讲,github
- WebAssembly 函数能够实现接近机器语言的性能。
- 与 NaCl 函数不一样,WebAssembly 函数是跨平台的。容许开发者在本身的电脑上测试函数,而后部署在任何服务器,硬件架构,公共云,或者容器上。
- WebAssembly 函数能够简单地被打包,部署,更新升级,编排。远比 NaCl 动态库容易管理。
- WebAssembly 比 NaCl 函数更安全,由于 WebAssembly 提供了一个安全隔离的沙箱。
另外,CNCF 的 WebAssembly 项目 WasmEdge 为 Rust 开发者提供了最友好的 API 来高效安全地执行 Tensorflow 模型。这比任何其余基于 C、C++、Rust 的 NaCl tensorflow API 都简单好用不少。值得你来学习!web
快速开发的模板
在这里,咱们提供了一个模板,能够快速上手进行开发。docker
模板连接:https://github.com/second-state/tencent-scf-wasm-runtimeapi
在这个 repo 里,咱们有两个完整的 WebAssembly 函数应用:一个是 main branch 里的图像处理函数,实现的功能是将图片变灰;另一个在 tensorflow branch 里的 AI 推理函数,实现的功能是用 AI 推理识别图片中最主要的物体。数组
这两个函数都是使用 Rust 编写的,所以咱们须要安装 Rust 编译器。安装好以后,按照下面的方法安装 wasm32-wasi 编译器目标,以生成 WebAssembly 字节码。安全
$ rustup target add wasm32-wasi
这两个应用模板的前端使用 next.js 编写,在这两个示例中,咱们将其前端 web UI 部署到了 GitHub Pages 上,可是你能够将其部署到任何一个支持静态网站的托管平台,好比腾讯的网站托管服务。具体如何部署,能够参考模板 github repo 的 README — 前端的 web UI。服务器
连接:https://github.com/second-state/tencent-scf-wasm-runtime#%E5%89%8D%E7%AB%AF%E7%9A%84-web-ui
示例1:图像处理
在这个模板中,咱们有一个已经编译好的 WebAssembly 函数放在 ·api/grayscale.wasm
文件里。这个函数的 Rust 源代码在 api/functions/image-grayscale
里面。
Rust 函数读入一个图片,而后输出这个图片的黑白版。从 STDIN
读入上传的图片,而后把黑白图片从 STDOUT
输出。其输入与输出都是二进制数组。
use std::io::{self, Read, Write}; use image::{ImageOutputFormat, ImageFormat}; fn main() { let mut buf = Vec::new(); io::stdin().read_to_end(&mut buf).unwrap(); let image_format_detected: ImageFormat = image::guess_format(&buf).unwrap(); let img = image::load_from_memory(&buf).unwrap(); let filtered = img.grayscale(); let mut buf = vec![]; match image_format_detected { ImageFormat::Gif => { filtered.write_to(&mut buf, ImageOutputFormat::Gif).unwrap(); }, _ => { filtered.write_to(&mut buf, ImageOutputFormat::Png).unwrap(); }, }; io::stdout().write_all(&buf).unwrap(); io::stdout().flush().unwrap(); }
你能够根据你的业务需求,更改 Rust 代码,好比将图片水平翻转、裁剪图片大小。改动 Rust 代码以后,须要用 cargo
命令就能够编译出新的 WebAssembly 文件 api/grayscale.wasm
。
$ cd api/functions/image-grayscale/ $ cargo build --release --target wasm32-wasi $ cp target/wasm32-wasi/release/grayscale.wasm ../../
api/server.js
这个脚本从 Web 函数的网关得到 HTTP request 的数据,传给 grayscale.wasm
函数执行,再把执行结果返回给 HTTP response。
说明:WasmEdge 支持 AOT 编译,大幅提高了应用的性能,所以在这里,咱们使用 WasmEdge AOT 编译器在 Docker 环境内生成的 grayscale.so 文件,
app.post('/func', (req, res) => { const wasmedge = spawn(path.join(__dirname, 'wasmedge'), [path.join(__dirname, 'grayscale.so')]); let d = []; wasmedge.stdout.on('data', (data) => { d.push(data);
为了将这个函数部署为 serverless 函数,咱们须要将 grayscale.wasm
与 server.js
以及 WasmEdge 的执行环境一块儿封装在一个容器镜像里面。
$ cd api $ docker build -t hkccr.ccs.tencentyun.com/secondstate/grayscale:0.1 ./ ... ... Successfully tagged hkccr.ccs.tencentyun.com/secondstate/grayscale:0.1
这里的
hkccr.ccs.tencentyun.com/secondstate/grayscale
是在腾讯云的容器服务上创建一个容器镜像后得到的,具体能够参考咱们的 README — 准备工做。
而后将这个容器镜像发布在腾讯云容器镜像仓库里。
$ docker push hkccr.ccs.tencentyun.com/secondstate/grayscale:0.1 The push refers to repository [hkccr.ccs.tencentyun.com/secondstate/grayscale] ... ... 0.1: digest: sha256:... size: 3246
到这一步,咱们的 WebAssembly 与 Rust 函数就构建好了,接下来就是将其部署到腾讯云 serverless 上。具体的步骤与截屏能够参考这个模板项目的 README。
示例2:图像识别
在 TensorFlow 分支,咱们有一个更复杂的 serverless 函数。它展现了如何用 Tensorflow 进行 AI 推理。Rust 与 WebAssembly 让咱们能够在几毫秒以内用深度学习识别输入图片上的物体。
下面这个Rust 程序从 STDIN
读取图像数据,而后将文本输出输出到 STDOUT
。 它用 WasmEdge Tensorflow API 来运行 AI 推理。
pub fn main() { // Step 1: Load the TFLite model let model_data: &[u8] = include_bytes!("models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_quant.tflite"); let labels = include_str!("models/mobilenet_v1_1.0_224/labels_mobilenet_quant_v1_224.txt"); // Step 2: Read image from STDIN let mut buf = Vec::new(); io::stdin().read_to_end(&mut buf).unwrap(); // Step 3: Resize the input image for the tensorflow model let flat_img = wasmedge_tensorflow_interface::load_jpg_image_to_rgb8(&buf, 224, 224); // Step 4: AI inference let mut session = wasmedge_tensorflow_interface::Session::new(&model_data, wasmedge_tensorflow_interface::ModelType::TensorFlowLite); session.add_input("input", &flat_img, &[1, 224, 224, 3]) .run(); let res_vec: Vec<u8> = session.get_output("MobilenetV1/Predictions/Reshape_1"); // Step 5: Find the food label that responds to the highest probability in res_vec // ... ... let mut label_lines = labels.lines(); for _i in 0..max_index { label_lines.next(); } // Step 6: Generate the output text let class_name = label_lines.next().unwrap().to_string(); if max_value > 50 { println!("It {} a <a href='https://www.google.com/search?q={}'>{}</a> in the picture", confidence.to_string(), class_name, class_name); } else { println!("It does not appears to be any food item in the picture."); } }
上文提到过模板项目中的 api/server.js
将 HTTP request 与 response 与 WasmEdge 联接起来。若是咱们更改了 Rust 函数的输入与输出,可能也须要改动 api/server.js
里面的胶水代码。
app.post('/func', (req, res) => { const wasmedge = spawn( path.join(__dirname, 'wasmedge-tensorflow-lite'), [path.join(__dirname, 'classify.so')], {env: {'LD_LIBRARY_PATH': __dirname}} );
修改了 Rust 函数
与 api/server.js
以后,按照上文所说的方法,从新建立与部署 Docker 镜像,新的函数就能够用了!
完整的部署请参考模板项目的 README,期待你的 Rust 函数!
连接:https://github.com/second-state/tencent-scf-wasm-runtime/blob/main/README.md
下一步
目前,咱们仍然把 WasmEdge 放在腾讯云 serverless 的 Docker 镜像里面运行。这虽然已经带来了巨大的好处,可是尚未彻底发挥 WebAssembly 的优点。WasmEdge 能够做为一个 Docker 的替代,直接运行函数,而不是在 Docker 之中运行函数。从而大幅提升 serverless 函数的性能,下降 infrastructure 的成本!目前已经可以从 Docker 与 k8s 的管理编排工具里面启动与管理 WasmEdge 应用程序。
另外,虽然 Rust 很是适合写高性能的 serverless 函数,但 Rust 确实有着比较陡峭的学习路线,这对于 serverless 开发者来讲体验并很差。咱们将推出一种“低代码”的解决方案,为具体应用设计“低代码”语言(DSL or Domain Specific Language)。WebAssembly 对语言编译器与解释器的普遍支持,使其特别适合运行各行各业的 DSL。敬请期待。