Dynamic Scaling to Kubernetes with KEDA

keda

What is KEDA?

KEDA (Kubernetes-based Event-Driven Autoscaling) works as a Kubernetes (K8s) application and provides event-driven scaling in K8s clusters. This ensures that applications can be dynamically scaled according to their load.

KEDA is a versatile tool because it can listen to events from many different sources. For example, an HTTP request, a message queue or data from an IoT device can be used in scaling operations. KEDA listens for these events and scales according to your application’s load. This allows your application to run at higher performance and lower cost.

Source: https://keda.sh

You can use Helm to install KEDA. Helm is a tool for installing packages (e.g. applications and services) on K8s clusters. First, you need to install Helm. To do this, you can use the following command:

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

You can then use the following command to install KEDA with Helm:

helm repo add kedacore https://kedacore.github.io/charts
helm install keda kedacore/keda

These commands install KEDA in your cluster and create all the required K8s objects.

To use KEDA, you need to configure your application to run with KEDA. This requires your app to be defined as a “ScaledObject”. ScaledObject specifies how your app will be scaled. For example, you can specify that your app scales when it receives an HTTP request or when it receives a message queue message. ScaledObject also specifies the conditions under which your app will scale. For example, you can specify that your app scales when CPU usage is high or memory usage is high.

Scaling an application with high CPU utilization in a given time interval

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: periodic
    metadata:
      interval: "30s"
    scaleMetric:
      type: cpu
      resourceName: my-service
      targetAverageUtilization: 75

This ScaledObject scales a deployment named “my-deployment” once every 30 seconds and scales when the CPU utilization of this deployment is high. For scaling, a service named “my-service” must have an average CPU utilization above 75%.

Scaling an application that scales based on HTTP requests

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: httptrigger
    metadata:
      method: POST
      url: http://my-service/users
    scaleMetric:
      type: httprate
      resourceName: my-service
      targetAverageValue: 10
      maxScaleOutRate: 5
      minScaleInRate: 1
      cooldownPeriod: "30s"

This ScaledObject scales a deployment named “my-deployment” based on HTTP requests and scales when the average number of requests of this deployment is high. For scaling, a service named “my-service” must have an average number of requests greater than 10. Furthermore, this ScaledObject also limits the scaling speed and specifies a “cooldown” period after scaling. This prevents the scaling process from being done several times in a row and preserves the performance of the application.

Scaling an application scaled by the number of rows in a database table

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: dbpoller
    metadata:
      connectionString: "Server=my-server;Database=my-db;User Id=my-user;Password=my-password;"
      query: "SELECT COUNT(*) FROM my-table"
      pollInterval: "30s"
    scaleMetric:
      type: custom
      resourceName: my-table
      targetAverageValue: 100000
      maxScaleOutRate: 5
      minScaleInRate: 1
      cooldownPeriod: "30s"

This ScaledObject scales a deployment named “my-deployment” by running a query for the database table “my-table” once every 30 seconds and scales when the row count of this table is high. For scaling, the average number of rows in the database table “my-table” must be more than 100000. Furthermore, this ScaledObject also limits the scaling speed and specifies a “cooldown” period after scaling.

Scaling an application that listens to messages from a message queue and scales according to the number of these messages

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: mqtrigger
    metadata:
      queueName: my-queue
      connectionString: "amqp://user:password@localhost:5672/vhost"
      authMode: plain
    scaleMetric:
      type: messagecount
      resourceName: my-queue
      targetAverageValue: 1000
      maxScaleOutRate: 5
      minScaleInRate: 1
      cooldownPeriod: "30s"

This ScaledObject scales a deployment named “my-deployment” by listening for messages from a message queue named “my-queue” and scales when the number of messages in this queue is high. For scaling, the average number of messages in a message queue named “my-queue” must be more than 1000. Furthermore, this ScaledObject also limits the scaling speed and specifies a “cooldown” period after scaling.

Scaling an application scaled by the number of files in a file system

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: fsnotify
    metadata:
      directoryPath: "/path/to/my/directory"
      fileType: .csv
    scaleMetric:
      type: filecount
      resourceName: my-directory
      targetAverageValue: 10000
      maxScaleOutRate: 5
      minScaleInRate: 1
      cooldownPeriod: "30s"

This ScaledObject scales a deployment named “my-deployment” by listening to a filesystem folder named “my-directory” and scales when the number of files of type “.csv” in this folder is high. For scaling, the average number of “.csv” files in the filesystem folder named “my-directory” must be greater than 10000. Furthermore, this ScaledObject also limits the scaling speed and specifies a “cooldown” period after scaling.

Scaling an application that scales based on data from an IoT device

apiVersion: keda.k8s.io/v1alpha1
kind: ScaledObject
metadata:
  name: my-scaledobject
spec:
  scaleTargetRef:
    deploymentName: my-deployment
  triggers:
  - type: iotdevice
    metadata:
      connectionString: "HostName=my-iot-hub.azure-devices.net;DeviceId=my-device;SharedAccessKey=XYZ"
    scaleMetric:
      type: devicetelemetry
      resourceName: my-device
      targetAverageValue: 75
      metricName: "temperature"
      maxScaleOutRate: 5
      minScaleInRate: 1
      cooldownPeriod: "30s"

This ScaledObject scales a deployment named “my-deployment” based on data from an IoT device named “my-device” and scales when the value of a data named “temperature” from this device is high. For scaling, the average value of “temperature” from an IoT device named “my-device” must be greater than 75. Furthermore, this ScaledObject also limits the scaling speed and specifies a “cooldown” period after scaling.

References:
https://keda.sh/

Leave a Reply

Your email address will not be published. Required fields are marked *