Before you begin, you need to have the following installed:
- Docker version 4.13.1 (90346) or later with containerd enabled
- k3d
- kubectl
- SpiderLightning and
slight - Rust
Start a k3d cluster with the WASM shims already installed:
k3d cluster create wasm-cluster --image ghcr.io/deislabs/containerd-wasm-shims/examples/k3d:v0.3.3 -p "8081:80@loadbalancer" --agents 2 --registry-create mycluster-registry:12345Apply RuntimeClass for SpiderLightning applications to use the SpiderLightning WASM shim:
kubectl apply -f https://raw.githubusercontent.com/deislabs/containerd-wasm-shims/main/deployments/workloads/runtime.yamlDeploy a pre-built sample SpiderLightning application:
kubectl apply -f https://raw.githubusercontent.com/deislabs/containerd-wasm-shims/main/deployments/workloads/workload.yaml
echo "waiting 5 seconds for workload to be ready"
sleep 5
curl -v http://0.0.0.0:8081/slight/helloConfirm you see a response from the sample application. For example:
$ curl -v http://0.0.0.0:8081/slight/hello
* Trying 0.0.0.0:8081...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8081 (#0)
> GET /hello HTTP/1.1
> Host: 0.0.0.0:8081
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept: */*
< Accept-Encoding: gzip
< Content-Length: 5
< Date: Tue, 11 Oct 2022 19:12:17 GMT
< Host: 0.0.0.0:8081
< User-Agent: curl/7.68.0
< X-Forwarded-For: 10.42.1.1
< X-Forwarded-Host: 0.0.0.0:8081
< X-Forwarded-Port: 8081
< X-Forwarded-Proto: http
< X-Forwarded-Server: traefik-7cd4fcff68-xr2gh
< X-Real-Ip: 10.42.1.1
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host 0.0.0.0 left intact
hello
Delete the pre-built sample SpiderLightning application:
kubectl delete -f https://raw.githubusercontent.com/deislabs/containerd-wasm-shims/main/deployments/workloads/workload.yamlClone the deislabs/containerd-wasm-shims project and navigate to the containerd-wasm-shims/images/slight directory. This directory contains the source for the application you deployed in the previous section.
git clone https://github.com/deislabs/containerd-wasm-shims.git
cd containerd-wasm-shims/images/slightUse rustup to install the wasm32-wasi target and cargo to build the application. For example:
rustup target add wasm32-wasi
cargo build --target wasm32-wasiUse slight run to run the application on your development computer. For example:
slight -c slightfile.toml run target/wasm32-wasi/debug/http_server_lib.wasmThe application is running at http://0.0.0.0:3000/.
Access the /hello route. For example, use curl in a new terminal window:
$$ curl -v http://0.0.0.0:3000/hello
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /hello HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.83.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< host: localhost:3000
< user-agent: curl/7.83.1
< accept: */*
< access-control-allow-origin: *
< access-control-allow-methods: *
< access-control-allow-headers: *
< access-control-expose-headers: *
< content-length: 12
< date: Fri, 07 Apr 2023 22:58:18 GMT
<
hello world!* Connection #0 to host localhost left intactReturn to the terminal window running slight run and stop the application.
Use docker to build the container image and push it to the k3d registry:
docker buildx build --platform=wasi/wasm -t localhost:12345/qs-wasm-slight .
docker push localhost:12345/qs-wasm-slight:latestCreate a qs.yaml file with the following:
apiVersion: apps/v1
kind: Deployment
metadata:
name: wasm-slight
spec:
replicas: 1
selector:
matchLabels:
app: wasm-slight
template:
metadata:
labels:
app: wasm-slight
spec:
runtimeClassName: wasmtime-slight
containers:
- name: testwasm
image: mycluster-registry:12345/qs-wasm-slight:latest
command: ["/"]
---
apiVersion: v1
kind: Service
metadata:
name: wasm-slight
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: wasm-slight
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: wasm-slight
annotations:
ingress.kubernetes.io/ssl-redirect: "false"
spec:
ingressClassName: traefik
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wasm-slight
port:
number: 80Deploy the application and confirm it is running:
kubectl apply -f qs.yaml
echo "waiting 5 seconds for workload to be ready"
sleep 5
curl -v http://0.0.0.0:8081/helloConfirm you see a response from the sample application. For example:
$ curl -v http://0.0.0.0:8081/hello
* Trying 0.0.0.0:8081...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8081 (#0)
> GET /hello HTTP/1.1
> Host: 0.0.0.0:8081
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept: */*
< Accept-Encoding: gzip
< Content-Length: 5
< Date: Tue, 11 Oct 2022 20:29:12 GMT
< Host: 0.0.0.0:8081
< User-Agent: curl/7.68.0
< X-Forwarded-For: 10.42.0.0
< X-Forwarded-Host: 0.0.0.0:8081
< X-Forwarded-Port: 8081
< X-Forwarded-Proto: http
< X-Forwarded-Server: traefik-7cd4fcff68-xr2gh
< X-Real-Ip: 10.42.0.0
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host 0.0.0.0 left intact
hello
Remove the sample application:
kubectl delete -f qs.yamlDelete the cluster:
k3d cluster delete wasm-clusterTry running Wasm applications on Docker Desktop or on Kubernetes, such as AKS.
If you prefer tutorials in video format, check out Deploying Your Wasm WASI App to Kubernetes in 10 minutes on Youtube.