HTTP 服务端实例

让我们构建一个基于 WebAssembly 的 HTTP 服务容器镜像。

这个 HTTP 服务应用程序是基于 Rust 开发,并采用了 WasmEdge 网络 socket API。

Kubernetes 可以使用 CRI-O、Docker 和 Containerd 来管理 wasm 应用程序生命周期。

先决条件

这是一个 Rust 示例,它需要你先安装 Rust 和 WasmEdge 以便可以编译运行 http 服务。

下载样例代码

1. mkdir http_server
2. cd http_server
3. wget -q https://raw.githubusercontent.com/second-state/wasmedge_wasi_socket/main/examples/http_server/Cargo.toml
4. mkdir src
5. cd src
6. wget -q https://raw.githubusercontent.com/second-state/wasmedge_wasi_socket/main/examples/http_server/src/main.rs
7. cd ../

构建 WASM 字节码

1. rustup target add wasm32-wasi
2. cargo build --target wasm32-wasi --release

wasm 字节码应用程序现在应该位于 ./target/wasm32-wasi/release/http_server.wasm 目录下。 你现在可以使用 wasmedge 测试运行它,然后将其发布为容器镜像。

在 Wasm 字节码上申请执行权限

1. chmod +x ./target/wasm32-wasi/release/http_server.wasm

使用 wasmedge 运行 http_server 应用程序字节码

当你在使用 wasmedge 来执行字节码并看到如下结果时,你已完成了将字节码打包到容器中的准备工作。

1. $ wasmedge ./target/wasm32-wasi/release/http_server.wasm
2. new connection at 1234

你可以在另一个的终端窗口中测试这个服务端。

1. $ curl -X POST http://127.0.0.1:1234 -d 'name=WasmEdge'
2. echo: name=WasmEdge

创建 Dockerfile

在 target/wasm32-wasi/release 文件夹中创建一个命名为 Dockerfile 的文件,其内容如下:

1. FROM scratch
2. ADD http_server.wasm /
3. CMD ["/http_server.wasm"]

创建一个带注释的容器镜像

请注意,添加自定义注释仍然是 buildah 中的新功能。

crun 容器运行时可以启动上述基于 WebAssembly 的容器镜像。但它需要容器镜像上的 module.wasm.image/variant=compat 注释来表明它是一个没有客人操作系统(安装在虚拟机上的系统)的 WebAssembly 应用程序。你可以在官方 crun repo 中找到详细信息。

要在容器镜像中添加 module.wasm.image/variant=compat 注释,你需要最新的 buildah。 目前,Docker 不支持此功能。请按照 buildah 的安装说明 构建最新的 buildah 二进制文件。

在 Ubuntu 上编译并安装最新的 buildah

在 Ubuntu zesty 和 xenial 上,使用这些命令为 buildah 做准备。

1. sudo apt-get -y install software-properties-common

3. export OS="xUbuntu_20.04"
4. sudo bash -c "echo \"deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /\" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
5. sudo bash -c "curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | apt-key add -"

7. sudo add-apt-repository -y ppa:alexlarsson/flatpak
8. sudo apt-get -y -qq update

10. sudo apt-get -y install bats git libapparmor-dev libdevmapper-dev libglib2.0-dev libgpgme-dev libseccomp-dev libselinux1-dev skopeo-containers go-md2man containers-common
11. sudo apt-get -y install golang-1.16 make

然后,按照下列步骤在 Ubuntu 上编译和安装 buildah。

1. mkdir -p ~/buildah
2. cd ~/buildah
3. export GOPATH=`pwd`
4. git clone https://github.com/containers/buildah ./src/github.com/containers/buildah
5. cd ./src/github.com/containers/buildah
6. PATH=/usr/lib/go-1.16/bin:$PATH make
7. cp bin/buildah /usr/bin/buildah
8. buildah --help

创建和发布具备 buildah 的容器镜像

在 target/wasm32-wasi/release/ 文件夹下,执行下列指令。

1. $ sudo buildah build --annotation "module.wasm.image/variant=compat" -t http_server .

3. #
4. # make sure docker is install and running
5. # systemctl status docker
6. # to make sure regular user can use docker
7. # sudo usermod -aG docker $USER#
8. # newgrp docker

10. # You may need to use docker login to create the `~/.docker/config.json` for auth.
11. #
12. # docker login

14. $ sudo buildah push --authfile ~/.docker/config.json http_server docker://docker.io/avengermojo/http_server:with-wasm-annotation

好了! 现在你可以尝试在 CRI-O 或 Kubernetes 上运行它了!