组件三:
基于HPA的CPU/内存扩缩容
Kubernetes原生的Horizontal Pod Autoscaler(HPA)支持基于CPU和内存的自动扩缩容。
今日文章阅读福利:《运维入门大礼包》
扫码添加小助理,发送暗号“运维”,即可获取。
HPA配置示例
# hpa-cpu.yamlapiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata:name: llm-hpa-cpuspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: llm-inference-serviceminReplicas: 1maxReplicas: 10metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 70- type: Resourceresource:name: memorytarget:type: UtilizationaverageUtilization: 80
问题:CPU利用率≠推理负载
LLM推理的瓶颈通常在GPU,而非CPU。单纯依赖CPU利用率可能导致:
GPU已满载,但CPU仅30%,无法触发扩容;
或CPU因数据预处理高负载,但GPU空闲,导致误扩。
因此,必须引入自定义指标。
组件四:
基于KEDA的事件驱动扩缩容
KEDA是CNCF毕业项目,支持基于外部事件源(如Kafka、RabbitMQ、Prometheus指标)的自动扩缩容。
架构图
安装KEDA
helm repo add kedacore https://kedacore.github.io/chartshelm repo updatehelm install keda kedacore/keda --namespace keda --create-namespace
KEDA ScaledObject配置
# keda-scaledobject.yamlapiVersion: keda.sh/v1alpha1kind: ScaledObjectmetadata:name: llm-scaledobjectnamespace: defaultspec:scaleTargetRef:name: llm-inference-serviceminReplicaCount: 1maxReplicaCount: 20triggers:- type: prometheusmetadata:serverAddress: http://prometheus-server.default.svc.cluster.local:9090metricName: llm_request_countthreshold: "10" # 每秒请求数 > 10 时扩容query: |sum(rate(llm_request_count[2m])) by (job)- type: prometheusmetadata:serverAddress: http://prometheus-server.default.svc.cluster.local:9090metricName: llm_pending_requeststhreshold: "5" # 队列长度 > 5 时扩容query: |llm_pending_requests
Java代码:模拟请求队列
// src/main/java/com/ai/inference/service/InferenceQueue.javapackage com.ai.inference.service;import com.ai.inference.metrics.LLMMetrics;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;import java.util.concurrent.BlockingQueue;import java.util.concurrent.LinkedBlockingQueue;public class InferenceQueue {private final BlockingQueue<String> queue = new LinkedBlockingQueue<>(100);private final LLMMetrics llmMetrics;public InferenceQueue(LLMMetrics llmMetrics) {this.llmMetrics = llmMetrics;}public boolean submit(String prompt) {boolean offered = queue.offer(prompt);if (offered) {// 更新待处理请求数llmMetrics.recordPendingRequests(queue.size());}return offered;}(fixedDelay = 100)public void process() {String prompt = queue.poll();if (prompt != null) {// 调用推理服务simulateInference(prompt);llmMetrics.recordPendingRequests(queue.size());}}private void simulateInference(String prompt) {try {Thread.sleep(500 + (long)(Math.random() * 1000)); // 模拟耗时} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}
组件五:
GPU感知调度与资源优化
Kubernetes支持GPU资源调度,但需正确配置。
节点打标与容忍
# 手动打标(通常由 device plugin 自动完成)kubectl label nodes gpu-node-1 accelerator=nvidia-tesla-t4kubectl taint nodes gpu-node-1 accelerator=nvidia-tesla-t4:NoSchedule
推理服务Pod配置
# llm-deployment-gpu.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: llm-inference-servicespec:replicas: 1selector:matchLabels:app: llmtemplate:metadata:labels:app: llmspec:containers:- name: inferenceimage: your-llm-service:1.0ports:- containerPort: 8080resources:limits:nvidia.com/gpu: 1memory: "16Gi"cpu: "4"requests:nvidia.com/gpu: 1memory: "8Gi"cpu: "2"env:- name: MODEL_NAMEvalue: "llama-3-8b"tolerations:- key: acceleratoroperator: Equalvalue: nvidia-tesla-t4effect: NoSchedule
监控GPU使用
使用dcgm-exporter将GPU指标暴露给Prometheus:
helm install dcgm-exporter gpu-helm-charts/dcgm-exporter
查询GPU利用率:
DCGM_FI_DEV_GPU_UTIL{container="triton"}
高级策略:
预测性扩缩容与成本优化
基于时间的预测扩缩容
使用KEDA的cron触发器,在已知高峰前预热实例。
triggers:- type: cronmetadata:timezone: Asia/Shanghaistart: 0 8 * * 1-5 # 工作日 8:00end: 0 18 * * 1-5 # 工作日 18:00desiredReplicas: "5"
分层部署:CPU vs GPU实例
GPU实例:处理实时推理;
CPU实例:处理异步任务、批量推理、预热缓存。
// 根据负载类型路由public String routeInference(String prompt, boolean isRealTime) {if (isRealTime) {return gpuClient.infer(prompt);} else {return asyncQueue.submit(prompt);}}
成本监控
使用Kubecost监控GPU资源成本。
helm install kubecost kubecost/cost-analyzer --namespace kubecost --create-namespace
服务网格集成:
Istio+流量管理
在多版本推理服务(如A/B测试)场景下,Istio可实现精细化流量控制。
架构图
Istio配置
# virtual-service.yamlapiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:name: llm-routingspec:hosts:- llm.example.comhttp:- route:- destination:host: llm-inference-service-v1weight: 90- destination:host: llm-inference-service-v2weight: 10
总结:
构建智能的 LLM 推理平台
本文系统阐述了在云原生环境下为LLM推理服务配置弹性伸缩的完整方案:
1.基础架构:使用Triton+Spring Boot构建高性能推理服务;
2.指标采集:通过Micrometer+Prometheus暴露关键性能指标;
3.原生扩缩:HPA适用于CPU/内存负载;
4.事件驱动:KEDA实现基于自定义指标(如请求率、队列长度)的精准扩缩;
5.GPU调度:合理配置资源请求与容忍,确保GPU实例正确调度;
6.高级策略:结合预测性扩缩、分层部署与成本监控,实现极致优化。
弹性伸缩不仅是技术实现,更是一种资源治理哲学。
通过智能化的调度,我们让AI服务既能应对洪峰,又能静享低谷,真正实现“按需付费、随用随扩”的云原生愿景。

