What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
Super-fast S3 Express storage.
New Graviton 4 processor instances.
Emily Freeman leaving AWS.
I don't use AWS, so none of this will affect me.
Kubernetes / Microservices / Software Development

Tutorial: First Look at Dapr for Microservices Running in Kubernetes

Dapr is a portable, event-driven runtime for distributed systems originally developed by Microsoft. This hands-on guide will walk you through all the steps involved in dealing with state management in Dapr.
Feb 14th, 2020 9:39am by
Featued image for: Tutorial: First Look at Dapr for Microservices Running in Kubernetes
Feature image via Pixabay.

In a previous article, I introduced the architecture and building blocks of Dapr, a portable, event-driven runtime for distributed systems originally developed by Microsoft. To appreciate the platform, let’s zoom into the state management building block of Dapr. This hands-on guide will walk you through all the steps involved in dealing with state management in Dapr.


We are going to deploy two microservices written in Node.js and Python in Kubernetes. The services will use Redis as the persistence layer to store the state. Since we use Dapr, we will swap out Redis with etcd while continuing to run the microservices.

To complete this tutorial, you need a Kubernetes cluster running within Minikube or a managed service such as the Azure Kubernetes Service (AKS).

I am running Minikube on my development machine.

Installing Dapr

Download the Dapr CLI for your OS from the releases page of GitHub repository, rename and add the binary to the path.

Run the below command to install Dapr in your Kubernetes environment.

The installer deploys a few Pods in the default Namespace that are a part of Dapr control plane. Like the service mesh, Dapr has a control plane that integrates with Kubernetes and a data plane that runs as a sidecar inside each Pod.

The dapr-operator Pod watches for Pods that have the Dapr Annotations. The dapr-sidecar-injector Pod is responsible for adding the sidecar container to each Pod annotated as “ true”. Finally, the dapr-placement Pod manages the communication across all the sidecar containers injected into the Pods.

Configuring Redis as the Persistence Layer

Since we are dealing with the state store, the next step is to deploy Redis and configuring it as the default state store for the microservices.

Deploy Redis by submitting the below YAML file to Kubernetes. This results in the creation of a Pod and a ClusterIP Service.

With the Redis Pod up and running, let’s configure it as a Dapr state store. Create a YAML file with the below specification and apply it.

This creates a Rudr component for Redis. Rudr is a reference implementation of the Open Application Model (OAM) specification jointly created by Microsoft and Alibaba. For more details on OAM and Rudr, refer to this article and the tutorial.

Deploying Microservices That Use Dapr State Store

Let’s start by creating a Pod and Service spec that runs the first microservice written in Node.js. You can take a look at the code for this microservice at Dapr samples repo.

Note that the Pod has Annotations for Dapr which act as a hint to inject the sidecar container.

If you analyze the code, you realize that the microservice is not directly referring to the persistent store in the microservice. Instead, it makes a call to the REST endpoint exposed by Dapr runtime. The sidecar container is responsible for enabling this communication between the microservice and the Dapr runtime.

Let’s deploy the second microservice written in Python that continuously invokes the API exposed by the first service deployed in the previous step.

Since both the Pods are annotated for Dapr, the control plane has injected the sidecar into the Pods.

Checking the logs of the Node Pod shows that the state is being persisted.

You can also access the same from the NodePort of Minikube.

You can check the key/value pair by accessing the CLI in the Redis Pod.

Replacing Redis State Store with etcd

Dapr also supports etcd as one of the Components for the state store building block service. Let’s now replace the Redis state store with etcd
First, create a etcd cluster. You can use a Helm Chart or the below YAML spec to deploy a 3-node etcd cluster in Kubernetes.

The etcd endpoint is exposed as a ClusterIP Service.

Now, we can delete the existing state store Component and recreate it with a pointer to etcd.

The below spec creates an etcd State Store:

Delete the Node.js Pod to force Kubernetes to launch a new Pod.

Wait for the Node.js Pod to become ready before checking if everything is intact by invoking the NodePort endpoint of the first microservice.

You can also use etcdctl, the client, from one of the etcd Pods to check the key/value pair maintaining the state.

This tutorial demonstrated how to use Dapr state service with Kubernetes. In one of the future tutorials, we will explore the resource binding building block of Dapr.

Janakiram MSV’s Webinar series, “Machine Intelligence and Modern Infrastructure (MI2)” offers informative and insightful sessions covering cutting-edge technologies. Sign up for the upcoming MI2 webinar at

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.