Updated on 2024-04-25 GMT+08:00

GPU-based HPA Practice

If there are GPU nodes in a cluster, you can view the GPU resource usage of the nodes through GPU metrics, such as the GPU usage and used GPU memory. After obtaining GPU monitoring metrics, you can configure auto scaling policies based on the GPU metrics of applications to adaptively adjust the number of nodes for the applications when services fluctuate.

Prerequisites

  • A cluster is available, and there are GPU nodes and GPU related services running in the cluster.
  • The CCE AI Suite (NVIDIA GPU) add-on has been installed in the cluster, and the add-on metrics API is working properly. You can log in to the GPU node and run the following command:
    curl {Pod IP}:2112/metrics

    In the preceding command, {Pod IP} indicates the pod IP address of the GPU add-on. If the metric result is returned, the GPU add-on is running properly.

  • The Cloud Native Cluster Monitoring add-on of v3.9.5 or later has been installed in the cluster, and it is deployed in server mode.

Collecting GPU Metrics

  1. Log in to the CCE console and click the cluster name to access the cluster console. In the navigation pane, choose ConfigMaps and Secrets.
  2. Select the monitoring namespace. On the ConfigMaps tab, locate the row containing user-adapter-config and click Update.

    Figure 1 Updating a ConfigMap

  3. On the Update ConfigMap page, click Edit in the Operation column of the config.yaml file in the Data pane. Then, add a custom metric collection rule under the rules field. Click OK.

    You can add multiple collection rules by adding multiple configurations under the rules field. For details, see Metrics Discovery and Presentation Configuration.

    The following is an example of a custom rule for collecting cce_gpu_memory_utilization. For details about more GPU metrics, see Monitoring GPU Metrics.
    rules:
    - seriesQuery: '{__name__=~"cce_gpu_memory_utilization",container!="",namespace!="",pod!=""}'
      seriesFilters: []
      resources:
        overrides:
          namespace:
            resource: namespace
          pod:
            resource: pod
      metricsQuery: sum(last_over_time(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)
    Figure 2 Customizing a collection rule

  4. Redeploy the custom-metrics-apiserver workload in the monitoring namespace.

    Figure 3 Redeploying custom-metrics-apiserver

  5. After the restart, check whether the metrics in the target pod are normal (replace the namespace and service pod names).

    # Obtain metrics.
    $ kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"
    
    {"kind":"APIResourceList","apiVersion":"v1","groupVersion":"custom.metrics.k8s.io/v1beta1","resources":[{"name":"pods/cce_gpu_memory_utilization","singularName":"","namespaced":true,"kind":"MetricValueList","verbs":["get"]},{"name":"namespaces/cce_gpu_memory_utilization","singularName":"","namespaced":false,"kind":"MetricValueList","verbs":["get"]}]}
    
    # Obtain workload metric values.
    $ kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/test-gpu-hpa-68667fdd94-grmd2/cce_gpu_memory_utilization"
    
    {"kind":"MetricValueList","apiVersion":"custom.metrics.k8s.io/v1beta1","metadata":{},"items":[{"describedObject":{"kind":"Pod","namespace":"default","name":"test-gpu-hpa-68667fdd94-grmd2","apiVersion":"/v1"},"metricName":"cce_gpu_memory_utilization","timestamp":"2024-01-10T08:36:44Z","value":"20","selector":null}]}

Creating an Auto Scaling Policy

  1. Choose Workloads in the navigation pane. Locate the target workload and choose More > Auto Scaling in the Operation column.
  2. Set Policy Type to HPA+CronHPA and enable HPA.

    You can select GPU monitoring parameters in Custom Policy to create an auto scaling policy. The following is an example.

    Figure 4 Selecting custom metrics

    In this example, cce_gpu_memory_utilization (GPU memory usage) is used as the scaling metric. For details about how to configure other HPA parameters, see HPA Policies.

  3. Return to the Scaling Policies page and check whether the HPA policy has been created.

    Figure 5 HPA policy created