In order to get up and running with the state and pub/sub building blocks two components are needed:
A full list of supported components can be found here:
The rest of this page describes how to get up and running with Redis.
Dapr can use any Redis instance - either containerized on your local dev machine or a managed cloud service. If you already have a Redis store, move on to the configuration section.
Redis is automatically installed in self-hosted environments by the Dapr CLI as part of the initialization process. You are all set and can skip to the [next steps](next steps)
You can use Helm to quickly create a Redis instance in our Kubernetes cluster. This approach requires Installing Helm v3.
Install Redis into your cluster:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis
Note that you will need a Redis version greater than 5, which is what Dapr’s pub/sub functionality requires. If you’re intending on using Redis as just a state store (and not for pub/sub) a lower version can be used.
Run kubectl get pods
to see the Redis containers now running in your cluster:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-0 1/1 Running 0 69s
redis-slave-0 1/1 Running 0 69s
redis-slave-1 1/1 Running 0 22s
Note that the hostname is redis-master.default.svc.cluster.local:6379
, and a Kubernetes secret, redis
, is created automatically.
This method requires having an Azure Subscription.
xxxxxx.redis.cache.windows.net:6380
. Note this for later.kubectl create secret generic redis --from-literal=redis-password=*********
kubectl create secret generic redis --from-literal=redis-password=*********
kubectl create secret generic redis --from-literal=redis-password=*********
Dapr uses components to define what resources to use for building block functionality. These steps go through how to connect the resources you created above to Dapr for state and pub/sub.
In self-hosted mode, component files are automatically created under:
%USERPROFILE%\.dapr\components\
$HOME/.dapr/components
For Kubernetes, files can be created in any directory, as they are applied with kubectl
.
Create a file named redis-state.yaml
, and paste the following:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: default
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: <REPLACE WITH HOSTNAME FROM ABOVE - for Redis on Kubernetes it is redis-master.default.svc.cluster.local:6379>
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
This example uses the the kubernetes secret that was created when setting up a cluster with the above instructions.
Create a file called redis-pubsub.yaml, and paste the following:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
namespace: default
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: <REPLACE WITH HOSTNAME FROM ABOVE - for Redis on Kubernetes it is redis-master.default.svc.cluster.local:6379>
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
This example uses the the kubernetes secret that was created when setting up a cluster with the above instructions.
For development purposes only you can skip creating kubernetes secrets and place passwords directly into the Dapr component file:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: default
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: <HOST>
- name: redisPassword
value: <PASSWORD>
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
namespace: default
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: <HOST>
- name: redisPassword
value: <PASSWORD>
By default the Dapr CLI creates a local Redis instance when you run dapr init
. However, if you want to configure a different Redis instance you can either:
$HOME/.dapr/components
%USERPROFILE%\.dapr\components
components
directory in your app folder containing the YAML files and provide the path to the dapr run
command with the flag --components-path
--components-path
.Run kubectl apply -f <FILENAME>
for both state and pubsub files:
kubectl apply -f redis-state.yaml
kubectl apply -f redis-pubsub.yaml