Develop a WASMEdge Service in Rust
Before you start please ensure that you have run through the getting started section to create an WASMEdge enabled kubernetes environment.
In this section we are going to build a very simple service that highlights the key aspects for running a WASM service on Knative.
We assume you have a running rust environment. If not please goto rustup.
We also assume you have a container management system such as docker or podman installed.
-
Install
cargo generate
This is a templating engine that can parse git repos to create projects.cargo install generate
-
Install
cargo wasi
withcargo install cargo-wasi
-
Set
CARGO_TARGET_WASM32_WASI_RUNNER=wasmedge
in your.profile
or your user profile settings on Windows by clicking: System > Advanced system settings > Advanced > Environment Variables. -
Now generate a project. The template projects are standard Rust projects with place holders that replace the name of the project and and the repo configs.
cargo generate gh:knawd/gen-templates
The prompts help complete some of the assets in the project and map as follows:
-
The name of the project. This is used to give the cargo project a name and to fill out the service and build scripts for this project
-
"Want to create a knative service definition?" This is a yes/no response and if you respond with yes a knative service yaml will be created that enables you to push to your kuberenetes environment from your desktop.
-
"Which container registry would you like to use?" This is a freeform response that fills out the service file and the generated documentation. Usual responses
docker.io
orquay.io
-
"Which container organisation would you like to use?" This is a freeform response that fills out the service file and the generated documentation. Example organisation on is
knawd
available here - https://quay.io/organization/knawd
-
-
Once the project is created you can explore the
README.md
to understand the different options available to you. -
Let run the tests to make sure the projects has generated correctly. The tests are embedded at the bottom of the the
handler.rs
file. This is a convention in rust but they can be created in seperate files.cargo wasi test
-
Now look under the build section in the README.md. You will see a generated command to build the docker image. Run the command for
podman
ordocker
. i.e.docker build -t {{container_reg}}/{{container_org}}/{{project-name}} .
or
podman build -t {{container_reg}}/{{container_org}}/{{project-name}} .
-
Then in the push section of the README take the code snippets to push the application to your registry. i.e.
docker push {{container_reg}}/{{container_org}}/{{project-name}}
or
podman push {{container_reg}}/{{container_org}}/{{project-name}}
-
Finally you can deploy your knative service by applying the service to the kubernetes cluster using the deploy step in the Project README. i.e.
kubectl apply -f service.yaml
or
oc apply -f service.yaml