在 GPU 工作節點上設定水平 Pod 自動擴充

檢閱下列步驟,在 GPU 工作節點上啟用水平 Pod 自動調整。

為什麼要進行水平 Pod 自動調整?
您可能想要設定水平 Pod 自動調整,以便在工作負載消耗的 GPU 多於或少於某個數量時,調整 Pod 的數量。 由於 GPU 是昂貴的資源,您可能不希望工作負載長時間以最大容量執行。 取而代之的是,您可以根據群集中正在運行的工作負載來擴充 Pod 或向上向下擴充 Pod。

必要條件

若要設定 HPA,您的群集上必須安裝下列元件。

  • NVIDIA Data Center GPU Manager (DCGM) 匯出程式可在 Kubernetes 中收集 GPU 指標。 DCGM 匯出器會揭露 Prometheus 的 GPU 指標,可使用 Grafana 將其視覺化。
  • 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