Event driven autoscaling in kubernetes using KEDA

Purushotham Reddy
4 min readApr 11, 2023

--

Overview:

In k8s we have a concept called HPA(Horizontal pod autoscaling) which means increasing the number of replicas or instances of application based on load. HPA scaling is classified into 3 categories.

  • Traditional scaling(Based on CPU & Memory of app. This is possible using Metric server.)
  • Custom Metric scaling(Based on Custom metrics of application. This is possible with integration of prometheus where application metrics get stored.)
  • External Scaling(Event driven autoscaling using Keda)

In this blog we are going to discuss about the third option i.e Event Driver autoscaling.

KEDA stands for kubernetes event driven autoscaling. With KEDA you can scale pods/containers in kubernetes based on the number of events needing to be processed.

KEDA works alongside with HPA. Keda is very light weight component that can be added to k8s cluster. with the help of KEDA metrics are exposed at external.metrics.k8s.io at the kubeapi level. These metrics will be consumed by HPA.

Pre-requisites

  • k8s cluster

Note: if you dont have kubenetes cluster, you can quickly create one using Kind. Refer to my blog for instructions.

Workflow

Scalers:

in KEDA we have something called scalers which is basically the event driven source i.e based on which the app pods gets scaled up or scaled down.

Scaled Object or Scaled Job:

This is a custom resource definition i.e basically a mapping between Scaler to Target app.

Scaled Object is used to map the target deployments, customresource definitions or CRDS.

Scaled Job is used to map the target jobs(k8s jobs)

Application

Target Application which gets scaled up or scaled down.

ex: Deployments or Statefulsets

There are inbuilt scalers available. You can refer to this link for complete list

Installation

KEDA can be installed in multiple ways.

  • Helm charts
  • Operator
  • Yaml manifest

In this blog we are going to look at easy method i.e through using yaml manifest.

kubectl apply -f https://github.com/kedacore/keda/releases/download/v2.10.0/keda-2.10.0.yaml

The above command installs bunch of resources. Please find the screenshot below for reference.

wait until all the components move into running state. you can verify it using below command.

kubectl get all -n keda

Besides the general pods, Custom resource definitions are also created. you can verify it using below command.

kubectl get crds| grep keda

Scaling apps using Cron Scaler

As mentioned earlier we have something called scaler(which is a source) using which we will be scaling the target app. We will be using Cron Scaler in this blog to understand keda better.

For Lets deploy a target app. We will be installing nginx deploymet for a target application.

kubectl create deployment nginx --image nginx --replicas 1
kubectl get pods

Now lets create cron scaler with the below content. create a file named cron-scaler.yaml with below content.

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: cron-scaledobject
namespace: default
spec:
scaleTargetRef:
name: nginx
triggers:
- type: cron
metadata:
timezone: Asia/Kolkata
start: 25 * * * *
end: 30 * * * *
desiredReplicas: "10"

Apply the manifest file.

kubectl apply -f cron-scaler.yaml

The above command creates ScaledObject CRD which then creates HPA under the hood. Execute the below command.

kubectl get scaledobject 
kubectl get hpa

Now lets watch the pods.

watch -x kubectl get pods

As you can see the from the above screenshots pods are scaled up automatically when the current time reaches the cron specified start time. Similarly pods gets scaled down automatically when the current times reaches the cron specified end time. The other attributes specified in the manifest are self explanatory.

After reaching the end time.

Summary:

In this blog we have seen what is keda, How keda helps in autoscaling the apps based on events. We have also seen how to use Cron as a scaler to autoscale the apps.

In the next blogs we can discuss more about other scalers and other keda features .

--

--

No responses yet