DEV Community

Cover image for Scale Deployments Based on HTTP Requests with Keda
CodeWithVed
CodeWithVed

Posted on • Updated on

Scale Deployments Based on HTTP Requests with Keda

I'm using CloudWatch as a trigger, which means I have installed the CloudWatch agent responsible for sending metrics to CloudWatch. My KEDA fetches metrics based on the collection interval defined in the configuration of ScaledObject.
You can follow the configuration below to set up the ScaledObject based on the number of HTTP requests.


apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: <ScaledObjectName>
spec:
  maxReplicaCount: 10
  minReplicaCount: 1
  pollingInterval: 10
  scaleTargetRef:
    name: <DeploymentName>
  triggers:
    - type: aws-cloudwatch
      name: <Name>
      authenticationRef:
        name: <AwsCredentialName>
      metadata:
        namespace: "CWAgent"
        dimensionName: <DimensionName1;DimensionName2>
        dimensionValue: <DimensionValue1;DimensionValue2>
        metricName: <MetricsName>
        metricEndTimeOffset: "10"
        metricCollectionTime: "10"
        targetMetricValue: "1000"
        minMetricValue: "0"
        metricStat: "Sum"
        metricStatPeriod: "10"
        awsRegion: "us-east-1"

Enter fullscreen mode Exit fullscreen mode

Also, instead of dimensionName and dimensionValue you can directly use the expression in your configuration like below:


apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: <ScaledObjectName>
spec:
  maxReplicaCount: 10
  minReplicaCount: 1
  pollingInterval: 10
  scaleTargetRef:
    name: <DeploymentName>
  triggers:
    - type: aws-cloudwatch
      name: <Name>
      authenticationRef:
        name: <AwsCredentialName>
      metadata:
        expression: "SELECT SUM(<metric-name>) FROM SCHEMA(CWAgent, metric_type,path) WHERE path = <your-path-name> AND metric_type = <metric-type>"
        metricCollectionTime: <metrics_collection_time>
        targetMetricValue: <target_metric_value>
        minMetricValue: <min_metric_value>
        metricStatPeriod : <metrics_stat_period>
        awsRegion: "us-east-1"

Enter fullscreen mode Exit fullscreen mode

Breakdown of the Configuration
apiVersion: keda.sh/v1alpha1
This specifies the version of the KEDA API being used, indicating that this is a version 1 alpha release of KEDA's scaling capabilities

  • kind: ScaledObject This indicates that the resource being defined is a ScaledObject, which is responsible for managing the scaling behavior of a Kubernetes workload based on external metrics metadata:
  • name: This is the unique name for the ScaledObject within the Kubernetes namespace.
  • spec:
  • maxReplicaCount: 10 This sets the maximum number of replicas that the deployment can scale up to, ensuring that resource limits are respected
  • minReplicaCount: 1 This defines the minimum number of replicas that should always be running, preventing the deployment from scaling down to zero
  • pollingInterval: 10 This specifies the interval (in seconds) at which KEDA checks the defined triggers to determine if scaling actions are needed
  • scaleTargetRef:
  • name: This references the name of the Kubernetes deployment that KEDA will scale based on the metrics collected 3. triggers: This section defines the conditions under which scaling actions will occur. In this case, it uses AWS CloudWatch metrics:
  • type: aws-cloudwatch Specifies that the trigger type is based on metrics from AWS CloudWatch 5. name: A unique identifier for the trigger.
  • authenticationRef:
  • name: This references the credentials required to access AWS CloudWatch metrics.
  • metadata:
  • namespace: "CWAgent" The namespace where the CloudWatch agent is deployed.
  • dimensionName: Specifies the dimensions for the CloudWatch metric, allowing for filtering based on specific criteria.
  • dimensionValue: Corresponding values for the specified dimensions.
  • metricName: The name of the CloudWatch metric to monitor.
  • metricEndTimeOffset: "10" The offset in seconds for the end time of the metric data being queried.
  • metricCollectionTime: "10" The time interval in seconds for collecting the metric data.
  • targetMetricValue: "1000" The desired value of the metric that triggers scaling actions.
  • minMetricValue: "0" The minimum value of the metric that is considered for scaling.
  • metricStat: "Sum" Specifies the statistical method used to aggregate the metric data.
  • metricStatPeriod: "10" The period in seconds over which the metric statistics are calculated.
  • awsRegion: "us-east-1" The AWS region where the CloudWatch metrics are being monitored

Just because of the cloudwatch trigger i Needed to specify another TriggerAuthentication kind which allows you to describe authentication parameters separately from the ScaledObject.


apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: <AwsCredentialName>
spec:
  podIdentity:
    provider: aws

Enter fullscreen mode Exit fullscreen mode

If you are using Prometheus locally then you wont need to specify the TriggerAuthentication, you will just need a ScaledObject to scale your deployment.

Top comments (0)