GPUワーカーノードで水平ポッドのオートスケールを設定する

GPUワーカーノードで水平ポッドオートスケーリングを有効にするには、次の手順を確認してください。

なぜ水平ポッドのオートスケールなのか?
ワークロードが一定量以上のGPUを消費したり、消費しなかったりしたときにポッド数をスケールするように、水平ポッド自動スケーリングを設定したい場合があります。 GPUは高価なリソースであるため、ワークロードを長時間フル稼働させたくない場合があります。 その代わりに、クラスタ内で実行中のワークロードに基づいてポッドをスケールしたり、スケールダウンしたりすることができる。

前提条件

HPAを設定するには、以下のコンポーネントがクラスタにインストールされている必要があります。

  • NVIDIA Data Center GPU Manager (DCGM) エクスポーターは、 Kubernetes で GPU メトリクスを収集します。 DCGM エクスポーターは、 Grafana を使用して視覚化できる Prometheus の GPU メトリクスを公開します。
  • Prometheus と Prometheus アダプタを使用して、カスタム・メトリクスを生成します。
  1. NVIDIA GPU Operatorをインストールします。

  2. Prometheus をインストールする。

    helm install prom-stack prometheus-community/kube-prometheus-stack -f ~/ca-prom-val.yaml
    
    cat ~/ca-prom-val.yaml
    
    prometheus:
        prometheusSpec:
            additionalScrapeConfigs:
            - job_name: gpu-metrics
                scrape_interval: 1s
                metrics_path: /metrics
                scheme: http
                kubernetes_sd_configs:
                - role: endpoints
                    namespaces:
                        names:
                        - nvidia-gpu-operator
                relabel_configs:
                - source_labels: [__meta_kubernetes_endpoints_name]
                    action: drop
                    regex: .*-node-feature-discovery-master
                - source_labels: [__meta_kubernetes_pod_node_name]
                    action: replace
                    target_label: kubernetes_node
    
  3. Prometheus。

    oc get svc
    
  4. Prometheus アダプターを取り付けてください。

    helm upgrade --install prometheus-adapter prometheus-community/prometheus-adapter --set prometheus.url="http://prom-stack-kube-prometheus-prometheus.default.svc.cluster.local"
    

HPAの設定

HPA を使用する配置を作成するには、次の手順を実行します。

  1. デプロイメントを作成します。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
        name: cuda-test
        labels:
            app: cuda-test
    spec:
        selector:
            matchLabels:
                app: cuda-test
        template:
            metadata:
                labels:
                    app: cuda-test
            spec:
                containers:
                - name: cuda-test-main
                    image: "registry.k8s.io/cuda-vector-add:v0.1"
                    command: ["bash", "-c", "for (( c=1; c<=5000; c++ )); do ./vectorAdd; done"]
                    resources:
                        limits:
                            nvidia.com/gpu: 1
    
  2. HorizontalPodAutoscaler リソースを作成します。

    kind: HorizontalPodAutoscaler
    apiVersion: autoscaling/v2
    metadata:
        name: cuda-hpa
        namespace: default
    spec:
        scaleTargetRef:
            apiVersion: apps/v1
            kind: Deployment
            name: cuda-test
        minReplicas: 1
        maxReplicas: 3
        metrics:
            - type: Pods
                pods:
                    metric:
                        name: DCGM_FI_DEV_GPU_UTIL     #the metric you want to use for autoscaling
                    target:
                        type: AverageValue
                        averageValue: '5'
    
  3. 結果を確認するには、以下のコマンドを実行してください。

    oc get pods | grep cuda
    
    cuda-test-d987464bf-brd48                                1/1     Running   0          4m19s
    cuda-test-d987464bf-gsx82                                0/1     Pending   0          4m19s
    cuda-test-d987464bf-zstzs                                1/1     Running   0          7m35s
    

    レプリカは1台で、ワークロードリソースの増加に伴い3台にスケールアップされた。

    Min replicas:       1
    Max replicas:       3
    Deployment pods:    3 current / 3 desired
    Events:
    Type    Reason             Age   From                       Message
    ----    ------             ----  ----                       -------
    Normal  SuccessfulRescale  50s   horizontal-pod-autoscaler  New size: 3; reason: pods metric DCGM_FI_DEV_GPU_UTIL above target