Exploring KEDA Scaling to Zero

Continuing experimentation on CNCF projects I’ve stumbled across one that is near and dear to the Microsoft Azure space since KEDA was introduced with some contributors from Microsoft and still has maintainers that are current as of this repo’s README.md. To understand further on what exactly KEDA is we start at the top of what is event-driven scale for containers in Kubernetes. The core of this offering is to scale nodes based on events placing KEDA as a central point for the Kubernetes Metric Server and can assist with autoscaling rules with its use of custom resource definition.

To explore this further I’ve diagrammed this to identify components and map CRD’s.

Think of KEDA sitting as a operator-agent listening for changes as they are detected the admission webhook validates the ScaledObject and checks if it doesn’t violate any changes such as pushing out multiple jobs that are the same this will ensure your objects scaling isn’t disrupted. Additionally as this sits as a agent listening for events and working based on the events to understand how to respond up or down. It’s important to note you can have triggers defined in various areas such as a Azure Blob Storage Account, Etcd, AWS DynamoDB Streams, AWS SQS Queues and many more that are listed as shown example. Now its likely where you send these events you’ll need some form of credentials this is the intention of the TriggerAuthentication CRD. As shown at the end of the YAML code.

triggers:
- type: aws-dynamodb-streams
  metadata:
    # Required: awsRegion
    awsRegion: "ap-northeast-1"
    # Optional: awsEndpoint
    awsEndpoint: ""
    # Required: tableName
    tableName: myTableName
    # Optional targetValue
    shardCount: "2"
----
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: keda-trigger-auth-aws-credentials
  namespace: keda-test
spec:
  secretTargetRef:
  - parameter: awsRoleArn    # The property in KEDA.
    name: test-secrets       # The name of the kubernetes secret.
    key: AWS_ROLE_ARN        # The key from the kubernetes secret.
---

Demo of KEDA in Use

To first start out we will use the following pre-requisites.

  • Kubernetes Cluster (1.29 – is what I’m running)
  • Helm Installed
  • Demo Repo (https://github.com/karthikeyanVK/mongo-keda-scaler)

For installation of KEDA via helm it has other options but you can use this approach if following along.

helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda --namespace keda --create-namespace

For our example we are going to deploy

If we then run this we get the output on our scaledobject resource.

After manipulating a insert with the following command this has trigger the “Active” to True.

kubectl exec $(kubectl get pods | grep "server" | cut -f 1 -d " ") -- keda-talk mongo insert

Now we can run the opposite and see what pods are expanding based on our insert.

kubectl exec $(kubectl get pods | grep "server" | cut -f 1 -d " ") -- keda-talk mongo delete

The pods have scaled based on our inserts take a look at BEFORE.

Notice after running a delete the pods are scaling down.

Notice when we run a log and grep we get the following on the syntax found.

So as you can see the New Replicas Count 1 moves to 0 and the cluster now looks like a bare minimal MongoDB.

The majority of the logs when we run against our namespace keda we can see how the execution works chronologically starting via the TriggerAuthentication, Detect Resource Targeted, Create a new HPA, Scale Logic is then found with ScaledObject Specification.

Summary

KEDA is a well supported CNCF project that has an abundance of integrations in other cloud service providers and options to integrate further based on your needs. Based on your organizations needs if your evaluating this project to understand the best way to use this in production you’d be enlightened to know Keda has reached graduate maturity level on August 22, 2023. This can and is used in production Microsoft Azure has also added this component to natively add as a component on Azure Kubernetes Service. To learn more on the project of KEDA see the following reference links below.

References: https://www.cncf.io/projects/keda/

Demo Repo: https://github.com/karthikeyanVK/mongo-keda-scaler