Skip to content

Latest commit

 

History

History
83 lines (59 loc) · 3.87 KB

03-deploy-spin-to-k8s.md

File metadata and controls

83 lines (59 loc) · 3.87 KB

Deploy your Spin applications to Kubernetes

We can deploy Spin applications on Kubernetes using a Wasm containerd shim.

Pre-requisites

Before you begin, you need to have the following installed:

Start and configure a k3d cluster

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.5.1 -p "8081:80@loadbalancer" --agents 2

Apply RuntimeClass for spin applications to use the Spin containerd Wasm shim:

cat <<EOF | kubectl create -f -
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
  name: wasmtime-spin
handler: spin
EOF

Package your Spin app inside a container

Example dockerfiles and k8s deployment manifests exist in the apps/03 directory.

We will use an experimental Spin k8s plugin to package our Spin application inside a Container. While Spin supports packaging Spin applications as OCI artifacts with spin registry, currently, the containerd-wasm-shim expects the Wasm modules to be inside a container. The shim then pulls the module outside of the container when the application is deployed to the cluster. In the future, the shim may support Spin application OCI artifacts, reducing the steps needed to deploy your Spin application to a cluster.

The plugin requires that all modules are available locally and that files are within subdirectories of the working directory. In particular, we need to get the static fileserver module, move our frontend files within this directory, and update our component manifest with the new resource paths.

Install the plugin, scaffold the Dockerfile, build the container, and push it to your container registry. The following example uses the GitHub Container Registry.

Note: if creating a Rust app, before deploying the app to Kubernetes, modify the spin.toml file to remove the following as the shim does not yet support spin watch: watch = ["src/**/*.rs", "Cargo.toml"]

# Install the plugin
$ spin plugin install -y -u https://raw.githubusercontent.com/chrismatteson/spin-plugin-k8s/main/k8s.json
# Build your app locally
$ spin build
# Scaffold your Dockerfile, passing in the namespace of your registry
$ spin k8s scaffold ghcr.io/my-registry  && spin k8s build
# Import your image into the k3d cluster
$ k3d image import -c wasm-cluster  ghcr.io/my-registry/hello-typescript:0.1.0
# After making sure it is a publicly accessible container or adding a regcred to your `deploy.yaml`
$ spin k8s deploy
# Watch the applications become ready
$ kubectl get pods --watch
# Query the application (modify the endpoint path to match your application name)
$ curl -v http://0.0.0.0:8081/hello-typescript

To learn more about each of the k8s plugin steps, see the documentation on running Spin on Kubernetes.

Cleanup

Bring down your k3d cluster:

k3d cluster delete wasm-cluster

Learning Summary

In this section you learned how to:

  • Use the containerd Wasm shim to package a Spin app within a Docker container
  • Deploy a containerized Spin app to a Kubernetes cluster

Navigation