KServe LLMInferenceService 单节点 GPU 部署实战:默认调度、无调度器与 Prefill-Decode 分离

原创2026-10-11 11:15:421,616 阅读
文章标签:模型推理服务人工智能大模型云原生后端微服务本地部署

KServe LLMInferenceService 单节点 GPU 部署实战:默认调度、无调度器与 Prefill-Decode 分离

本文以 KServe 仓库中的 single-node-gpu 示例目录 为骨架,系统讲解如何在单个 GPU 节点上以 Qwen2.5-7B-Instruct 为示例模型,部署三种不同形态的 LLMInferenceService:带默认调度器、不带调度器(直接走 Kubernetes Service 负载均衡)、以及基于 RoCE/RDMA 的 Prefill-Decode 分离部署。读完本文,你将掌握 LLMInferenceService 的核心字段语义、调度器(Endpoint Picker)的路由原理、KV Cache 跨节点传输的配置方法,以及对应的监控与排障手段。

一、背景与前置条件

LLMInferenceService(资源短名 llmisvc)是 KServe 针对大语言模型推理场景提供的专用 CRD(serving.kserve.io/v1alpha2),其 Spec 定义位于 pkg/apis/serving/v1alpha2/llm_inference_service_types.go。相比通用 InferenceService,它原生内置了 router(路由)、scheduler(调度器/Endpoint Picker)与 prefill(预填充工作负载)等大模型专属概念。

部署前需要满足以下前提:

  • 拥有 GPU 节点的 Kubernetes 集群(示例以 NVIDIA GPU 为例,资源名为 nvidia.com/gpu);
  • 模型权重可通过 HuggingFace(hf:// URI)或 PVC 访问;
  • 使用 Prefill-Decode 分离示例时,需要 RoCE 网络配置与 RDMA 支持(资源名为 rdma/roce_gdr)。

三个示例统一使用 Qwen2.5-7B-Instruct 模型,每个副本的资源规格为:1 块 NVIDIA GPU、limit 4 核 CPU / 32Gi 内存、request 2 核 CPU / 16Gi 内存。Prefill-Decode 示例额外为每个副本申请 1 个 rdma/roce_gdr 资源。

二、三种部署形态总览

对比维度 无调度器(No Scheduler) 默认调度器(Default Scheduler) Prefill-Decode 分离
路由逻辑 Kubernetes Service 基础负载均衡 高级(PD 分离)
Prefix Cache 路由 ❌ ✅ ✅
Prefill-Decode 分离 ❌ ❌ ✅
KV Cache 传输 ❌ ❌ ✅
资源开销 最低 低 中等
适用场景 简单/开发 生产(基础) 生产(高级)

表格中的对比可以结合调度器配置源码进一步理解:KServe 为调度器内置了两套默认的 EndpointPickerConfig,分别位于 config/llmisvcconfig/config-llm-scheduler-eppconfig-default.yaml(单 profile,默认调度)与 config/llmisvcconfig/config-llm-scheduler-eppconfig-default-pd.yaml(prefill/decode 双 profile,PD 分离)。无调度器形态则完全不部署这一组件。

三、示例一:默认调度器部署

配置文件为 llm-inference-service-qwen2-7b-gpu.yaml,完整内容如下:

apiVersion: serving.kserve.io/v1alpha2
kind: LLMInferenceService
metadata:
  name: qwen2-7b-instruct-single
spec:
  model:
    uri: hf://Qwen/Qwen2.5-7B-Instruct
    name: Qwen/Qwen2.5-7B-Instruct
  # Three replicas for load balancing
  replicas: 3
  router:
    scheduler: { }  # Default scheduler with default load balancing
    route: { }
    gateway: { }
  template:
    containers:
      - name: main
        resources:
          limits:
            cpu: '4'
            memory: 32Gi
            nvidia.com/gpu: "1"
          requests:
            cpu: '2'
            memory: 16Gi
            nvidia.com/gpu: "1"
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
            scheme: HTTPS
          initialDelaySeconds: 120
          periodSeconds: 30
          timeoutSeconds: 30
          failureThreshold: 5

字段语义与源码对应关系:

  • spec.model.uri:模型来源,hf:// 表示 HuggingFace。源码 llm_inference_service_types.go 中 LLMModelSpec 规定该 URI 由 storage-initializer initContainer 在 main 容器启动前下载模型产物;
  • spec.model.name:请求中 model 参数使用的模型名。若省略,将默认取 metadata.name;
  • spec.replicas:副本数,对应 WorkloadSpec.Replicas(类型为 *int32,最小值 0)。此处 3 副本用于负载均衡;
  • spec.router.scheduler: {}:声明使用默认调度器。源码中 SchedulerSpec 的注释明确说明:当 scheduler 字段非空时,控制器会创建一个 InferenceModel 资源以对接 Gateway 的调度器(Endpoint Picker);而 SchedulerSpec 上方也强调“调度器仅在存在多个推理副本时有效”,这正是示例保留 3 副本的原因;
  • spec.router.route: {} 与 spec.router.gateway: {}:路由与网关均使用控制器托管的默认资源({} 即“由控制器创建并管理”)。根据 RouterSpec 定义,route、gateway、ingress 三者互斥,这里选用的是 Gateway API(HTTPRoute + Gateway)方案;
  • spec.template:Pod 模板。类型为 corev1.PodSpec,可直接沿用 Kubernetes 原生 Pod 字段,包括这里的 livenessProbe(HTTPS 探测 /health,初始延迟 120s)。

部署命令:

kubectl apply -f llm-inference-service-qwen2-7b-gpu.yaml

适用场景:开发与测试;跨副本的简单负载均衡;不需要特殊路由逻辑的场合。

此形态下,调度器组件对应 config-llm-scheduler.yaml 中定义的 Endpoint Picker 容器(镜像 ghcr.io/llm-d/llm-d-router-endpoint-picker,gRPC 端口 9002,metrics 端口 9090),并采用默认 EPP 配置 approx-prefix-cache-producer + inflight-load-producer + prefix-cache-affinity-filter + token-load-scorer 的插件链,实现基本的 prefix-cache 亲和与 token 负载评分。

四、示例二:无调度器部署

配置文件为 llm-inference-service-qwen2-7b-gpu-no-scheduler.yaml:

apiVersion: serving.kserve.io/v1alpha2
kind: LLMInferenceService
metadata:
  name: qwen2-7b-instruct-single-no-scheduler
spec:
  model:
    uri: hf://Qwen/Qwen2.5-7B-Instruct
    name: Qwen/Qwen2.5-7B-Instruct
  # Three replicas for load balancing
  replicas: 3
  router:
    # No scheduler - routing handled by Kubernetes service
    route: { }
  template:
    containers:
      - name: main
        resources:
          limits:
            cpu: '4'
            memory: 32Gi
            nvidia.com/gpu: "1"
          requests:
            cpu: '2'
            memory: 16Gi
            nvidia.com/gpu: "1"
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
            scheme: HTTPS
          initialDelaySeconds: 120
          periodSeconds: 30
          timeoutSeconds: 30
          failureThreshold: 5

与示例一的关键差异仅在于 router 段:这里移除了 scheduler 与 gateway 字段,仅保留 route: {}。依据 SchedulerSpec 的定义,scheduler 字段不存在(为空)时控制器不会创建任何 Endpoint Picker 相关资源,流量直接由 Kubernetes Service(kube-proxy)分发到各副本。

关键差异总结:

  • 不部署调度器 Pod,资源开销更低;
  • 路由由 Kubernetes Service 完成,请求简单轮询分发;
  • 不支持高级路由能力:无 prefix-cache 亲和、无 prefill-decode 分离、无 KV Cache 传输。

适用场景:追求最小开销的部署;不需要高级调度特性的场景;直接依赖 Kubernetes Service 负载均衡即可满足需求的场合。

部署命令:

kubectl apply -f llm-inference-service-qwen2-7b-gpu-no-scheduler.yaml

五、示例三:Prefill-Decode 分离部署(RDMA / KV Cache 传输)

配置文件为 llm-inference-service-pd-qwen2-7b-gpu.yaml,完整内容如下:

apiVersion: serving.kserve.io/v1alpha2
kind: LLMInferenceService
metadata:
  name: qwen2-7b-instruct-pd
  annotations:
    # RoCE network required for KV cache transfer via RDMA
    k8s.v1.cni.cncf.io/networks: roce-p2
spec:
  # Main/decode pool replica count
  replicas: 1
  model:
    uri: hf://Qwen/Qwen2.5-7B-Instruct
    name: Qwen/Qwen2.5-7B-Instruct
  router:
    route: { }
    gateway: { }
    scheduler: { }
  template:
    # This affinity rule ensures that the KV transfer happens over the RDMA network because if the pods are placed
    # on the same node it will go over NVLink.
    #affinity:
    #  podAntiAffinity:
    #    preferredDuringSchedulingIgnoredDuringExecution:
    #    - weight: 100
    #      podAffinityTerm:
    #        labelSelector:
    #          matchExpressions:
    #          - key: app.kubernetes.io/component
    #            operator: In
    #            values:
    #            - llminferenceservice-workload-prefill
    #        topologyKey: kubernetes.io/hostname
    containers:
      - name: main
        env:
          # Enable RDMA for KV cache transfer
          - name: KSERVE_INFER_ROCE
            value: "true"
          # Pod IP for KV transfer side channel
          - name: VLLM_NIXL_SIDE_CHANNEL_HOST
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          # Enable KV cache transfer via NixlConnector (RDMA-based)
          - name: VLLM_ADDITIONAL_ARGS
            value: "--kv_transfer_config '{\"kv_connector\":\"NixlConnector\",\"kv_role\":\"kv_consumer\"}'"
          # UCX configuration for RDMA transport
          - name: UCX_PROTO_INFO
            value: "y"
          - name: UCX_TLS
            value: "rc,sm,self,cuda_copy,cuda_ipc"
        resources:
          limits:
            cpu: '4'
            memory: 32Gi
            nvidia.com/gpu: "1"
            rdma/roce_gdr: 1
          requests:
            cpu: '2'
            memory: 16Gi
            nvidia.com/gpu: "1"
            rdma/roce_gdr: 1
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
            scheme: HTTPS
          initialDelaySeconds: 120
          periodSeconds: 30
          timeoutSeconds: 30
          failureThreshold: 5
  prefill:
    # Prefill pool replica count (higher for concurrent prefill requests)
    replicas: 2
    template:
      # This affinity rule ensures that the KV transfer happens over the RDMA network because if the pods are placed
      # on the same node it will go over NVLink
      #affinity:
      #  podAntiAffinity:
      #    preferredDuringSchedulingIgnoredDuringExecution:
      #    - weight: 100
      #      podAffinityTerm:
      #        labelSelector:
      #          matchExpressions:
      #          - key: app.kubernetes.io/component
      #            operator: In
      #            values:
      #            - llminferenceservice-workload
      #        topologyKey: kubernetes.io/hostname
      containers:
        - name: main
          env:
            - name: KSERVE_INFER_ROCE
              value: "true"
            - name: VLLM_NIXL_SIDE_CHANNEL_HOST
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: VLLM_ADDITIONAL_ARGS
              value: "--kv_transfer_config '{\"kv_connector\":\"NixlConnector\",\"kv_role\":\"kv_producer\"}'"
            - name: UCX_PROTO_INFO
              value: "y"
            - name: UCX_TLS
              value: "rc,sm,self,cuda_copy,cuda_ipc"
          resources:
            limits:
              cpu: '4'
              memory: 32Gi
              nvidia.com/gpu: "1"
              rdma/roce_gdr: 1
            requests:
              cpu: '2'
              memory: 16Gi
              nvidia.com/gpu: "1"
              rdma/roce_gdr: 1
          livenessProbe:
            httpGet:
              path: /health
              port: 8000
              scheme: HTTPS
            initialDelaySeconds: 120
            periodSeconds: 30
            timeoutSeconds: 30
            failureThreshold: 5

5.1 核心配置项

  • 顶层 replicas: 1(Main/Decode 池)与 prefill.replicas: 2(Prefill 池):源码 LLMInferenceServiceSpec 对 prefill 字段的注释明确指出:当 prefill 段被包含时,控制器会额外创建一个用于 prompt 处理(prefill)的独立 Deployment,与主(decode)Deployment 并存,从而允许 prefill 与 decode 独立扩缩容、独立分配硬件。这里 prefill 池 2 副本是为了支撑更高的并发 prefill 吞吐;
  • k8s.v1.cni.cncf.io/networks: roce-p2 注解:将 Pod 附加到 RoCE 网络(Multus CNI),为 RDMA 传输提供网络平面;
  • KSERVE_INFER_ROCE=true:启用 RDMA 进行 KV Cache 传输;
  • VLLM_NIXL_SIDE_CHANNEL_HOST:通过 status.podIP 注入当前 Pod IP,作为 KV 传输的带外(side channel)通道地址;
  • VLLM_ADDITIONAL_ARGS:向 vLLM 传递 --kv_transfer_config,指定 kv_connector: NixlConnector(RDMA 传输后端);decode 池角色为 kv_consumer,prefill 池角色为 kv_producer;
  • UCX_TLS: rc,sm,self,cuda_copy,cuda_ipc:显式配置 UCX 传输层,选取 rc(RoCE/InfiniBand 可靠连接)、sm(共享内存)、self、cuda_copy、cuda_ipc 等协议以获取最优 RDMA 性能;
  • rdma/roce_gdr 资源:每个副本在 limits 与 requests 中均声明 1 个 RDMA 设备(GPU Direct RDMA)。

5.2 Pod 反亲和(可选,默认注释)

YAML 中被注释掉的 affinity.podAntiAffinity 规则,其作用是将 prefill 与 decode Pod 放置到不同节点(topologyKey: kubernetes.io/hostname):若两者位于同一节点,KV Cache 传输会走 NVLink 而不会走 RDMA;启用该规则即可强制 KV 传输走 RDMA 网络。模板中注释部分的 preferredDuringSchedulingIgnoredDuringExecution(权重 100)为软约束,生产环境可按需取消注释并评估节点拓扑。

5.3 部署步骤

# 确保 RoCE 网络已配置(network-roce-p2.yaml 需按集群环境自行准备,可参考同仓库
# docs/samples/llmisvc/dp-ep/deepseek-r1-gpu-rdma-roce/ 下的网络配置示例)
kubectl apply -f network-roce-p2.yaml

# 部署服务
kubectl apply -f llm-inference-service-pd-qwen2-7b-gpu.yaml

5.4 调度器如何路由请求

当 scheduler: {} 存在时,控制器使用 config-llm-scheduler-eppconfig-default-pd.yaml 中定义的 PD 版 EndpointPickerConfig,其插件链与两个调度 profile 完整展示了路由决策过程:

plugins:
  - type: always-disagg-pd-decider
  - type: disagg-profile-handler
    parameters:
      deciders:
        prefill: always-disagg-pd-decider
  - type: prefill-filter
  - type: decode-filter
  - type: approx-prefix-cache-producer
  - type: inflight-load-producer
  - type: prefix-cache-affinity-filter
  - type: token-load-scorer
  - type: active-request-scorer
  - type: max-score-picker
schedulingProfiles:
  - name: prefill
    plugins:
      - pluginRef: prefill-filter
      - pluginRef: prefix-cache-affinity-filter
      - pluginRef: token-load-scorer
      - pluginRef: max-score-picker
  - name: decode
    plugins:
      - pluginRef: decode-filter
      - pluginRef: active-request-scorer
      - pluginRef: max-score-picker

结合该配置,请求路由的三步决策为:

  1. 新请求(无 KV Cache)→ Prefill 池:由 always-disagg-pd-decider 判定请求必然走分离路径,prefill-filter 将请求限定到 prefill 端点;
  2. 续跑请求(已有 KV Cache)→ Decode 池:decode-filter 将携带 KV Cache 的续跑请求限定到 decode 端点,active-request-scorer 依据在途请求负载评分;
  3. KV Cache 传输:prefill 完成后,KV Cache 块由 prefill Pod(kv_producer)经 RDMA 传输至 decode Pod(kv_consumer)。

配置中的 maxPrefixTokensToMatch(approx-prefix-cache-producer,默认 131072 即 128K tokens)与 peakPrefillThroughput(prefix-cache-affinity-filter,默认 15928 tokens/sec)等默认值,均可通过 LLMInferenceService 的 spec.router.scheduler.config 覆盖。

5.5 Threshold 参数

文档强调 threshold: 0 这一语义:threshold: 0 表示始终启用 prefill-decode 分离(所有请求都走 prefill→decode 流程);threshold: N 表示仅在估算 token 数大于 N 时才分离。在实际示例中,由于 PD 版本使用 always-disagg-pd-decider 插件(而非阈值判断),因此等价于“永远分离”的行为。

六、Prefill-Decode 分离原理

6.1 LLM 推理的两个阶段

  1. Prefill(预填充):处理完整输入 prompt 以生成 KV Cache。
    • 计算密集型;
    • 可跨 token 并行;
    • 每个请求仅发生一次。
  2. Decode(解码):基于 KV Cache 逐个生成输出 token。
    • 内存密集型;
    • 顺序执行(无法跨 token 并行);
    • 每个请求发生多次(每个输出 token 一次)。

6.2 为什么分离

  • 资源优化:prefill 与 decode Pod 可配置不同的资源规格(本示例中二者硬件规格相同,但架构上完全支持差异化配置);
  • 更高吞吐:专用 prefill Pod 可并发处理多个 prefill 请求(示例 2 副本);
  • 更低延迟:decode Pod 不与 prefill 操作争抢资源;
  • KV Cache 传输:prefill 生成 KV Cache 后通过 RDMA 传输给 decode Pod。

6.3 组件标签与运维识别

KServe 在 pkg/constants/constants.go 中定义了完整的组件标签常量,用于区分不同角色(监控、排障时直接按 label 选择资源):

LLMComponentRouter                = "llminferenceservice-router"
LLMComponentRouterScheduler       = "llminferenceservice-router-scheduler"
LLMComponentWorkload              = "llminferenceservice-workload"
LLMComponentWorkloadPrefill       = "llminferenceservice-workload-prefill"

以及角色标签值:

LLMDRoleLabelKey = "llm-d.ai/role"
LLMDRoleDecode   = "decode"
LLMDRolePrefill  = "prefill"
LLMDRoleBoth     = "both"

这解释了监控命令中 app.kubernetes.io/component=llminferenceservice-workload(main/decode)与 ...-workload-prefill(prefill)两个 label 的来源。

七、监控与功能验证

7.1 查看服务状态

# 列出所有 LLMInferenceService
kubectl get llminferenceservice -owide

# 查看推理 Pod 状态(main/decode 池)
kubectl get pods -l app.kubernetes.io/component=llminferenceservice-workload

# Prefill-Decode 示例:再查看 prefill 池
kubectl get pods -l app.kubernetes.io/component=llminferenceservice-workload-prefill

7.2 查看日志

# 调度器日志(使用调度器的示例)
kubectl logs -l app.kubernetes.io/component=llminferenceservice-scheduler -f

# 推理 Pod(main/decode)日志
kubectl logs <pod-name> -c main

# Prefill Pod 日志
kubectl logs <prefill-pod-name> -c main

7.3 发送推理请求验证

# 获取服务 URL
kubectl get route -l serving.kserve.io/inferenceservice=<service-name>

# 发送一次补全请求
curl -k https://<route-url>/v1/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen/Qwen2.5-7B-Instruct",
    "prompt": "What is Kubernetes?",
    "max_tokens": 100
  }'

八、如何选择合适的示例

  • 生产(基础需求):使用示例一(默认调度器),兼顾基础的 prefix-cache 亲和与负载均衡;
  • 开发/测试:使用示例二(无调度器),资源开销最低、行为最简单;
  • 生产(高级需求,具备 RDMA/RoCE 网络):使用示例三(prefill-decode),适合高并发、prefill 为瓶颈、或对 KV Cache 传输有要求的场景。

九、常见故障排查

9.1 Pod 无法启动

# 检查节点 GPU 可用性
kubectl describe node <node-name> | grep nvidia.com/gpu
  • 检查资源配额(quota)是否足够;
  • 查看 Pod 事件:kubectl describe pod <pod-name>。

9.2 RDMA 问题(Prefill-Decode 示例)

# 确认 RoCE 网络附加定义存在
kubectl get network-attachment-definitions

# 检查节点上 RDMA 设备可用性
kubectl describe node <node-name> | grep rdma/roce_gdr
  • 在 Pod 输出中查看 UCX 相关日志。

9.3 KV Cache 传输失败

  • 确认 VLLM_NIXL_SIDE_CHANNEL_HOST 已正确设置为 Pod IP(YAML 中通过 status.podIP 注入);
  • 检查 prefill 与 decode Pod 之间的 RDMA 连通性;
  • 确认 KSERVE_INFER_ROCE=true 已设置;
  • 查看调度器日志中的路由决策,确认请求是否被正确导向 prefill/decode 池。

9.4 性能问题

  • 使用 NVIDIA DCGM 等工具监控 GPU 利用率;
  • 检查请求在各副本间的分布是否均衡;
  • 查看调度器 metrics(如默认配置中的 9090 端口)评估路由有效性;
  • 必要时增加副本数以提升吞吐。

十、延伸阅读

登录后查看全文
kserve