To get up and running with the state and Pub/sub building blocks, you’ll need two components:
A full list of supported components can be found here:
For this tutorial, we describe how to get up and running with Redis.
Dapr can use any Redis instance, either:
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! Skip ahead to the next steps.
You can use Helm to create a Redis instance in our Kubernetes cluster. Before beginning, install Helm v3.
Install Redis into your cluster:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis --set image.tag=6.2
For Dapr’s Pub/sub functionality, you’ll need at least Redis version 5. For state store, you can use a lower version.
Note that adding --set architecture=standalone
to the install
command creates a single replica Redis setup, which can save memory and resources if you are working in a local environment.
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-replicas-0 1/1 Running 0 69s
redis-replicas-1 1/1 Running 0 22s
For Kubernetes:
redis-master.default.svc.cluster.local:6379
redis
, is created automatically.Verify you have an Azure subscription.
xxxxxx.redis.cache.windows.net:6380
.Navigate to Access Keys under Settings.
Create a Kubernetes secret to store your Redis password:
kubectl create secret generic redis --from-literal=redis-password=*********
Deploy a Redis instance from AWS Redis.
Note the Redis hostname in the AWS portal for later.
Create a Kubernetes secret to store your Redis password:
kubectl create secret generic redis --from-literal=redis-password=*********
Deploy a MemoryStore instance from GCP Cloud MemoryStore.
Note the Redis hostname in the GCP portal for later.
Create a Kubernetes secret to store your Redis password:
kubectl create secret generic redis --from-literal=redis-password=*********
Dapr defines resources to use for building block functionality with components. 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
Since Kubernetes files are applied with kubectl
, they can be created in any directory.
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: localhost:6379
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
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
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.
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: localhost:6379
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
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
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.
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>
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
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>
# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
# - name: enableTLS
# value: true
When you run dapr init
, Dapr creates a default redis pubsub.yaml
on your local machine. Verify by opening your components directory:
%UserProfile%\.dapr\components\pubsub.yaml
~/.dapr/components/pubsub.yaml
For new component files:
components
directory in your app folder containing the YAML files.dapr run
command with the flag --resources-path
If you initialized Dapr in slim mode (without Docker), you need to manually create the default directory, or always specify a components directory using --resources-path
.
Run kubectl apply -f <FILENAME>
for both state and pubsub files:
kubectl apply -f redis-state.yaml
kubectl apply -f redis-pubsub.yaml