# Installing Metrics Provider

Metrics Provider transmits metrics of [Managed Service for Kubernetes cluster](../../concepts/index.md#kubernetes-cluster) objects to monitoring systems and [automatic scaling systems](../../concepts/autoscale.md). You can also transmit metrics in the opposite direction. For example, cluster objects can receive metrics from [Yandex Monitoring](../../../monitoring/concepts/index.md).

The provider converts a request to collect external metrics from a Managed Service for Kubernetes cluster object into the required Monitoring format, and also performs the reverse conversion: from Monitoring to the cluster object.

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

1. If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

   The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

1. [Create a service account](../../../iam/operations/sa/create.md) with the `monitoring.viewer` [role](../../../iam/concepts/access-control/roles.md).
1. [Create an authorized access key](../../../iam/operations/authentication/manage-authorized-keys.md#create-authorized-key) for the [service account](../../../iam/concepts/users/service-accounts.md) in JSON format and save it to the `sa-key.json` file:

   ```bash
   yc iam key create \
     --service-account-name=<service_account_name> \
     --output=sa-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 %}

## 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. Locate the Managed Service for Kubernetes cluster you need in the list, click its name, and select the **Marketplace** tab.
1. Under **Application available for installation**, select [Metrics Provider](https://yandex.cloud/en/marketplace/products/yc/metric-provider) and click **Go to install**.
1. Configure the application:
   * **Namespace**: Create a new [namespace](../../concepts/index.md#namespace), e.g., `metrics-provider-space`. If you leave the default namespace, Metrics Provider may work incorrectly.
   * **Application name**: Specify the application name.
   * **Folder ID**: Specify the [ID of the folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) where Metrics Provider will run.
   * **Time window**: Specify the time window to collect metrics over (in `DdHhMmSs` format, e.g., `5d10h30m20s`).
   * **Disable downsampling**: Select this option not to use a data [downsampling function](../../../monitoring/concepts/decimation.md). This is an optional setting.
   * **Aggregation function**: Select the data [aggregation function](../../../monitoring/concepts/querying.md#combine-functions). This is an optional setting. The default value is `AVG`.
   * **Data filling**: Optionally, configure filling in missing data:
     * `NULL`: Returns `null` as the metric value, and `timestamp` as the timestamp value. This is a default value.
     * `NONE`: Returns no values.
     * `PREVIOUS`: Returns the value from the previous data point.
   * **Maximum number of points**: Specify the maximum number of points to return in a request response. This is an optional setting. The value of this parameter must be greater than `10`.
   * **Downsampling time window**: Specify a time window (grid) in milliseconds. This is an optional setting. 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`.

     {% note info %}

     Select either **Maximum number of points** or **Downsampling time window**. Leave these fields blank not to use either setting. For more information, see [this API guide](../../../monitoring/api-ref/MetricsData/read.md).

     {% endnote %}

   * **Secret Key**: Copy and paste the contents of the `sa-key.json` file or create a new service account access key. The service account must have the `monitoring.viewer` role.
1. Click **Install**.
1. Wait for the application status to change to `Deployed`.

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

1. [Install Helm](https://helm.sh/docs/intro/install/) v3.8.0 or higher.
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).
1. Add a repository named `metric-provider`:

   ```bash
   cat sa-key.json | helm registry login cr.yandex --username 'json_key' --password-stdin && \
   helm pull oci://cr.yandex/yc-marketplace/yandex-cloud/metric-provider/chart/metric-provider \
     --version 0.1.13 \
     --untar
   ```

   If you set `namespace` to the default namespace, Metrics Provider may work incorrectly. We recommend specifying a value different from all the existing namespaces, e.g., `metrics-provider-space`.

   {% 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 %}

1. Install and set up Metrics Provider:

   ```bash
   helm install \
     --namespace <namespace> \
     --create-namespace \
     --set yandexMetrics.folderId=<folder_ID> \
     --set window=<time_window> \
     --set-file yandexMetrics.token.serviceAccountJson=<path_to_file_with_service_account_authorized_key> \
     --set yandexMetrics.downsampling.gridAggregation=<aggregation_function> \
     --set yandexMetrics.downsampling.gapFilling=<data_filling> \
     --set yandexMetrics.downsampling.maxPoints=<maximum_number_of_points> \
     --set yandexMetrics.downsampling.gridInterval=<downsampling_time_window> \
     --set yandexMetrics.downsampling.disabled=<data_downsampling_mode> \
     metric-provider ./metric-provider/
   ```

   The following parameters are required:
   * `--namespace`: [Namespace](../../concepts/index.md#namespace) where the provider will be deployed.
   * `yandexMetrics.folderId`: [ID of the folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) where the provider will run.
   * `window`: Specify the time window to collect metrics over (in `DdHhMmSs` format, e.g., `5d10h30m20s`).
   * `yandexMetrics.token.serviceAccountJson`: Path to the file with the authorized key of the service account with the `monitoring.viewer` role.

   Downsampling parameters. For the provider to work, you need to select at least one of the parameters below:
   * `yandexMetrics.downsampling.gridAggregation`: Data [aggregation function](../../../monitoring/concepts/querying.md#combine-functions). The default value is `AVG`.
   * `yandexMetrics.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.
   * `yandexMetrics.downsampling.maxPoints`: Maximum number of points to receive in a request response. The value of this parameter must be greater than `10`.
   * `yandexMetrics.downsampling.gridInterval`: Time window (grid) 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`.
   * `yandexMetrics.downsampling.disabled`: Disable data downsampling. The possible values are `true` or `false`.

     {% note info %}

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

     {% endnote %}

## Use cases {#examples}

* [Using Metrics Provider to deliver metrics](../../tutorials/marketplace/metrics-provider.md)