In this guide we create and deploy a simple Spin HTTP app. This guide is derived from
Spinβs spin-wagi-http
example
and shows how to run a Spin application serving routes from two programs written in
different languages (Rust and C++) using both the Spin executor and the Wagi executor
on Unikraft Cloud. To run it, follow these steps:
Install the kraft
CLI tool and a container runtime engine, e.g. Docker.
Clone the examples
repository and cd
into the examples/spin-wagi-http/
directory:
git clone https://github.com/kraftcloud/examples
cd examples/spin-wagi-http/
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, π©πͺ) in this guide:
# Set Unikraft Cloud access token
# Set metro to Frankfurt, DE
When done, invoke the following command to deploy this application on Unikraft Cloud:
kraft cloud deploy -p 443:3000 -M 2048 .
The output shows the instance URL and other details:
[ β ] Deployed successfully!
β ββββββββββ name : spin-wagi-http-is72r
β ββββββββββ uuid : 045c1bda-0f2e-4f8b-98c7-a208bfa7d143
β βββββββββ state : running
β βββββββββββ url : https://damp-bobo-wg43p36e.fra0.kraft.host
β βββββββββ image : spin-wagi-http@sha256:57a5151996d83332af6da521e1cd92271a8c3ac7ae26bc44a7c0dbbc0a30e577
β βββββ boot time : 300.06 ms
β ββββββββ memory : 2048 MiB
β βββββββ service : damp-bobo-wg43p36e
β ββ private fqdn : spin-wagi-http-is72r.internal
β ββββ private ip : 172.16.28.16
β ββββββββββ args : /usr/bin/spin up --from /app/spin.toml --listen 0.0.0.0:3000
In this case, the instance name is spin-wagi-http-is72r
and the URL is https://damp-bobo-wg43p36e.fra0.kraft.host
.
They are different for each run.
Then curl
the hello route:
curl -i https://damp-bobo-wg43p36e.fra0.kraft.host/hello
And curl
the goodbye route:
curl -i https://damp-bobo-wg43p36e.fra0.kraft.host/goodbye
At any point in time, you can list information about the instance:
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME
spin-wagi-http-is72r damp-bobo-wg43p36e.fra0.kraft.host running 1 minute ago spin-wagi-http@sha2... 2.0 GiB /usr/bin/spin up --from /app/spin.tom... 300064us
When done, you can remove the instance:
kraft cloud instance remove spin-wagi-http-is72r
Customize your Application
To customize the application, update the files in the repository, listed below:
wagi-http-cpp
: C++ server handling the hello route
http-rust
: Rust server handling the goodbye route
Kraftfile
: the Unikraft Cloud specification
Dockerfile
: the Docker-specified application filesystem
spin.toml
: The Spin TOML configuration file
cmd : [ " /usr/bin/spin " , " up " , " --from " , " /app/spin.toml " , " --listen " , " 0.0.0.0:3000 " ]
FROM rust:1.75.0-bookworm AS spin
apt-get install -y --no-install-recommends \
wget -q -O spin.tar.gz "https://github.com/fermyon/spin/releases/download/v${SPIN_VERSION}/spin-v${SPIN_VERSION}-linux-amd64.tar.gz" ; \
tar xzvf ./spin.tar.gz; \
mv ./spin /usr/bin/spin; \
rustup target add wasm32-wasi
wget -q -O wasi-sdk.tar.gz "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION}.0-linux.tar.gz" ; \
mkdir -p /opt/wasi-sdk; \
tar xzvf ./wasi-sdk.tar.gz --strip-components 1 -C /opt/wasi-sdk/; \
rm -rf ./wasi-sdk.tar.gz;
COPY ./http-rust /app/http-rust
RUN cargo build --release
COPY ./wagi-http-cpp /app/wagi-http-cpp
WORKDIR /app/wagi-http-cpp
COPY --from=build /app /app
COPY ./spin.toml /app/spin.toml
spin_manifest_version = 2
description = " A hello world application that serves content from a C++ program and a Rust program "
executor = { type = " wagi " } # _start (the default entrypoint) is automatically mapped to main()
executor = { type = " http " }
source = " wagi-http-cpp/main.wasm "
command = " make build -C wagi-http-cpp "
source = " http-rust/target/wasm32-wasi/release/goodbyerust.wasm "
[component.goodbye.build]
command = " cargo build --target wasm32-wasi --release --manifest-path http-rust/Cargo.toml "
Lines in the Kraftfile
have the following roles:
spec: v0.6
: The current Kraftfile
specification version is 0.6
.
runtime: spin:latest
: The Unikraft runtime kernel to use is Spin.
rootfs: ./Dockerfile
: Build the application root filesystem using the Dockerfile
.
cmd: ["/usr/bin/spin", "up", "--from", "/app/spin.toml", "--listen", "0.0.0.0:3000"]
: Use spin
as the command to start the app, with the given parameters.
The following options are available for customizing the application:
If only updating the existing files under the wagi-http-cpp
and http-rust
directories, no other change is required.
If new files are added, these have to be copied in the application filesystem, using the COPY
command in the Dockerfile
.
More extensive changes may require expanding the Dockerfile
with additional Dockerfile
commands .
Learn More
Use the --help
option for detailed information on using Unikraft Cloud:
Or visit the CLI Reference .