/spin-http-go-example

Example Spin HTTP Application written in Go

Primary LanguageShell

About

Build status

Example Spin HTTP Application written in Go.

Usage

Install Go, TinyGo and Spin.

Start the application:

spin up --build

Access the HTTP endpoint:

xdg-open http://localhost:3000

Container Image Usage

There are two ways to run a WebAssembly binary in a container:

  1. Ship the spin binary and your .wasm file in a docker container image.
  2. Ship the .wasm file and the spin.toml manifest file in a oci image artifact; this can be done in a single step with spin registry push. Then use a container runtime or orchestrator that supports running wasm containers. For example, containerd and the containerd-shim-spin-v2 containerd shim.

NB The Fermyon Cloud directly uses the .wasm file, so there is no need to use a container. Instead use spin deploy to deploy the application to the Fermyon Cloud.

Kubernetes Usage

See https://developer.fermyon.com/spin/v2/kubernetes.

containerd Usage

Install containerd and the containerd-shim-spin.

containerd crictl Usage

Use crictl:

# see https://kubernetes.io/docs/concepts/architecture/cri/
# see https://kubernetes.io/docs/tasks/debug/debug-cluster/crictl/
# see https://kubernetes.io/docs/reference/tools/map-crictl-dockercli/
# see https://github.com/kubernetes-sigs/cri-tools/blob/master/docs/crictl.md
# see https://github.com/kubernetes-sigs/cri-tools/blob/master/cmd/crictl/sandbox.go
# see https://github.com/kubernetes-sigs/cri-tools/blob/master/cmd/crictl/container.go
# see https://github.com/kubernetes/cri-api/blob/kubernetes-1.27.10/pkg/apis/runtime/v1/api.proto
crictl pull \
  ghcr.io/rgl/spin-http-go-example:0.1.0
crictl images list
crictl info | jq .config.containerd.runtimes
install -d -m 700 /var/log/cri
cat >cri-spin-http-go-example.pod.yml <<'EOF'
metadata:
  uid: cri-spin-http-go-example
  name: cri-spin-http-go-example
  namespace: default
log_directory: /var/log/cri/cri-spin-http-go-example
EOF
cat >cri-spin-http-go-example.web.ctr.yml <<'EOF'
metadata:
  name: web
image:
  image: ghcr.io/rgl/spin-http-go-example:0.1.0
command:
  - /
log_path: web.log
EOF
pod_id="$(crictl runp \
  --runtime spin \
  cri-spin-http-go-example.pod.yml)"
web_ctr_id="$(crictl create \
  $pod_id \
  cri-spin-http-go-example.web.ctr.yml \
  cri-spin-http-go-example.pod.yml)"
crictl start $web_ctr_id
web_ctr_ip="$(crictl inspectp $pod_id | jq -r .status.network.ip)"
wget -qO- "http://$web_ctr_ip"
crictl ps -a                    # list containers.
crictl inspect $web_ctr_id | jq # inspect container.
crictl logs $web_ctr_id         # dump container logs.
crictl pods                     # list pods.
crictl inspectp $pod_id | jq    # inspect pod.
crictl stopp $pod_id            # stop pod.
crictl rmp $pod_id              # remove pod.
rm -rf /var/log/cri/cri-spin-http-go-example

containerd ctr Usage

NB This is not yet working. See deislabs/containerd-wasm-shims#202.

Use ctr:

ctr image pull \
  ghcr.io/rgl/spin-http-go-example:0.1.0
ctr images list
ctr run \
  --detach \
  --runtime io.containerd.spin.v2 \
  --net-host \
  ghcr.io/rgl/spin-http-go-example:0.1.0 \
  ctr-spin-http-go-example
ctr sandboxes list # aka pods.
ctr containers list
ctr container rm ctr-spin-http-go-example

References