Autoscaling a Dapr app with KEDA
For Kubernetes, Dapr integrates with KEDA, an event driven autoscaler for Kubernetes. Many of Dapr’s pub/sub components overlap with the scalers provided by so it’s easy to configure your Dapr deployment on Kubernetes to autoscale based on the back pressure using KEDA.
This how-to walks through the configuration of a scalable Dapr application along with the back pressure on Kafka topic, however you can apply this approach to pub/sub components offered by Dapr.
To install KEDA, follow the instructions on the KEDA website.
If you don’t have access to a Kafka service, you can install it into your Kubernetes cluster for this example by using Helm:
To check on the status of the Kafka deployment:
kubectl rollout status deployment.apps/kafka-cp-ksql-server -n kafka
kubectl rollout status statefulset.apps/kafka-cp-kafka -n kafka
When done, also deploy the Kafka client and wait until it’s ready:
kubectl -n kafka exec -it kafka-client -- kafka-topics \
--zookeeper kafka-cp-zookeeper-headless:2181 \
--topic demo-topic \
--create \
--partitions 10 \
--replication-factor 3 \
--if-not-exists
Next, we’ll deploy the Dapr Kafka pub/sub component for Kubernetes. Paste the following YAML into a file named kafka-pubsub.yaml
:
The above YAML defines the pub/sub component that your application subscribes to, the we created above. If you used the Kafka Helm install instructions above you can leave the brokers
value as is. Otherwise, change this to the connection string to your Kafka brokers.
Also notice the autoscaling-subscriber
value set for consumerID
which is used later to make sure that KEDA and your deployment use the same Kafka partition offset.
Now, deploy the component to the cluster:
Next, we will deploy the KEDA scaling object that monitors the lag on the specified Kafka topic and configures the Kubernetes Horizontal Pod Autoscaler (HPA) to scale your Dapr deployment in and out.
A few things to review here in the above file:
name
in thescaleTargetRef
section in thespec:
is the Dapr ID of your app defined in the Deployment (The value of thedapr.io/id
annotation)pollingInterval
is the frequency in seconds with which KEDA checks Kafka for current topic partition offsetminReplicaCount
is the minimum number of replicas KEDA creates for your deployment. (Note, if your application takes a long time to start it may be better to set that to1
to ensure at least one replica of your deployment is always running. Otherwise, set that to and KEDA creates the first replica for you)maxReplicaCount
is the maximum number of replicas for your deployment. Given how works, you shouldn’t set that value higher than the total number of topic partitionstopic
in the Kafkametadata
section which should be set to the same topic to which your Dapr deployment subscribe (In this exampledemo-topic
)- Similarly the
bootstrapServers
should be set to the same broker connection string used in thekafka-pubsub.yaml
file - The
consumerGroup
should be set to the same value as theconsumerID
in thekafka-pubsub.yaml
file
Next, deploy the KEDA scaler to Kubernetes:
All done!
Now, that the ScaledObject
KEDA object is configured, your deployment will scale based on the lag of the Kafka topic. More information on configuring KEDA for Kafka topics is available here.
You can now start publishing messages to your Kafka topic demo-topic
and watch the pods autoscale when the lag threshold is higher than topics, as we have defined in the KEDA scaler manifest. You can publish messages to the Kafka Dapr component by using the Dapr CLI command