# GPUカスタムメトリクスを使用したクラスタの自動スケーリング

KubernetesはPrometheusとの連携により、GPUメトリクスなどのカスタムメトリクスに基づく自動スケーリングをサポートします。本記事では、FPT Kubernetes Engineプラットフォーム上で動作するGPUベースのアプリケーション向けに自動スケーリングを設定する方法をご紹介します。

<figure><img src="/files/amJkCa7G0nhrK8D2Q0tS" alt=""><figcaption></figcaption></figure>

## 要件:

* GPUが接続されたKubernetesクラスター
* 実行状態にあるGPUベースのアプリケーション

## 手順

{% stepper %}
{% step %}

#### ステップ1: kube-prometheus-stack および prometheus-adapter パッケージをインストールする

#### FPT App Catalog サービスを使用する

* FPT App Catalog サービスを使用し、App Catalog を作成した後、\[クラスターに接続] を選択して GPU クラスターに接続します。
* App Catalogs メニューで、リポジトリとして fptcloud-catalogs を選択し、prometheus を検索して kube-prometheus-stack パッケージのインストールを選択します。パッケージをデプロイするためのリリース名とネームスペースを入力します。

<figure><img src="/files/0DOtvk01xBXJnonYtm20" alt=""><figcaption></figcaption></figure>

#### Helmチャートを使用する:

```
helm repo add xplat-fke 
https://registry.fke.fptcloud.com/chartrepo/xplat-fke
 && helm repo update
helm install --wait --generate-name \
-n prometheus --create-namespace \ xplat-fke/kube-prometheus-stack
prometheus_service=$(kubectl get svc -n prometheus -lapp=kube-prometheus-stack-prometheus -ojsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
helm install --wait --generate-name \
-n prometheus --create-namespace \ xplat-fke/prometheus-adapter \
--set prometheus.url=http://${prometheus_service}.prometheus.svc.cluster.local
```

* kube-prometheus-stack パッケージのデプロイ後、prometheus-adapter のデプロイを続行しますが、kube-prometheus-stack の正しい prometheus サービスを指すようパッケージの値を変更する必要があります。例えば、kube-prometheus-stack のネームスペースが prometheus に設定されている場合、入力が必要な値は以下の通りです：

```
<>.<>.svc.cluster.local
prometheus-kube-prometheus-prometheus.prometheus.svc.cluster.local
```

<figure><img src="/files/F3efYW6ydjgnaUNhY45T" alt=""><figcaption></figcaption></figure>

次に、2つのパッケージの状態を確認します

<figure><img src="/files/3F1vnhXEj0GFCgBJYiwc" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### ステップ2: GPUアプリケーションの水平ポッドオートスケーラーを設定する

水平ポッドオートスケーラー（HPA）は、構成で指定された条件を満たすようポッドを自動的にスケーリングします。前のセクションでは、prometheus-addapterを設定した後、GPUワークロードを監視するためにDCGMのカスタムメトリクスをエクスポートします。

HPAマニフェストファイルの例

```
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
 name: my-gpu-app
spec:
 maxReplicas: 3  # Update this accordingly
 minReplicas: 1
 scaleTargetRef:
   apiVersion: apps/v1beta1
   kind: Deployment
   name: my-gpu-app # Add label from Deployment we need to autoscale
 metrics:
 - type: Pods  # scale pod based on gpu
   pods:
     metric:
       name: DCGM_FI_PROF_GR_ENGINE_ACTIVE # Add the DCGM metric here accordingly
     target:
       type: AverageValue
       averageValue: 0.8
```

以下のリンクにあるNVIDIAのDCGMメトリクスに関するドキュメントを参照してください。<https://docs.nvidia.com/datacenter/dcgm/1.6/dcgm-api/group__dcgmFieldIdentifiers.html>

その後、新しく作成されたHPAを確認します：

`kubectl get hpa -A`
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ai-docs.fptcloud.com/ai-factory-guideline-jp/fpt-gpu-cloud/gpu-cluster/gpu-cluster-managed-k8s-with-gpu-virtual-machine/gaidochtoriaru/kurasutasukringu/gpukasutamumetorikusuwoshitakurasutanosukringu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
