Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Valkey trigger application tutorial #136

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating step by step:

  • change Redis to Valkey where possible (some template prompts and manifest configuration can't change unless the SDK is changed or a new SDK is written specifically for Valkey)
  • Install Valkey (instead of Redis via Bitnami) - see the #TODO inline
  • Use secrets instead of password in plain text in config or commands

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Publish-Subscribe With Redis
description: Learn how to create a Spin App that responds to messages on pub-sub Redis channels and runs in Kubernetes
title: Publish-Subscribe With Valkey
description: Learn how to create a Spin App that responds to messages on pub-sub Valkey channels and runs in Kubernetes
categories: [Spin Operator]
tags: [Tutorials]
weight: 100
Expand All @@ -10,7 +10,7 @@ weight: 100

For this tutorial, we will be using:
- [Spin](https://developer.fermyon.com/spin/v2/install) to build and deploy our event-driven WebAssembly application, and
- [Redis](https://redis.io/docs/install/install-redis/) to generate events in our real-time messaging scenario.
- [Valkey](https://valkey.io/docs/) to generate events in our real-time messaging scenario.

## Create a Kubernetes Cluster

Expand Down Expand Up @@ -44,48 +44,48 @@ helm install spin-operator \
oci://ghcr.io/spinkube/charts/spin-operator
```

## Redis
## Valkey

Let's dive in and install Redis because we need information about the Redis instance to configure our Spin App. We will use the following helm commands to get the job done:
Let's dive in and install Valkey because we need information about the Valkey instance to configure our Spin App. We will use the following helm commands to get the job done:

```bash
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install my-redis bitnami/redis
helm install my-Valkey bitnami/Valkey
```

The `helm` installation process from above prints a lot of useful information to the terminal. For example, the endpoints to communicate with Redis (read/write vs read-only):
The `helm` installation process from above prints a lot of useful information to the terminal. For example, the endpoints to communicate with Valkey (read/write vs read-only):

```bash
my-redis-master.default.svc.cluster.local for read/write operations (port 6379)
my-redis-replicas.default.svc.cluster.local for read-only operations (port 6379)
my-Valkey-master.default.svc.cluster.local for read/write operations (port 6379)
my-Valkey-replicas.default.svc.cluster.local for read-only operations (port 6379)
```

In addition, there are pre-written commands that you can copy and paste. For example:

```bash
export REDIS_PASSWORD=$(kubectl get secret --namespace default my-redis -o jsonpath="{.data.redis-password}" | base64 -d)
export Valkey_PASSWORD=$(kubectl get secret --namespace default my-valkey -o jsonpath="{.data.valkey-password}" | base64 -d)
```

Go ahead and run the command above to store the password as an environment variable for the current terminal session. If required, you can print the actual password using:

```bash
echo $REDIS_PASSWORD
echo $VALKEY_PASSWORD
```

## Create a Redis Message Handler Using Rust
## Create a Valkey Message Handler Using Rust

We use Spin's convenient `redis-rust` template to scaffold our Rust-based Redis message handler:
We use Spin's convenient `valkey-rust` template to scaffold our Rust-based Valkey message handler:

```bash
spin new -t redis-rust redis-message-handler
spin new -t redis-rust valkey-message-handler
```

The command above will provide the prompts for you to add the Description, Redis address and Redis channel (We use the `my-redis-master.default.svc.cluster.local` from above to help configure the Redis address, and the channel is arbitrary i.e. `channel-one`):
The command above will provide the prompts for you to add the Description, Valkey address and Valkey channel (We use the `my-valkey-master.default.svc.cluster.local` from above to help configure the Valkey address, and the channel is arbitrary i.e. `channel-one`):

```bash
Description: Redis message handler using Rust
Redis address[redis://localhost:6379]: redis://:<password>@my-redis-master.default.svc.cluster.local:6379
Description: Valkey message handler using Rust
Redis address[valkey://localhost:6379]: valkey://:<password>@my-valkey-master.default.svc.cluster.local:6379
Redis channel: channel-one
```

Expand All @@ -94,7 +94,7 @@ Redis channel: channel-one
We change into our application directory, and can see the layout that Spin has scaffolded for us:

```bash
cd redis-message-handler
cd valkey-message-handler
tree .
```

Expand All @@ -108,16 +108,16 @@ The above `tree .` command, produces the following output:
└── lib.rs
```

If we open the application manifest (`spin.toml` file) we see that Spin has already pre-populated the [Redis trigger configuration](https://developer.fermyon.com/spin/v2/redis-trigger#the-spin-redis-trigger):
If we open the application manifest (`spin.toml` file) we see that Spin has already pre-populated the [Valkey trigger configuration](https://developer.fermyon.com/spin/v2/redis-trigger#the-spin-redis-trigger):

```toml
# --snip --
[application.trigger.redis]
address = "redis://:password@my-redis-master.default.svc.cluster.local:6379"
address = "valkey://:password@my-valkey-master.default.svc.cluster.local:6379"

[[trigger.redis]]
channel = "channel-one"
component = "redis-message-handler"
component = "valkey-message-handler"
# --snip --
```

Expand All @@ -133,7 +133,7 @@ use bytes::Bytes;
use spin_sdk::redis_component;
use std::str::from_utf8;

/// A simple Spin Redis component.
/// A simple Spin Valkey component.
#[redis_component]
fn on_message(message: Bytes) -> Result<()> {
println!("{}", from_utf8(&message)?);
Expand All @@ -155,15 +155,15 @@ spin build
We will now push the application image to a registry. You can use any container registry you prefer (like DockerHub). But for this tutorial, we’ll use a simple one that does not require authentication:

```bash
spin registry push ttl.sh/redis-message-handler:0.1.0
spin registry push ttl.sh/valkey-message-handler:0.1.0
```

> This image will be available for the default time of 24h (because we're using a server tag instead of specifying a duration for the image to live).

To create the Kubernetes deployment manifest we can use the `spin kube scaffold` command:

```bash
spin kube scaffold --from ttl.sh/redis-message-handler:0.1.0
spin kube scaffold --from ttl.sh/valkey-message-handler:0.1.0
```

As we can see, our `SpinApp` is all set and using the `containerd-shim-spin` executor:
Expand All @@ -172,9 +172,9 @@ As we can see, our `SpinApp` is all set and using the `containerd-shim-spin` exe
apiVersion: core.spinoperator.dev/v1alpha1
kind: SpinApp
metadata:
name: redis-message-handler
name: valkey-message-handler
spec:
image: "ttl.sh/redis-message-handler:0.1.0"
image: "ttl.sh/valkey-message-handler:0.1.0"
executor: containerd-shim-spin
replicas: 2
```
Expand All @@ -184,36 +184,37 @@ spec:
We deploy the Spin App to our Kubernetes cluster by piping the deployment manifest to kubectl:

```bash
spin kube scaffold --from ttl.sh/redis-message-handler:0.1.0 | kubectl apply -f -
spin kube scaffold --from ttl.sh/valkey-message-handler:0.1.0 | kubectl apply -f -
```

## Test

To test the application, we will run an additional container for publishing messages to our Redis channel:
To test the application, we will run an additional container for publishing messages to our Valkey channel:

```bash
kubectl run redis-client \
kubectl run valkey-client \
--namespace default \
--restart='Never' \
--env REDIS_PASSWORD=$REDIS_PASSWORD \
--env VALKEY_PASSWORD=$VALKEY_PASSWORD \
# TODO new way of installing valkey
--image docker.io/bitnami/redis:7.2.4-debian-12-r9 \
--command -- sleep infinity
```

Then, we want to jump into the container using `kubectl exec`:

```bash
kubectl exec --tty -i redis-client --namespace default -- bash
kubectl exec --tty -i valkey-client --namespace default -- bash
```

And, access the Redis CLI from inside the cluster:
And, access the Valkey CLI from inside the cluster:

```bash
REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h my-redis-master.default.svc.cluster.local
VALKEYCLI_AUTH="$VALKEY_PASSWORD" valkey-cli -h my-valkey-master.default.svc.cluster.local
```

This provides us with the following prompt at which point we can publish our message:

```bash
my-redis-master.default.svc.cluster.local:6379> PUBLISH channel-one message-one
my-valkey-master.default.svc.cluster.local:6379> PUBLISH channel-one message-one
```