NVIDIA NIM 微服務是可部署在 Kubernetes 上的模型推理容器。在生產環境中,了解這些微服務的計算和內存配置對于制定成功的自動擴展計劃至關重要。
在本文中,我們將介紹如何通過適用于 LLMs 的 NVIDIA NIM 模型設置和使用 Kubernetes 橫向 Pod 自動擴展(HPA),以便根據特定的自定義指標自動擴展和縮小微服務。
預備知識?
要遵循本教程,您需要以下預備知識列表:
- An NVIDIA AI Enterprise license
- 適用于 LLMs 的 NVIDIA NIM 可根據 NVIDIA AI Enterprise 許可證自行托管。在集群中為 LLMs 部署 NIM 需要生成 NGC API 密鑰,以便 Kubernetes 集群可以下載容器鏡像。
- Kubernetes 集群版本 1.29 或更高版本 (我們使用的是 DGX Cloud Clusters )
- 管理員對 Kubernetes 集群的訪問權限
- Kubernetes CLI 工具 kubectl 已安裝
- HELM CLI 已安裝
設置 Kubernetes 集群
本教程中的第一步是使用適當的組件設置 Kubernetes 集群,以啟用指標丟棄和 Kubernetes HPA 服務的可用性。這需要以下組件:
- Kubernetes 指標服務器
- Prometheus
- Prometheus 網卡
- Grafana
Kubernetes 指標服務器?
Metrics Server 負責從 Kubelets 中抓取資源指標,并通過 Metrics API 在 Kubernetes API Server 中公開這些指標。 Horizontal Pod Autoscaler 和 kubectl top 命令均可使用此命令。
要安裝 Kubernetes Metric Server 服務器,請使用 Helm 。
helm repo add metrics - server https: / / kubernetes - sigs.github.io / metrics - server / helm upgrade - - install metrics - server metrics - server / metrics - server |
Prometheus 和 Grafana?
Prometheus 和 Grafana 是用于從 Pod 中抓取指標和創建控制面板的知名工具。要安裝 Prometheus 和 Grafana,請使用包含許多不同組件的 kube-prometheus-stack Helm 圖表。
helm repo add prometheus - community https: / / prometheus - community.github.io / helm - charts helm repo update helm install [RELEASE_NAME] prometheus - community / kube - prometheus - stack |
Prometheus 網卡通過 Metrics API 在 Kubernetes apiserver 中公開 Prometheus 中截取的指標。這使 HPA 能夠使用 Pod 中的自定義指標來制定擴展策略。
要在與 Prometheus 和 Grafana 相同的命名空間中安裝 Prometheus 適配器,請使用以下 Helm 命令:
helm repo add prometheus - community https: / / prometheus - community.github.io / helm - charts helm repo update helm install <name> prometheus - community / prometheus - adapter - n <namespace> |
確保 Prometheus 適配器指向正確的 Prometheus 服務端點。在這種情況下,我必須編輯部署并更正 URL。
kubectl edit deployment prom - adapter - prometheus - adapter - n prometheus spec: affinity: {} containers: - args: - / adapter - - - secure - port = 6443 - - - cert - dir = / tmp / cert - - - prometheus - url = http: / / prometheus - prometheus.prometheus.svc: 9090 - - - metrics - relist - interval = 1m - - - v = 4 - - - config = / etc / adapter / config.yaml image: registry.k8s.io / prometheus - adapter / prometheus - adapter:v0. 12.0 |
如果一切設置正確,您應使用以下命令查看 Prometheus 中的指標:
kubectl get - - raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/<namespace>/pods/*/gpu_cache_usage_perc?selector=app%3Dmeta-llama3-8b" { "kind" : "MetricValueList" , "apiVersion" : "custom.metrics.k8s.io/v1beta1" , "metadata" :{}, "items" :[{ "describedObject" :{ "kind" : "Pod" , "namespace" : "<namespace>" , "name" : "meta-llama3-70b-5db5f7dd89-tvcwl" , "apiVersion" : "/v1" }, "metricName" : "gpu_cache_usage_perc" , "timestamp" : "2025-01-02T20:13:15Z" , "value" : "1m" , "selector" :null},{ "describedObject" :{ "kind" : "Pod" , "namespace" : "<namespace>" , "name" : "meta-llama3-8b-5c6ddbbfb5-dp2mv" , "apiVersion" : "/v1" }, "metricName" : "gpu_cache_usage_perc" , "timestamp" : "2025-01-02T20:13:15Z" , "value" : "14m" , "selector" :null}]} |
部署 NIM 微服務?
在本教程中,您將 LLM 的 NIM 用作進行擴展的微服務,特別是使用模型 meta/llama-3.1-8b-instruct 。部署 NIM 微服務有多個選項:
部署完成后,您應留意 NIM for LLMs 微服務的服務名稱和命名空間,因為這將在許多命令中使用。
適用于 LLM 的 NIM 已經公開了 Prometheus 端點 ,其中包含許多有趣的指標。要查看端點,請使用以下命令:
kubectl - n <namespace> port - forward svc / <service - name> 8080 |
在瀏覽器中,轉到 localhost:8080/metrics 并查找名為 gpu_cache_usage_perc
的特定指標。在本文中,您將此指標用作自動擴展的基礎。此指標顯示 KV 緩存的百分比利用率,并由 vLLM 堆棧 報告。
您將使用適用于 LLMs 的 NIM Grafana 控制面板來觀察這些指標。下載 JSON 控制面板 并將其上傳到您的 Grafana 實例。如需登錄控制面板,請參閱 Grafana 訪問說明。
加載適用于 LLMs 的 NIM 控制面板后,您應該會看到與圖 1 相似的控制面板。(我為該控制面板部署了 70b 和 8b,因此 KV 緩存數量翻倍。)

現在,您已經部署了可觀測性堆棧和微服務,您可以開始在 Grafana 控制面板中生成流量和觀察指標。生成流量的工具是 genai-perf 。
要從集群上的 Pod 運行此工具,請按照以下步驟操作,并確保將其安裝在與 NIM for LLMs 微服務相同的命名空間中。
使用 NVIDIA Triton 創建 Pod:
kubectl run <pod - name> - - image = nvcr.io / nvidia / tritonserver: 24.10 - py3 - sdk - n <namespace> - - command - - sleep 100000 |
登錄 Pod,現在您可以運行 genai-perf CLI:
kubectl exec - - stdin - - tty <pod - name> - - / bin / bash genai - perf - - help |
要將流量發送到模型 meta/llama-3.1-8b-instruct,需要 genai-perf 從 Hugging Face 下載相應的標記器。從 Hugging Face 獲取 API 密鑰并登錄。
pip install - - upgrade huggingface_hub[cli] export HF_TOKEN = <hf - token> huggingface - cli login - - token $HF_TOKEN |
設置正確的環境變量并生成流量。有關不同參數的更多信息, 請參閱 genai-perf 文檔。 模型名稱和服務名稱必須準確并反映您的設置。
export INPUT_SEQUENCE_LENGTH = 200 export INPUT_SEQUENCE_STD = 10 export OUTPUT_SEQUENCE_LENGTH = 200 export CONCURRENCY = 10 export MODEL = meta / llama - 3.1 - 8b - instruct genai - perf profile \ - m $MODEL \ - - endpoint - type chat \ - - service - kind openai \ - - streaming - u meta - llama3 - 8b : 8080 \ - - synthetic - input - tokens - mean $INPUT_SEQUENCE_LENGTH \ - - synthetic - input - tokens - stddev $INPUT_SEQUENCE_STD \ - - concurrency $CONCURRENCY \ - - output - tokens - mean $OUTPUT_SEQUENCE_LENGTH \ - - extra - inputs max_tokens:$OUTPUT_SEQUENCE_LENGTH \ - - extra - inputs min_tokens:$OUTPUT_SEQUENCE_LENGTH \ - - extra - inputs ignore_eos:true \ - - tokenizer meta - llama / Meta - Llama - 3 - 8B - Instruct - - - v - - max - threads = 256 |
在本文中,我創建了通過改變并發數量 (100、200、300 和 400) 來運行的多次流量生成。在 Grafana 控制面板中,您可以看到 KV 緩存利用率百分比 (圖 2)。每次并發試驗時,KV 緩存百分比利用率都在增加,從 100 個并發時的 9.40%一直到 400 個并發時的 40.9%。您還可以更改其他相關參數 (例如輸入和輸出序列長度),并觀察其對 KV 緩存利用率的影響。

現在,您已觀察到并發對 KV 緩存利用率的影響,可以創建 HPA 資源。根據 gpu_cache_usage_perc
指標創建要擴展的 HPA 資源:
apiVersion: autoscaling / v2 kind: HorizontalPodAutoscaler metadata: name: gpu - hpa - cache spec: scaleTargetRef: apiVersion: apps / v1 kind: Deployment name: meta - llama3 - 8b minReplicas: 1 maxReplicas: 10 metrics: - type : Pods pods: metric: name: gpu_cache_usage_perc target: type : AverageValue averageValue: 100m kubectl create - f hpa - gpu - cache.yaml - n <namespace> kubectl get hpa - n <namepsace> - w |
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE gpu-hpa-cache Deployment/meta-llama3-8b 9m/100m 1 10 1 3m37s |
在不同并發度 (10、100、200) 下運行 genai-perf,并觀察 HPA 指標增長情況:
kubectl get hpa - n <namepsace> - w |
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE gpu-hpa-cache Deployment/meta-llama3-8b 9m/100m 1 10 1 3m37s gpu-hpa-cache Deployment/meta-llama3-8b 8m/100m 1 10 1 4m16s gpu-hpa-cache Deployment/meta-llama3-8b 1m/100m 1 10 1 4m46s gpu-hpa-cache Deployment/meta-llama3-8b 33m/100m 1 10 1 5m16s gpu-hpa-cache Deployment/meta-llama3-8b 56m/100m 1 10 1 5m46s gpu-hpa-cache Deployment/meta-llama3-8b 39m/100m 1 10 1 6m16s gpu-hpa-cache Deployment/meta-llama3-8b 208m/100m 1 10 1 6m46s gpu-hpa-cache Deployment/meta-llama3-8b 208m/100m 1 10 3 7m1s gpu-hpa-cache Deployment/meta-llama3-8b 293m/100m 1 10 3 7m16s gpu-hpa-cache Deployment/meta-llama3-8b 7m/100m 1 10 3 7m46s |
檢查 pod 的數量,您應該會看到 autoscaling 添加了兩個新的 pod:
kubectl get pods - n <namespace> |
NAME READY STATUS RESTARTS AGE meta-llama3-8b-5c6ddbbfb5-85p6c 1/1 Running 0 25s meta-llama3-8b-5c6ddbbfb5-dp2mv 1/1 Running 0 146m meta-llama3-8b-5c6ddbbfb5-sf85v 1/1 Running 0 26s |
HPA 也會縮小。縮小之前的等待時間由 --horizontal-pod-autoscaler-downscale-stabilization
標志決定,默認為 5 分鐘。這意味著規模縮小會逐漸發生,從而消除指標值快速波動的影響。等待 5 分鐘,檢查縮小。
kubectl get pods - n <namespace> |
NAME READY STATUS RESTARTS AGE meta-llama3-8b-5c6ddbbfb5-dp2mv 1/1 Running 0 154m |
結束語?
在本文中,我介紹了如何設置 Kubernetes 集群以根據自定義指標進行擴展,并展示了如何根據 KV 緩存利用率參數擴展 LLM 的 NIM。
在此主題中,有許多高級領域需要進一步探索。例如,還可以考慮使用許多其他指標進行擴展,例如請求延遲、請求吞吐量和 GPU 計算利用率。您可以 在一個 HPA 資源中基于多個指標進行擴展 ,并進行相應擴展。
另一個值得關注的領域是能夠使用 Prometheus 查詢語言 (PromQL) 創建新指標,并將其添加 到 Prometheus 適配器的 configmap 中,以便 HPA 進行擴展。
?