[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for Kubernetes](../../index.md) > [Step-by-step guides](../index.md) > Installing applications from Cloud Marketplace > Installing KEDA

# Installing KEDA with Yandex Monitoring support

[KEDA](https://keda.sh) is an application that performs autoscaling of Kubernetes resources based on load monitoring.

KEDA application:

* Integrates Kubernetes autoscaling with Yandex Monitoring.
* Allows you to scale applications based on metrics from Monitoring, such as Application Load Balancer metrics or managed databases.
* Supports time window configuration, as well as aggregation and processing of `NaN` values.

## Getting started {#before-you-begin}

1. [Create a service account](../../../iam/operations/sa/create.md):

   ```bash
   yc iam service-account create --name keda-sa
   ```

1. [Assign](../../../iam/operations/roles/grant.md) the `monitoring.viewer` role to the service account you created earlier:

   ```bash
   yc resource-manager folder add-access-binding \
     --id <folder_ID> \
     --service-account-name keda-sa \
     --role monitoring.viewer
   ```

   You can get the folder ID with the [list of folders](../../../resource-manager/operations/folder/get-id.md).

1. [Create an authorized key](../../../iam/operations/authentication/manage-authorized-keys.md#create-authorized-key) for the service account you created earlier and save it to a file named `authorized-key.json`:

   ```bash
   yc iam key create \
     --service-account-name keda-sa \
     --output authorized-key.json
   ```

1. [Make sure](../connect/security-groups.md) the security groups for the Managed Service for Kubernetes cluster and its node groups are configured correctly. If a rule is missing, [add it](../../../vpc/operations/security-group-add-rule.md).

    {% note warning %}
    
    The configuration of security groups determines performance and availability of the cluster and the services and applications running in it.
    
    {% endnote %}

1. [Install kubect](https://kubernetes.io/docs/tasks/tools/install-kubectl) and [configure it to work with the new cluster](../connect/index.md#kubectl-connect).

## Installation from Yandex Cloud Marketplace {#marketplace-install}

1. In the [management console](https://console.yandex.cloud), select a folder.
1. Navigate to **Managed Service for&nbsp;Kubernetes**.
1. Click the name of the [Managed Service for Kubernetes](../../concepts/index.md#kubernetes-cluster) cluster you need and select the ![image](../../../_assets/console-icons/shopping-cart.svg) **Marketplace** tab.
1. Under **Application available for installation**, select [KEDA with Yandex Monitoring support](https://yandex.cloud/en/marketplace/products/yc/keda) and click **Go to install**.
1. Configure the application:
   * **Namespace**: Create a new [namespace](../../concepts/index.md#namespace) named `keda-system`. If you keep the default namespace, KEDA may work incorrectly.
   * **Application name**: Specify the application name.
   * **Service account key**: Copy the contents of the `authorized-key.json` file to this field.

1. Click **Install**.
1. Wait for the application to change its status to `Deployed`.
1. Create the `ScaledObject` resource with these resource autoscaling settings:

   ```yaml
   apiVersion: keda.sh/v1alpha1
   kind: ScaledObject
   metadata:
     name: <resource_name>
   spec:
     scaleTargetRef:
       name: <autoscaling_resource_name>
     pollingInterval: <monitoring_event_check_interval>
     cooldownPeriod: <waiting_period_after_event>
     minReplicaCount: <minimum_number_of_replicas_for_autoscaling>
     maxReplicaCount: <maximum_number_of_replicas_for_autoscaling>
     triggers:
     - type: external
       metadata:
         scalerAddress: keda-external-scaler-yc.keda-system.svc.cluster.local:8080
         query: <Yandex_Monitoring_metric_request>
         folderId: "<folder_ID>"
         targetValue: "<target_metric_value>"
         downsampling.disabled: <enable_data_decimation_mode>
         downsampling.gridAggregation: "<data_aggregation_function>"
         downsampling.gridInterval: "<downsampling_time_window>"
         downsampling.maxPoints: <maximum_number_of_points>
         downsampling.gapFilling: <data_filling>

         timeWindow: "<time_interval_window>"
         timeWindowOffset: "<time_window_indent>"
    
         logLevel: "<logging_level>"
         logMetrics: "<allow_Monitoring_request_logging>"
         logAggregation: "<allow_data_aggregation_logging>"
   ```

   Required metadata in the `triggers` field:

   * `scalerAddress`: Autoscaling service address. It is always `keda-external-scaler-yc.keda-system.svc.cluster.local:8080`.
   * `query`: Monitoring metrics request.
   * `folderId`: [ID of the folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) where the provider will run.
   * `targetValue`: Target metric value, exceeding which adds a pod for the replica.

   Downsampling parameters. For autoscaling to work, you need to select at least one of these parameters:
   * `downsampling.gridAggregation`: Data [aggregation function](../../../monitoring/concepts/querying.md#combine-functions). The possible values are `MAX`, `MIN`, `SUM`, `AVG`, `LAST`, or `COUNT`. The default value is `AVG`.
   * `downsampling.gapFilling`: Settings for filling in missing data:
     * `NULL`: Returns `null` as the metric value, and `timestamp` as the timestamp value.
     * `NONE`: Returns no values.
     * `PREVIOUS`: Returns the value from the previous data point.
   * `downsampling.maxPoints`: Maximum number of points to receive in a request response. The value of this parameter must be greater than `10`.
   * `downsampling.gridInterval`: Time window (grid) width in milliseconds. It is used for downsampling: points inside the window are merged into a single one using the aggregation function. The value of this parameter must be greater than `0`.
   * `downsampling.disabled`: Disable data downsampling. The possible values are `true` or `false`.

     {% note info %}

     Use only one of these parameters: `downsampling.maxPoints`, `downsampling.gridInterval`, or `downsampling.disabled`. For more information about downsampling parameters, see [this API guide](../../../monitoring/api-ref/MetricsData/read.md).

     {% endnote %}

   Time window parameters:
   * `timeWindow`: Time period for calculating the aggregation function (in `DdHhMmSs` format, e.g., `5d10h30m20s`).
   * `timeWindowOffset`: Delay in calculating the function in the time window.

   Logging parameters:
   * `logLevel`: Logging level. The possible values are `debug`, `info`, `warn`, `error`, and `none`. The default value is `info`.
   * `logMetrics`: Allow logging of Monitoring requests, `true` or `false`. The default value is `false`.
   * `logAggregation`: Allow data aggregation logging, `true` or `false`. The default value is `false`.

   For more information on the `ScaledObject` resource parameters, see the [project's guide on Github](https://github.com/yandex-cloud/yc-keda-external-scaler).

## Installation using a Helm chart {#helm-install}

1. [Install Helm](https://helm.sh/docs/intro/install/) v3.8.0 or higher.

1. To install a [Helm chart](https://helm.sh/docs/topics/charts/) with KEDA, run this command:

   ```bash
   helm pull oci://cr.yandex/yc-marketplace/yandex-cloud/keda/chart/keda-external-scaler-yc \
     --version 1.1.3 \
     --untar && \
   helm install \
     --namespace keda-system \
     --create-namespace \
     --set-file secret.data=./authorized-key.json \
     --set keda.enabled=true \
     keda ./keda
   ```

   If you set `namespace` to the default namespace, KEDA may work incorrectly.

   {% note info %}
   
   If you are using a Helm version below 3.8.0, add the `export HELM_EXPERIMENTAL_OCI=1 && \` string at the beginning of the command to enable [Open Container Initiative](https://opencontainers.org/) (OCI) support in the Helm client.
   
   {% endnote %}

## An example of using KEDA {#keda-usage}

1. Deploy `nginx` in the Managed Service for Kubernetes cluster.
1. Install [ALB Ingress Controller](alb-ingress-controller.md) and the `Ingress` resource for `nginx`.
1. Create the `ScaledObject` resource with these settings for autoscaling web application pods based on Application Load Balancer metrics:

   ```yaml
   apiVersion: keda.sh/v1alpha1
   kind: ScaledObject
   metadata:
     name: demo-app-scaler
   spec:
     scaleTargetRef:
       name: <Deployment_resource_name>
     pollingInterval: 30
     cooldownPeriod: 60
     minReplicaCount: 1
     maxReplicaCount: 20
     triggers:
     - type: external
       metadata:
         scalerAddress: keda-external-scaler-yc.keda-system.svc.cluster.local:8080
         query: |
          alias(replace_nan(series_sum("load_balancer.requests_count_per_second"{
          service="application-load-balancer", 
          load_balancer="<load_balancer_ID>", 
          http_router="*", virtual_host="*", 
          route="<route_ID>", 
          backend_group="*", backend="*", 
          zone="*", code="total"}), 0), "Total requests count")
         folderId: "<folder_ID>"
         targetValue: "<target_metric_value>"
         downsampling.gridAggregation: "AVG"
         downsampling.gridInterval: "120000"

         timeWindow: "2m"
         timeWindowOffset: "30s"
    
         logLevel: "debug"
         logMetrics: "true"
         logAggregation: "true"
   ```

## See also {#see-also}

* [KEDA guides](https://keda.sh/docs/)
* [KEDA with Yandex Monitoring support on GitHub](https://github.com/yandex-cloud/yc-keda-external-scaler)