最近こんな記事を見つけたので、この機会にcontainerd+wasmを動かしてみた。
概要
以下のページの手順に従ってサンプルを実行を試す。
その後、rustのHello Worldを動かす。
GitHub - containerd/runwasi: Facilitates running Wasm / WASI workloads managed by containerd
確認環境
ubuntu22.04 on raspberrypi4
sdカードに新たにOSを書き直したものを使用した。
環境構築
ツール類のインストール
- ubuntuにツールをインストールする。
$ sudo apt update $ sudo apt install build-essential libclang-dev
- containerdをインストールする。
以下のページを参考に作業した。
containerd/getting-started.md at main · containerd/containerd · GitHub
$ wget https://github.com/containerd/containerd/releases/download/v1.6.18/containerd-1.6.18-linux-arm64.tar.gz $ sudo tar Cxzvf /usr/local containerd-1.6.18-linux-arm64.tar.gz
containerdをsystemdに登録する。
$ wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service $ sudo mkdir -p /usr/local/lib/systemd/system/ $ sudo cp containerd.service /usr/local/lib/systemd/system/containerd.service $ sudo systemctl daemon-reload $ sudo systemctl enable --now containerd
- rustのインストールが必要なので、以下のサイトを参考に作業した。
Installation - The Rust Programming Language
$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
途中で以下のように聞かれるので1で回答。
1) Proceed with installation (default) 2) Customize installation 3) Cancel installation
最後に環境を有効化する。
$ source "$HOME/.cargo/env"
WasmEdgeのインストール
Wasmedgeをランタイムとして使用するために以下でインストールする。
$ curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash $ sudo -E sh -c 'echo "$HOME/.wasmedge/lib" > /etc/ld.so.conf.d/libwasmedge.conf' $ sudo ldconfig
runwasiのクローン
$ git clone https://github.com/containerd/runwasi.git
テスト実行
手順にあるのでテストを実行してみる。
$ cd runwasi $ cargo test -- --nocapture
shimをビルド、インストールする
$ make build $ sudo make install
デモの実行
$ rustup target add wasm32-wasi $ make load $ sudo ctr run --rm --runtime=io.containerd.wasmedge.v1 ghcr.io/containerd/runwasi/wasi-demo-app:latest testwasm /wasi-demo-app.wasm echo 'hello'
さらにアプリに渡すオプションを変更してもう一回実行する。
$ sudo ctr run --rm --runtime=io.containerd.wasmedge.v1 ghcr.io/containerd/runwasi/wasi-demo-app:latest testwasm /wasi-demo-app.wasm daemon
アプリの作成
デモアプリを参考にして、Hello Worldを実行してみる。
デモアプリはRustで書かれている。Rustは書いたことないけど、これを機に勉強していこうかな。
rustプロジェクトの作成
$ cd ~/ $ cargo new hello-containerd-wasm $ cd hello-containerd-wasm
デモアプリの資材をコピー
runwasi/crates/wasi-demo-appからbuild.rsファイルをコピーする。
$ cp /path/to/runwasi/crates/wasi-demo-app/build.rs ./
その後、このファイル内の"wasi-demo-app"という文字列を、"hello-containerd-wasm"に置換する。
Cargo.tomlを編集
wasi-demo-appのCargo.tomlを参考にして以下のように変更する。
[package] name = "hello-containerd-wasm" version = "0.1.0" edition = "2021" [build-dependencies] tar = {version="0.4", optional=true} sha256 = {version="1.1", optional=true} log = {version="0.4", optional=true} env_logger = {version="0.10", optional=true} oci-spec = {version="0.6", optional=true} oci-tar-builder = { git = "https://github.com/containerd/runwasi.git", optional=true } anyhow = {version="1.0", optional=true} [features] default = [] oci-v1-tar = ["default", "tar", "sha256", "log", "env_logger", "oci-spec", "oci-tar-builder", "anyhow"]
アプリをwasmにビルド
$ cargo build --target wasm32-wasi
アプリをwasmedgeで動確
$ wasmedge target/wasm32-wasi/debug/hello-containerd-wasm.wasm
コンテナイメージ作成
$ cargo build --features oci-v1-tar
これで、"target/wasm32-wasi/debug/img.tar"が生成される。
containerdへ取り込み
$ sudo ctr -n default image import --all-platforms target/wasm32-wasi/debug/img.tar
以下のコマンドで、取り込めたことを確認。 "ghcr.io/containerd/runwasi/hello-containerd-wasm:latest"が今回取り込んだイメージ。
$ sudo ctr images ls REF TYPE DIGEST SIZE PLATFORMS LABELS ghcr.io/containerd/runwasi/hello-containerd-wasm:latest application/vnd.oci.image.manifest.v1+json sha256:2d6614491b2a4def9f211a54171f1117e7d623f939d7a4aa26077abbf765128b 2.1 MiB wasi/wasm - ghcr.io/containerd/runwasi/wasi-demo-app:latest application/vnd.oci.image.manifest.v1+json sha256:fdff1828d29ccb564a0332cfb4a9cd7b1f982e1ecc56a1d3de3f26156e8da675 2.5 MiB wasi/wasm -
コンテナ実行
デモアプリと同様に以下のように実行できた。
$ sudo ctr run --rm --runtime=io.containerd.wasmedge.v1 ghcr.io/containerd/runwasi/hello-containerd-wasm:latest mytestwasm /hello-containerd-wasm.wasm Hello, world!
まとめ
containerdでwasmの実行を試した。
wasmコンテナを作成してそれをcontainerdに登録、実行できた。