在 GPU 工作节点上设置水平 pod 自动扩展

查看以下步骤,在 GPU 工作节点上启用水平 pod 自动扩展。

为什么要进行水平 pod 自动扩展?
您可能需要配置水平 pod 自动伸缩功能,以便在工作负载消耗的 GPU 超过或低于一定数量时缩放 pod 的数量。 由于 GPU 是一种昂贵的资源,因此您可能不希望工作负载长时间以最大容量运行。 相反,您可以根据集群中运行的工作负载来扩展 pod 或向上向下扩展 pod。

先决条件

要配置 HPA,必须在群集上安装以下组件。

  • NVIDIA 数据中心 GPU 管理器 (DCGM) 输出程序可在 Kubernetes 中收集 GPU 指标。 DCGM 输出程序公开了 Prometheus 的 GPU 指标,可使用 Grafana 对其进行可视化。
  • Prometheus 和 Prometheus 适配器生成自定义指标。
  1. 安装 NVIDIA GPU 操作器

  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