opensuse妗岄潰绯荤粺 (opensuse绯荤粺)

最近 openSUSE 的发行版 Tumebleweed 添加了对 WasmEdge 的支持。SUSE 工程师 Alexandre Vicenzi 写了一篇文章介绍如何在 openSUSE Tumbleweed 上使用 Podman 和 WasmEdge 运行 Wasm 工作负载

2024年1月19日 | Alexandre Vicenzi | CC-BY-SA-3.0

opensuse缃戠粶椹卞姩,opensuse妗岄潰绯荤粺

以下为正文:

WebAssembly (缩写为 Wasm)[2] 是一种可移植的二进制指令格式。它因作为编译目标的可移植性而受到欢迎,使得同一个 Wasm 文件能在 Web 上的客户端和服务器应用程序部署。 [Image: Easily Implement dolphin-2.2-yi-34b on Your Device with Rust + Wasm (12).png] 我们可以利用 Wasm 的可移植性,通过结合 crun Podman[3] ,来肩并肩运行 Wasm 工作负载和 Linux 容器。crun 支持使用 WasmEdge[4] Wasmtime[5] ,或 Wasmer[6] 运行时来运行 Wasm 工作负载。虽然 Podman 默认使用 runc,但 runccrun 可以互换使用。

WasmEdge[7] 是一个轻量级、高性能且可扩展的 WebAssembly 运行时,适用于云原生和边缘应用。WasmEdge 最近添加到了 openSUSE Tumbleweed[8] 中,这使我们能够在容器中支持 Wasm 工作负载,只要我们在 crun 中启用一个实验性功能。

现在我们有了 openSUSE Tumbleweed 中的 WasmEdge 和 crun 对 Wasm 工作负载的实验性支持,我们可以在 Podman 上运行 WebAssembly 工作负载。

设置环境

我们首先需要在 Podman 的默认 OCI 运行时中安装 crun ,用作 runc。

zypper **in** crun

一旦安装好了 crun,检查你是否有 Wasm 支持。

$ crun -v
crun version 1.9
commit: a538ac4ea1ff319bcfe2bf81cb5c6f687e2dc9d3
rundir: /run/user/1000/crun
spec: 1.0.0
+SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL

在上面的输出中,我们可以看到 crun 支持WasmEdge (+WASM:wasmedge).

准备我们的应用程序

我们将创建一个简单的 “Hello” Rust 应用程序,然后编译为 WebAssembly。

首先,安装 Rust 和 WasmEdge:

zypper **in** rust wasmedge

然后创建一个“Hello” Rust 应用程序:

$ cargo new hello --bin
$ cd hello

改变 src/main.rs 中的消息为 Hello WebAssembly! 或者其他你想要的消息。

现在让我们编译我们的应用程序,但目标将是 Wasm。

cargo build --target wasm32-wasi

我们现在可以执行刚刚编译的二进制文件并检查它是否按预期工作。

$ wasmedge run target/wasm32-wasi/debug/hello.wasm
Hello WebAssembly!

你已经成功构建了 Wasm 应用程序。

创建 Wasm 容器

现在,我们有了一个 WebAssembly 二进制码,让我们将其添加到容器中。

创建一个名为 Containerfile 的文件并向其中添加以下内容:

**FROM** scratch
**COPY** target/wasm32-wasi/debug/hello.wasm /
**CMD** ["/hello.wasm"]

让我们使用 Buildah 构建 Wasm 容器。

$ buildah build --platform**=**wasi/wasm -t hello-wasm .

现在你应该有一个 Wasm 容器了。

运行 Wasm 工作负载

用 Podman 运行 WebAssembly 容器:

$ podman run --rm hello-wasm
Hello WebAssembly!

太棒了,我们有了一个可用的 Wasm 容器。

结论

WebAssembly 是一个相当新的话题,但它已经获得了很多关注,因为可以重复利用你已经了解或使用的大部分工具,并且可以轻松移植应用程序。

运行原生 Wasm 容器是这种格式的可移植性的另一个例子。

参考资料

[1]

https://news.openSUSE.org/2024/01/19/podman-wasm-support/: https://news.opensuse.org/2024/01/19/podman-wasm-support/

[2]

WebAssembly (缩写为 Wasm): https://webassembly.org/

[3]

Podman: https://podman.io/

[4]

WasmEdge: https://wasmedge.org/

[5]

Wasmtime: https://wasmtime.dev/

[6]

Wasmer: https://wasmer.io/

[7]

WasmEdge: https://wasmedge.org/

[8]

openSUSE Tumbleweed: https://get.opensuse.org/