[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for Kubernetes](../../index.md) > [Tutorials](../index.md) > Using Cloud Marketplace products > Using Metrics Provider to deliver metrics

# Using Metrics Provider to deliver metrics

Metrics Provider delivers metrics of Managed Service for Kubernetes [cluster](../../concepts/index.md#kubernetes-cluster) objects to monitoring systems and [automatic scaling systems](../../concepts/autoscale.md).

In this tutorial, you will learn how to set up the delivery of external metrics to Horizontal Pod Autoscaler using Metrics Provider.

To set up the delivery of metrics:

1. [Set up the runtime environment](#create-files).
1. [Install Metrics Provider and the runtime environment](#install).
1. [Test Metrics Provider](#validate).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

The support cost for this solution includes:

* Fee for using the master and outgoing traffic in a Managed Service for Kubernetes cluster (see [Managed Service for Kubernetes pricing](../../pricing.md)).
* Fee for using computing resources, OS, and storage in cluster nodes (VMs) (see [Compute Cloud pricing](../../../compute/pricing.md)).
* Fee for a public IP address assigned to cluster nodes (see [Virtual Private Cloud pricing](../../../vpc/pricing.md#prices-public-ip)).


## 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 security groups](../../operations/connect/security-groups.md) for the Managed Service for Kubernetes cluster and its node groups.

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

1. [Create a Managed Service for Kubernetes cluster](../../operations/kubernetes-cluster/kubernetes-cluster-create.md) and [node group](../../operations/node-group/node-group-create.md) with any suitable configuration. When creating, specify the preconfigured security groups.

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

## Set up the runtime environment {#create-files}

To test Metrics Provider, you will create the `nginx` test app and [Horizontal Pod Autoscaler](../../concepts/autoscale.md#hpa) to receive CPU utilization metrics from Metrics Provider.
1. Create the `app.yaml` file with the `nginx` app manifest:

   {% cut "app.yaml" %}

   ```yaml
   ---
   ### Deployment
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: nginx
     namespace: kube-system
     labels:
       app: nginx
   spec:
     replicas: 1
     selector:
       matchLabels:
         app: nginx
     template:
       metadata:
         name: nginx
         labels:
           app: nginx
       spec:
         containers:
           - name: nginx
             image: registry.k8s.io/hpa-example
             resources:
               requests:
                 memory: "256Mi"
                 cpu: "500m"
               limits:
                 memory: "500Mi"
                 cpu: "1"
   ---
   ### Service
   apiVersion: v1
   kind: Service
   metadata:
     name: nginx
     namespace: kube-system
   spec:
     selector:
       app: nginx
     ports:
       - protocol: TCP
         port: 80
         targetPort: 80
     type: LoadBalancer
   ```

   {% endcut %}

1. Create the `hpa.yaml` file with the Horizontal Pod Autoscaler manifest for `test-hpa`:

   ```yaml
   apiVersion: autoscaling/v2beta2
   kind: HorizontalPodAutoscaler
   metadata:
     name: test-hpa
     namespace: kube-system
   spec:
     maxReplicas: 2
     scaleTargetRef:
       apiVersion: apps/v1
       kind: Deployment
       name: nginx
     metrics:
       - type: External
         external:
           metric:
             name: cpu_usage
             selector:
               matchLabels:
                 service: "compute"
                 resource_id: "<node_name>"
                 resource_type: "vm"
           target:
             type: Value
             value: "20"
   ```

   You can get the name of the node for deploying Metrics Provider and the runtime environment with the list of cluster nodes:

   ```bash
   kubectl get nodes
   ```

## Install Metrics Provider and the runtime environment {#install}

1. Install Metrics Provider by following [this guide](../../operations/applications/metrics-provider.md).
1. Create a test application and Horizontal Pod Autoscaler:

   ```bash
   kubectl apply -f app.yaml && \
   kubectl apply -f hpa.yaml
   ```

1. Make sure the app [pods](../../concepts/index.md#pod) switched to `Running`:

   ```bash
   kubectl get pods -n kube-system | grep nginx && \
   kubectl get pods -n kube-system | grep metrics
   ```

   Result:

   ```text
   nginx-6c********-dbfrn                      1/1     Running   0          2d22h
   nginx-6c********-gckhp                      1/1     Running   0          2d22h
   metrics-server-v0.3.1-6b********-f7dv6      2/2     Running   4          7d3h
   ```

## Test Metrics Provider {#validate}

Make sure that Horizontal Pod Autoscaler receives metrics from Metrics Provider and uses them to calculate the number of the `nginx` app pods:

```bash
kubectl -n kube-system describe hpa test-hpa
```

In the expected command output, the `AbleToScale` and `ScalingActive` conditions should be `True`:

```text
Name:                          test-hpa
Namespace:                     kube-system
...
Min replicas:                  1
Max replicas:                  2
Deployment pods:               2 current / 2 desired
Conditions:
  Type            Status  Reason            Message
  ----            ------  ------            -------
  AbleToScale     True    ReadyForNewScale  recommended size matches current size
  ScalingActive   True    ValidMetricFound  the HPA was able to successfully calculate a replica count from external metric cpu_usage(&LabelSelector{MatchLabels:map[string]string{resource_id: <node_name>,resource_type: vm,service: compute,},MatchExpressions:[]LabelSelectorRequirement{},})
Events:           <none>
```

{% note info %}

Metrics Provider will take some time to fetch metrics from the Managed Service for Kubernetes cluster. If the `unable to get external metric ... no metrics returned from external metrics API` error occurs, repeat the provider test after a few minutes.

{% endnote %}

## Delete the resources you created {#clear-out}

Delete the resources you no longer need to avoid paying for them:

1. [Delete the Managed Service for Kubernetes cluster](../../operations/kubernetes-cluster/kubernetes-cluster-delete.md).
1. [Delete](../../../vpc/operations/address-delete.md) the cluster [public static IP address](../../../vpc/concepts/address.md#public-addresses) if you reserved one.