This sample illustrates how you can use the Spin Command Trigger to one time commands with Spin.
In the context of Kubernetes we can turn one-time commands into Jobs and CronJobs to perform individual tasks once or on a schedule.
The sample consists of two Spin Apps:
- API: A simple HTTP API that interacts with a key-value store
- CRON: A command app which wipes data from the key-value store
The API exposes the following endpoints
GET /value
- Returns the value of a counter from key-value storePOST /value
- Increments the counter in key-value store by1
DELETE /value
- Removes the counter from the key-value storeGET /
- Prints available API endpoints
The Command App leverages Spin's key-value store API to load all keys available in the key-value store and deletes them.
- Local (
spin up
) - SpinKube
- Fermyon Platform for Kubernetes
To use this sample you must have
- Rust installed on your machine
- The
wasm32-wasi
target for Rust installed (rustup target add wasm32-wasi
)
- The
- Spin CLI installed on your machine
- Command Trigger Plugin must be installed
Follow the steps outlined below to run the API and the Command App:
- Start the API using
spin up -f spin.toml --runtime-config-file ./local.rtc.toml
- Increment the counter by sending
POST
requests to/value
:curl -X POST localhost:3000/value
- Check the value of the counter using
curl localhost:3000/value
(you should see a value other than0
) - Run the Command App from a clean terminal instance using
spin up -f spin-cron.toml --runtime-config-file ./local.rtc.toml
- Check the value of the counter using
curl localhost:3000/value
(it should now return0
)
The k8s
folder contains a deploy.sh
script that you can use to deploy the sample to your Kubernetes cluster.
You must have SpinKube installed
The script itself deploys the following artifacts to your cluster:
- Redis will be deployed to the
redis
namespace - The Runtime Configuration File will be stored in the
rtc
secret in thedefault
namespace - The API will be deployed to the
default
namespace - The Command App will be deployed as a CronJob to the
default
namespace and will be executed every 2nd minute