Overview

Getting Started

Install knative and WASMEdge on IBM Cloud

  1. Create a cluster called knawd-k8s on IBM Cloud. For full instructions see https://cloud.ibm.com/docs/containers?topic=containers-getting-started

  2. Install the IBM Cloud Cli. The instructions please ensure you install the https://cloud.ibm.com/docs/cli?topic=cli-getting-started

  3. Install the kubernetes service plugin https://cloud.ibm.com/docs/cli?topic=cli-install-devtools-manually#idt-install-kube

  4. Once the cluster is complete login with the ibmcloud cli

    ibm cloud login
    
  5. Now configure the kubectl connection

    ibmcloud ks cluster config -c knawd-k8s
    
  6. We are going to follow a summary of the instructions for the knative website.

  7. Install the custom resource definitions which are the objects such as service that you will use to deploy an app.

    kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.3/serving-crds.yaml
    
  8. Then install the knative serving core

    kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.3/serving-core.yaml
    
  9. Now we install the networking layer

    kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.8.1/kourier.yaml
    
  10. And Patch the knative space

    kubectl patch configmap/config-network \
     --namespace knative-serving \
     --type merge \
     --patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'
    
  11. Finally configure dns

    kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.3/serving-default-domain.yaml
    
  12. Patch the knative service to allow for the crun runtime to be specified as an attribute in the service.

    kubectl patch configmap/config-features -n knative-serving --type merge --patch '{"data":{"kubernetes.podspec-runtimeclassname":"enabled"}}'
    
  13. Now run install the deployment project

    git clone https://github.com/uirlis/wasi-crun-deployer
    cd wasi-crun-deployer/charts/wasi-crun-deployer
    helm install wasi-crun-deployer . --create-namespace --namespace knawd --set job.autoRestart=true
    
  14. Finally run the tempate helloworld service.

    git clone https://github.com/uirlis/helloworld-rust-wasi
    cd helloworld-rust-wasi
    kubectl apply -f service.yaml
    
  15. Get the external IP of the kourier network

    kubectl get svc kourier -n kourier-system
    
    NAME      TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                      AGE
    kourier   LoadBalancer   172.21.191.127   159.122.80.147   80:32294/TCP,443:31406/TCP   44m
    
    
  16. Visit the endpoint generated buy sslip.io - http://helloworld-rust-wasi.default..sslip.io/

    curl http://helloworld-rust-wasi.default.159.122.80.147.sslip.io/
    Hello Rust Sample v1
    

Install knative and WASMEdge on OpenShift

WIP - This is a first draft and NOT had a clean cluster validation.

  1. Follow the excellent OpenShift documentation to install OpenShift Serverless which is based on knative. Ensure that you also install the knative-serving service into the knative-serving project

    https://docs.openshift.com/container-platform/4.11/serverless/install/install-serverless-operator.html

  2. Patch the knative service to allow for the crun runtime to be specified as an attribute in the service.

    oc patch configmap/config-features -n knative-serving --type merge --patch '{"data":{"kubernetes.podspec-runtimeclassname":"enabled"}}'
    
  3. As the deployer configures the node it needs to run with elevated privaliges. We should create a dedicated project for it and apply labels to minimise the warnings returned.

    oc new-project knawd
    oc label --overwrite ns knawd   pod-security.kubernetes.io/enforce=privileged   pod-security.kubernetes.io/enforce-version=v1.24
    oc label --overwrite ns knawd   pod-security.kubernetes.io/audit=privileged   pod-security.kubernetes.io/enforce-version=v1.24
    oc label --overwrite ns knawd   pod-security.kubernetes.io/warn=privileged   pod-security.kubernetes.io/enforce-version=v1.24
    

    This should be part of the helm package in later releases.

  4. Now run install the project into the knawd on the cluster

    git clone https://github.com/uirlis/wasi-crun-deployer
    cd wasi-crun-deployer/charts/wasi-crun-deployer
    helm install wasi-crun-deployer . --namespace knawd -f rhel8-values.yaml --set job.autoRestart=true
    
  5. Finally run the sample helloworld service to validate the installation.

    git clone https://github.com/uirlis/helloworld-rust-wasi
    cd helloworld-rust-wasi
    kubectl apply -f service.yaml
    

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.

  1. Install cargo generate This is a templating engine that can parse git repos to create projects.

    cargo install generate
    
  2. install WASMEdge

  3. Install cargo wasi with cargo install cargo-wasi

  4. 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.

  5. 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:

    1. 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

    2. "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.

    3. "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 or quay.io

    4. "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

  6. Once the project is created you can explore the README.md to understand the different options available to you.

  7. 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
    
  8. 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 or docker. i.e.

    docker build -t {{container_reg}}/{{container_org}}/{{project-name}} .
    

    or

    podman build -t {{container_reg}}/{{container_org}}/{{project-name}} .
    
  9. 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}}
    
  10. 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