iTranslated by AI
Trying Spin and wasmCloud as Wasm Platforms
With wasi-http, WebAssembly (Wasm) can now set up a web server standalone without relying on a host language.
Among the recently released options, Spin and wasmCloud are Wasm hosting services. I will try them out while comparing the two.
spin
A Wasm serverless service developed by Fermyon.
There are patterns for hosting on Spin Cloud, provided by Spin itself, and hosting on Kubernetes (k8s) using SpinKube.
install
$ curl -fsSL https://developer.fermyon.com/downloads/install.sh | bash
$ cp spin ~/bin
$ spin new
$ cd spin-rust
$ spin build
$ spin up
# open http://localhost:3000/
Viewing the generated code
use spin_sdk::http::{IntoResponse, Request, Response};
use spin_sdk::http_component;
/// A simple Spin HTTP component.
#[http_component]
fn handle_spin_rust(req: Request) -> anyhow::Result<impl IntoResponse> {
println!("Handling request to {:?}", req.header("spin-full-url"));
Ok(Response::builder()
.status(200)
.header("content-type", "text/plain")
.body("Hello, Fermyon")
.build())
}
Deploy
Let's try deploying to Spin Cloud.
Running $ spin login will prompt you to log in, so complete the process by linking your GitHub account and entering the one-time code.
Running $ spin cloud deploy will display the deployment status at https://cloud.fermyon.com/.
This time, it was deployed to https://spin-rust-tjttf312.fermyon.app/.
I changed just one line and ran spin build && spin cloud deploy.
It was reflected in 33 seconds. I feel like it's not that fast for Wasm. Let's check the build size.
$ ls -al target/wasm32-wasi/release/spin_rust.wasm
.rwxr-xr-x 1.9M kotaro.chikuba 10 Aug 17:32 target/wasm32-wasi/release/spin_rust.wasm
I wonder if it's not optimized. Let's look inside with twiggy.
# cargo install twiggy
$ twiggy top target/wasm32-wasi/release/spin_rust.wasm -n 10
Shallow Bytes │ Shallow % │ Item
───────────────┼───────────┼──────────────────────────────────────────────────
699497 ┊ 36.02% ┊ custom section '.debug_str'
447542 ┊ 23.05% ┊ custom section '.debug_info'
276454 ┊ 14.24% ┊ custom section '.debug_line'
191232 ┊ 9.85% ┊ custom section '.debug_ranges'
52979 ┊ 2.73% ┊ custom section 'component-type:platform'
46720 ┊ 2.41% ┊ "function names" subsection
36418 ┊ 1.88% ┊ data[0]
14009 ┊ 0.72% ┊ custom section 'component-type:imports'
7913 ┊ 0.41% ┊ custom section 'component-type:wasi-http-trigger'
6538 ┊ 0.34% ┊ dlmalloc
162651 ┊ 8.38% ┊ ... and 668 more.
1941953 ┊ 100.00% ┊ Σ [678 Total Rows]
Since it's mainly debug information, I should just do a release build.
Add to Cargo.toml
[profile.release]
lto = true
opt-level = "z"
This makes it 798k.
Remove debug information with wasm-snip.
# cargo install wasm-snip
$ wasm-snip target/wasm32-wasi/release/spin_rust.wasm -o target/wasm32-wasi/release/spin_rust.wasm
277kb
Let's check the content with twiggy in this state.
$ twiggy top target/wasm32-wasi/release/spin_rust.wasm -n 10
Shallow Bytes │ Shallow % │ Item
───────────────┼───────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
52979 ┊ 19.15% ┊ custom section 'component-type:platform'
39908 ┊ 14.43% ┊ "function names" subsection
33620 ┊ 12.15% ┊ data[0]
14009 ┊ 5.06% ┊ custom section 'component-type:imports'
7913 ┊ 2.86% ┊ custom section 'component-type:wasi-http-trigger'
6670 ┊ 2.41% ┊ anyhow::fmt::<impl anyhow::error::ErrorImpl>::debug::he4ba69e13e10de28
6207 ┊ 2.24% ┊ dlmalloc
4577 ┊ 1.65% ┊ wasi:http/incoming-handler@0.2.0#handle
3447 ┊ 1.25% ┊ <&T as core::fmt::Display>::fmt::h22c4c1857bc12b26
3359 ┊ 1.21% ┊ <spin_sdk::http::Request as spin_sdk::http::conversions::TryFromIncomingRequest>::try_from_incoming_request::{{closure}}::h3e1fb0a378bb429d
103944 ┊ 37.57% ┊ ... and 553 more.
276633 ┊ 100.00% ┊ Σ [563 Total Rows]
Since these are related to the platform and WASI interfaces, this is about as small as I can roughly get it.
Let's see if deployment becomes faster in this state.
$ spin cloud deploy
Uploading spin-rust version 0.1.0 to Fermyon Cloud...
Deploying...
Waiting for application to become ready............ ready
View application: https://spin-rust-tjttf312.fermyon.app/
Manage application: https://cloud.fermyon.com/app/spin-rust
33 seconds. This part doesn't seem to change. Probably the scaling speed would change, but I don't want to benchmark on the free tier.
I'll probably have to balance debugging ease and decide whether to strip this information.
spin on k8s
Setting up Kubernetes (k8s) is a bit tedious, so I'll limit this to an introduction, but there is a way to host Spin on k8s and deploy Spin applications to it.
Looking at the Quickstart, it seems like you deploy Spin Cloud itself to k8s and then deploy Wasm to that.
$ k3d cluster create wasm-cluster \
--image ghcr.io/spinkube/containerd-shim-spin/k3d:v0.15.1 \
--port "8081:80@loadbalancer" \
--agents 2
# Package and Distribute the hello-spin app
$ spin registry push --build ttl.sh/hello-spin:24h
$ spin kube deploy --from ttl.sh/hello-spin:24h
spinapp.core.spinoperator.dev/hello-spin created
I personally seek lightness and simplicity in Wasm, so I wouldn't necessarily choose to use k8s, but conversely, it's useful if you want to experimentally try out the Wasm ecosystem in an environment that already uses k8s. Even if you need to set up a VPC for security requirements, it seems manageable through k8s settings. Having more points of entry is better.
The Starter plan is free. 100,000 requests/month, 1GB SQLite.
The Growth plan is $19.38/month and can handle up to 1,000,000 requests. Egress is 50GB/month. Anything beyond that requires consultation. It includes a 50GB SQLite database.
wasmCloud
This is also a CNCF project and can be deployed to Docker, k8s, and the edge.
install
Install the CLI tool called wash. It sounds like laundry.
# mac
$ brew install wasmcloud/wasmcloud/wash
$ wash up
🛁 wash up completed successfully, already running
🕸 NATS is running in the background at http://127.0.0.1:4222
📜 Logs for the host are being written to /Users/kotaro.chikuba/.wash/downloads/wasmcloud.log
NATS is this:
NATS is a simple, secure and high performance open source data layer for cloud native applications, IoT messaging, and microservices architectures.
We feel that it should be the backbone of your communication between services. It doesn't matter what language, protocol, or platform you are using; NATS is the best way to connect your services.
I wonder if it's like a simplified version of k8s.
The image is like starting up a platform and then deploying to it.
wasmCloud Application Deployment Manager (wadm) manages declarative application deployments, reconciling the current state of an application with its desired state.
In a declarative deployment pattern, a developer defines an application's components, configuration, and scaling properties using a static configuration file that can be versioned, shared, edited, and otherwise used as a source of truth. In wasmCloud, these application manifests comply with the Open Application Model (OAM) and can be written in YAML or JSON. Once a deployment is declared, wadm issues low-level commands to bring that declaration to life.
It looks like nats-server + wadm is similar to k8s + deployment manifests.
$ wash new component hello --template-name hello-world-rust
$ cd hello
$ wash build
$ wash app deploy wadm.yaml
# Check the processes
$ wash app list
Name Latest Version Deployed Version Deploy Status Description
rust-hello-world 01J4Y17MGBGT73PS74H9WYA81Z 01J4Y17MGBGT73PS74H9WYA81Z Deployed HTTP hello world demo in Rust, using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)
$ curl localhost:8080
Hello from Rust
It worked.
Let's see what wadm.yaml looks like.
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: rust-hello-world
annotations:
description: 'HTTP hello world demo in Rust, using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)'
wasmcloud.dev/authors: wasmCloud team
wasmcloud.dev/source-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/rust/components/http-hello-world/wadm.yaml
wasmcloud.dev/readme-md-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/rust/components/http-hello-world/README.md
wasmcloud.dev/homepage: https://github.com/wasmCloud/wasmCloud/tree/main/examples/rust/components/http-hello-world
wasmcloud.dev/categories: |
http,http-server,rust,hello-world,example
spec:
components:
- name: http-component
type: component
properties:
image: file://./build/http_hello_world_s.wasm
# To use the a precompiled version of this component, use the line below instead:
# image: ghcr.io/wasmcloud/components/http-hello-world-rust:0.1.0
traits:
# Govern the spread/scheduling of the component
- type: spreadscaler
properties:
instances: 1
# Add a capability provider that enables HTTP access
- name: httpserver
type: capability
properties:
image: ghcr.io/wasmcloud/http-server:0.22.0
traits:
# Establish a unidirectional link from this http server provider (the "source")
# to the `http-component` component (the "target") so the component can handle incoming HTTP requests,
#
# The source (this provider) is configured such that the HTTP server listens on 127.0.0.1:8080
- type: link
properties:
target: http-component
namespace: wasi
package: http
interfaces: [incoming-handler]
source_config:
- name: default-http
properties:
address: 127.0.0.1:8080
At first glance, it looks like a k8s manifest. Can it be applied to k8s as is?
kubectl apply -f wadm.yaml
application/echo created
It seems to be defined as a k8s custom resource.
Let's take a look at the Rust lib.rs.
wit_bindgen::generate!({
generate_all
});
use exports::wasi::http::incoming_handler::Guest;
use wasi::http::types::*;
struct HttpServer;
impl Guest for HttpServer {
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) {
let response = OutgoingResponse::new(Fields::new());
response.set_status_code(200).unwrap();
let response_body = response.body().unwrap();
ResponseOutparam::set(response_out, Ok(response));
response_body
.write()
.unwrap()
.blocking_write_and_flush(b"Hello from Rust!\n")
.unwrap();
OutgoingBody::finish(response_body, None).expect("failed to finish response body");
}
}
export!(HttpServer);
The difference is that Spin uses macros (derive) while wasmCloud uses impl Guest for HttpServer, but there doesn't seem to be any essential difference.
By the way, I saw the exact same thing in Moonbit's http-wasi with the following code.
response_body
.write()
.unwrap()
.blocking_write_and_flush(b"Hello from Rust!\n")
.unwrap();
It seems that using http-wasi leads to a similar interface. While Spin likely hides it with a dedicated wrapper, wasmCloud leaves the WIT exposed.
It uses wit-deps. Let's look at wit/deps.toml.
http = "https://github.com/WebAssembly/wasi-http/archive/v0.2.0.tar.gz"
keyvalue = "https://github.com/WebAssembly/wasi-keyvalue/archive/main.tar.gz"
logging = "https://github.com/WebAssembly/wasi-logging/archive/main.tar.gz"
Running wit-deps update imports them under wit/deps.
This code is expanded by the following macro.
wit_bindgen::generate!({
generate_all
});
The build size is 1.7M. Since it would be the same even after optimization, I'll skip it.
Just a personal thought: given that the foundation is still unstable, I feel that having WIT exposed is more reassuring than having it wrapped poorly. In that regard, wasmCloud seems to have a better approach than Spin.
I looked into deployment methods, and while wasmCloud itself seems to originate from https://cosmonic.com/, has that not been released to general users yet?
Currently, running wasmCloud requires preparing your own k8s cluster.
Summary
- Spin is wrapped with an SDK and has a strong sense of being its own platform. It comes with a hosting service. There is little information on use cases, and its reliability is unknown.
- wasmCloud is hosted by the CNCF and has a strong sense of standard compliance. You need to prepare your own k8s cluster to run it.
Discussion