Seldon Core V2 本地实验(Local Experiments)实战:模型/管道流量分割、镜像与粘性会话完整指南
Seldon Core V2 本地实验(Local Experiments)实战:模型/管道流量分割、镜像与粘性会话完整指南
本文基于 seldon-core 仓库中的 samples/local-experiments.md(由 docs/source/contents/examples/local-experiments.md 以 include 方式引入)编写,完整演示如何在不依赖 Kubernetes 的本地环境中,使用 seldon CLI 对模型(Model)和管道(Pipeline)创建、运行并观测实验(Experiment):包括按权重分割流量的 A/B 实验、将流量镜像到影子模型的 Mirror 实验,以及通过 x-seldon-route 响应头实现的粘性会话(Sticky Session)路由。读完本文,你将掌握 Experiment 资源的完整生命周期(加载模型 → 创建/启动实验 → 推理验证 → 停止实验 → 卸载资源),并能通过 CLI 输出、Agent 暴露的 Prometheus 指标和 seldon pipeline inspect 三类手段验证流量走向。
运行前提:所有示例命令均需在仓库的
samples目录下执行;本地环境需要已经启动 seldon-core v2 的本地组件(数据面 Envoy 默认监听localhost:9000、控制面 Scheduler 默认监听localhost:9004),并且安装好seldonCLI。CLI 的默认推理地址与相关标志定义可参见 operator/cmd/seldon/cli/flags.go(defaultInferHost = "0.0.0.0:9000")。
一、Experiment 资源模型:先理解“实验”在 Seldon Core V2 中的定义
所有本地实验都围绕 mlops.seldon.io/v1alpha1 的 Experiment 资源展开。从 operator/apis/mlops/v1alpha1/experiment_types.go 可以看出其核心结构:
type ExperimentSpec struct {
Default *string `json:"default,omitempty"`
Candidates []ExperimentCandidate `json:"candidates"`
Mirror *ExperimentMirror `json:"mirror,omitempty"`
ResourceType ResourceType `json:"resourceType,omitempty"`
}
type ExperimentCandidate struct {
Name string `json:"name"`
Weight uint32 `json:"weight"`
}
type ExperimentMirror struct {
Name string `json:"name"`
Percent uint32 `json:"percent"`
}
字段语义如下:
| 字段 | 类型 | 说明 |
|---|---|---|
default |
string | 实验的默认资源(模型名或管道名),实验关闭后流量回落到的目标 |
candidates |
数组 | 参与流量分割的候选资源列表,每项包含 name 与 weight(权重) |
mirror |
对象 | 可选,流量镜像目标,包含 name(被镜像资源名)与 percent(镜像百分比) |
resourceType |
string | 资源类型,取值为 model(默认)或 pipeline,决定实验作用在模型还是管道上 |
其中 ResourceType 的两种取值定义在同一文件(operator/apis/mlops/v1alpha1/experiment_types.go):
const (
ModelResourceType ResourceType = "model"
PipelineResourceType ResourceType = "pipeline"
)
当 CLI 提交实验时,AsSchedulerExperimentRequest() 会把该 CRD 结构转换成 Scheduler 侧的 gRPC 请求(operator/apis/mlops/v1alpha1/experiment_types.go);Scheduler 内部以 scheduler/pkg/store/experiment/experiment.go 的 Experiment 结构体存储运行态信息——其中 Active、Default、ResourceType、Candidates、Mirror 分别对应规格字段,而 CandidatesReady/IsMirrorReady 这两个方法(scheduler/pkg/store/experiment/experiment.go)正是 CLI 状态轮询所依赖的“就绪判定”逻辑。理解这一点,有助于读懂下文 seldon experiment status -w 输出中的 candidatesReady 与 mirrorReady 字段。
二、Model Experiment:两个 Iris 模型之间的 50/50 A/B 分割
2.1 定义并加载两个模型
实验使用两个完全相同的 SKlearn Iris 分类模型,只是元数据名称不同。查看模型清单:
cat ./models/sklearn1.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: iris
spec:
storageUri: "gs://seldon-models/mlserver/iris"
requirements:
- sklearn
cat ./models/sklearn2.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: iris2
spec:
storageUri: "gs://seldon-models/mlserver/iris"
requirements:
- sklearn
两个清单均位于仓库 samples/models/sklearn1.yaml 与 samples/models/sklearn2.yaml。storageUri 指向 GCS 上的公共模型包,requirements 声明该模型需要 sklearn 能力(由 MLServer 提供),Scheduler 会据此将模型调度到具备相应能力的服务器上。
加载两个模型:
seldon model load -f ./models/sklearn1.yaml
seldon model load -f ./models/sklearn2.yaml
{}
{}
等待两个模型进入 ModelAvailable 状态:
seldon model status iris -w ModelAvailable
seldon model status iris2 -w ModelAvailable
{}
{}
说明:
-w对应 CLI 的wait-condition标志,ModelStatus()实现中会以 1 秒为间隔轮询 Scheduler 的状态流,直到满足指定状态或超时(见 operator/pkg/cli/scheduler.go)。
2.2 实验前基线推理
用 50 次迭代调用两个模型,确认各自可独立服务:
seldon model infer iris -i 50 \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
Success: map[:iris_1::50]
seldon model infer iris2 -i 50 \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
Success: map[:iris2_1::50]
这里 -i 50(--iterations)表示执行 50 次推理,输出 Success: map<a href="https://link.gitcode.com/i/d66ea11a1e586d2f24138e5facfdd782" target="_blank">:iris_1::50] 的统计格式来自 [operator/pkg/cli/infer.go:CLI 会解析响应头 x-seldon-route,并按路由名累加计数。iris_1 表示模型 iris 的第 1 个版本,即“推理真正命中的模型内部名称”。
2.3 创建 50/50 A/B 实验
创建实验清单,在 iris 上新增一个候选 iris2,两者各占 50% 流量:
cat ./experiments/ab-default-model.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: experiment-sample
spec:
default: iris
candidates:
- name: iris
weight: 50
- name: iris2
weight: 50
启动实验:
seldon experiment start -f ./experiments/ab-default-model.yaml
等待实验就绪:
seldon experiment status experiment-sample -w | jq -M .
{
"experimentName": "experiment-sample",
"active": true,
"candidatesReady": true,
"mirrorReady": true,
"statusDescription": "experiment active",
"kubernetesMeta": {}
}
说明:
-w会让ExperimentStatus()持续轮询直到active为 true(operator/pkg/cli/scheduler.go)。candidatesReady与mirrorReady分别对应 Scheduler 存储层对候选资源与镜像资源就绪状态的判定(scheduler/pkg/store/experiment/experiment.go)。
2.4 验证流量按权重分割
再次发起 50 次推理,观察路由统计是否接近 50/50:
seldon model infer iris -i 50 \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
Success: map[:iris2_1::27 :iris_1::23]
如输出所示,50 次请求中有 27 次命中 iris2_1、23 次命中 iris_1,大致符合 50/50 的权重配置(实际分布服从概率抽样,次数越多越接近权重比例)。
2.5 观察并复用粘性会话(Sticky Session)
用 --show-headers 查看实验返回的完整 HTTP 响应头,重点是 x-seldon-route:
seldon model infer iris --show-headers \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
> POST /v2/models/iris/infer HTTP/1.1
> Host: localhost:9000
> Content-Type:[application/json]
> Seldon-Model:[iris]
< X-Seldon-Route:[:iris_1:]
< Ce-Id:[463e96ad-645f-4442-8890-4c340b58820b]
< Traceparent:[00-fe9e87fcbe4be98ed82fb76166e15ceb-d35e7ac96bd8b718-01]
< X-Envoy-Upstream-Service-Time:[3]
< Ce-Specversion:[0.3]
< Date:[Thu, 29 Jun 2023 14:03:03 GMT]
< Ce-Source:[io.seldon.serving.deployment.mlserver]
< Content-Type:[application/json]
< Server:[envoy]
< X-Request-Id:[cieou5ofh5ss73fbjdu0]
< Ce-Endpoint:[iris_1]
< Ce-Modelid:[iris_1]
< Ce-Type:[io.seldon.serving.inference.response]
< Content-Length:[213]
< Ce-Inferenceservicename:[mlserver]
< Ce-Requestid:[463e96ad-645f-4442-8890-4c340b58820b]
{
"model_name": "iris_1",
"model_version": "1",
"id": "463e96ad-645f-4442-8890-4c340b58820b",
"parameters": {},
"outputs": [
{
"name": "predict",
"shape": [
1,
1
],
"datatype": "INT64",
"parameters": {
"content_type": "np"
},
"data": [
2
]
}
]
}
响应头中 X-Seldon-Route:<a href="https://link.gitcode.com/i/c8ac6163539bec184c1b18808e2fe388" target="_blank">:iris_1:] 即本次请求实际命中的路由。CLI 会把该值写入本地 session 文件——这正是粘性会话的实现基础。从源码看,REST 与 gRPC 两种协议下分别由 saveStickySessionKeyHttp 与 saveStickySessionKeyGrpc 提取 x-seldon-route 响应头并写入 session 文件([operator/pkg/cli/session.go),后续使用 -s 时再由 getStickySessionKeys() 读回(operator/pkg/cli/session.go)。
使用 -s(--sticky-session)让后续请求沿用上一次的路由键,从而保证每次都命中同一候选:
seldon model infer iris -s -i 50 \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
Success: map[:iris_1::50]
gRPC 协议同样支持粘性会话:
seldon model infer iris --inference-mode grpc -s -i 50\
'{"model_name":"iris","inputs":[{"name":"input","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[1,4]}]}'
Success: map[:iris_1::50]
说明:
--inference-mode取值rest或grpc,定义见 operator/cmd/seldon/cli/flags.go。gRPC 模式下,CLI 通过 gRPC 拦截器打印 HTTP/2 请求/响应头(operator/pkg/cli/infer.go),粘性会话键通过 gRPC metadata 传递(addStickySessionToContext,operator/pkg/cli/infer.go)。
2.6 停止实验并卸载模型
seldon experiment stop experiment-sample
seldon model unload iris
seldon model unload iris2
三、Pipeline Experiment:在两个管道之间分割流量
管道实验与模型实验流程一致,区别在于:实验对象是 Pipeline 资源,且 Experiment 清单需显式声明 resourceType: pipeline。此部分示例同时展示了管道推理、管道状态查询以及“管道 + 底层模型”两级路由的统计输出。
3.1 准备模型与管道
查看两个 Triton 模型(add10 在输入上加 10,mul10 乘 10):
cat ./models/add10.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: add10
spec:
storageUri: "gs://seldon-models/scv2/samples/triton_23-03/add10"
requirements:
- triton
- python
cat ./models/mul10.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: mul10
spec:
storageUri: "gs://seldon-models/scv2/samples/triton_23-03/mul10"
requirements:
- triton
- python
加载并等待就绪:
seldon model load -f ./models/add10.yaml
seldon model load -f ./models/mul10.yaml
{}
{}
seldon model status add10 -w ModelAvailable
seldon model status mul10 -w ModelAvailable
{}
{}
定义两个单步管道,分别封装 mul10 与 add10:
cat ./pipelines/mul10.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Pipeline
metadata:
name: pipeline-mul10
spec:
steps:
- name: mul10
output:
steps:
- mul10
cat ./pipelines/add10.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Pipeline
metadata:
name: pipeline-add10
spec:
steps:
- name: add10
output:
steps:
- add10
两个管道清单对应仓库中的 samples/pipelines/add10.yaml 与 samples/pipelines/mul10.yaml。
加载管道并等待就绪:
seldon pipeline load -f ./pipelines/add10.yaml
seldon pipeline load -f ./pipelines/mul10.yaml
seldon pipeline status pipeline-add10 -w PipelineReady
seldon pipeline status pipeline-mul10 -w PipelineReady
{"pipelineName":"pipeline-add10", "versions":[{"pipeline":{"name":"pipeline-add10", "uid":"cieov47l80lc739juklg", "version":1, "steps":[{"name":"add10"}], "output":{"steps":["add10.outputs"]}, "kubernetesMeta":{}}, "state":{"pipelineVersion":1, "status":"PipelineReady", "reason":"created pipeline", "lastChangeTimestamp":"2023-06-29T14:05:04.460868091Z", "modelsReady":true}}]}
{"pipelineName":"pipeline-mul10", "versions":[{"pipeline":{"name":"pipeline-mul10", "uid":"cieov47l80lc739jukm0", "version":1, "steps":[{"name":"mul10"}], "output":{"steps":["mul10.outputs"]}, "kubernetesMeta":{}}, "state":{"pipelineVersion":1, "status":"PipelineReady", "reason":"created pipeline", "lastChangeTimestamp":"2023-06-29T14:05:04.631980330Z", "modelsReady":true}}]}
3.2 管道基线推理
通过 gRPC 协议分别调用两个管道,验证加/乘语义正确:
seldon pipeline infer pipeline-add10 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}' | jq -M .
{
"outputs": [
{
"name": "OUTPUT",
"datatype": "FP32",
"shape": [
"4"
],
"contents": {
"fp32Contents": [
11,
12,
13,
14
]
}
}
]
}
seldon pipeline infer pipeline-mul10 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}' | jq -M .
{
"outputs": [
{
"name": "OUTPUT",
"datatype": "FP32",
"shape": [
"4"
],
"contents": {
"fp32Contents": [
10,
20,
30,
40
]
}
}
]
}
输入 [1,2,3,4] 经 add10 输出 [11,12,13,14],经 mul10 输出 [10,20,30,40],语义验证无误。
3.3 创建管道 A/B 实验
cat ./experiments/addmul10.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: addmul10
spec:
default: pipeline-add10
resourceType: pipeline
candidates:
- name: pipeline-add10
weight: 50
- name: pipeline-mul10
weight: 50
启动并等待就绪:
seldon experiment start -f ./experiments/addmul10.yaml
seldon experiment status addmul10 -w | jq -M .
{
"experimentName": "addmul10",
"active": true,
"candidatesReady": true,
"mirrorReady": true,
"statusDescription": "experiment active",
"kubernetesMeta": {}
}
3.4 验证管道流量分割
以 pipeline-add10 为入口执行 50 次推理,注意统计中同时出现管道级路由与模型级路由:
seldon pipeline infer pipeline-add10 -i 50 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
Success: map[:add10_1::28 :mul10_1::22 :pipeline-add10.pipeline::28 :pipeline-mul10.pipeline::22]
其中 pipeline-add10.pipeline / pipeline-mul10.pipeline 表示实验在管道层面选择的候选;add10_1 / mul10_1 表示实际执行的底层模型。可以看到 50 次请求在两个管道间约 28/22 分割(接近 50/50),模型级统计与管道级统计保持一致——这正是 Seldon v2 实验路由的“两级路由”特性。
3.5 管道的粘性会话
用 --show-headers 观察 gRPC 实验请求返回的路由头:
seldon pipeline infer pipeline-add10 --show-headers --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
> /inference.GRPCInferenceService/ModelInfer HTTP/2
> Host: localhost:9000
> seldon-model:[pipeline-add10.pipeline]
< x-envoy-expected-rq-timeout-ms:[60000]
< x-request-id:[cieov8ofh5ss739277i0]
< date:[Thu, 29 Jun 2023 14:05:23 GMT]
< server:[envoy]
< content-type:[application/grpc]
< x-envoy-upstream-service-time:[6]
< x-seldon-route:[:add10_1: :pipeline-add10.pipeline:]
< x-forwarded-proto:[http]
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[11, 12, 13, 14]}}]}
x-seldon-route 同时携带模型级与管道级两个路由键,返回给客户端供粘性会话复用。携带 -s 再发一次请求,可见请求头与响应头都带上了该路由:
seldon pipeline infer pipeline-add10 -s --show-headers --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
> /inference.GRPCInferenceService/ModelInfer HTTP/2
> Host: localhost:9000
> x-seldon-route:[:add10_1: :pipeline-add10.pipeline:]
> seldon-model:[pipeline-add10.pipeline]
< content-type:[application/grpc]
< x-forwarded-proto:[http]
< x-envoy-expected-rq-timeout-ms:[60000]
< x-seldon-route:[:add10_1: :pipeline-add10.pipeline: :pipeline-add10.pipeline:]
< x-request-id:[cieov90fh5ss739277ig]
< x-envoy-upstream-service-time:[7]
< date:[Thu, 29 Jun 2023 14:05:24 GMT]
< server:[envoy]
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[11, 12, 13, 14]}}]}
连续执行 50 次粘性会话推理,全部命中同一路由组合:
seldon pipeline infer pipeline-add10 -s -i 50 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
Success: map[:add10_1::50 :pipeline-add10.pipeline::150]
注意此处模型级命中 50 次、管道级统计为 150 次(每次请求在管道输入、模型调用、管道输出等多个上报点都会累加),这是 gRPC 管道推理在多级上报下的计数特征。
3.6 模型实验与管道实验叠加
管道实验与模型实验可以同时存在。加载第三个模型 add20(输入加 20):
cat ./models/add20.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: add20
spec:
storageUri: "gs://seldon-models/triton/add20"
requirements:
- triton
- python
seldon model load -f ./models/add20.yaml
{}
seldon model status add20 -w ModelAvailable
{}
创建以 add10 为默认、add10 与 add20 各 50% 的模型级实验:
cat ./experiments/add1020.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: add1020
spec:
default: add10
candidates:
- name: add10
weight: 50
- name: add20
weight: 50
seldon experiment start -f ./experiments/add1020.yaml
seldon experiment status add1020 -w | jq -M .
{
"experimentName": "add1020",
"active": true,
"candidatesReady": true,
"mirrorReady": true,
"statusDescription": "experiment active",
"kubernetesMeta": {}
}
此时模型实验(add1020)与管道实验(addmul10)同时生效。直接推理模型 add10,流量在 add10_1 与 add20_1 间分割:
seldon model infer add10 -i 50 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
Success: map[:add10_1::22 :add20_1::28]
再经管道入口推理 100 次,可以看到两层实验同时参与路由:模型级候选 add10_1/add20_1 与管道级候选 pipeline-add10.pipeline/pipeline-mul10.pipeline 都出现在统计中:
seldon pipeline infer pipeline-add10 -i 100 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
Success: map[:add10_1::24 :add20_1::32 :mul10_1::44 :pipeline-add10.pipeline::56 :pipeline-mul10.pipeline::44]
用 --show-headers 查看命中组合,本例中请求先被模型实验路由到 add20_1(输出 [21,22,23,24]),同时管道级路由仍为 pipeline-add10.pipeline:
seldon pipeline infer pipeline-add10 --show-headers --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
> /inference.GRPCInferenceService/ModelInfer HTTP/2
> Host: localhost:9000
> seldon-model:[pipeline-add10.pipeline]
< x-request-id:[cieovf0fh5ss739279u0]
< x-envoy-upstream-service-time:[5]
< x-seldon-route:[:add10_1: :pipeline-add10.pipeline:]
< date:[Thu, 29 Jun 2023 14:05:48 GMT]
< server:[envoy]
< content-type:[application/grpc]
< x-forwarded-proto:[http]
< x-envoy-expected-rq-timeout-ms:[60000]
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[11, 12, 13, 14]}}]}
使用 -s 的粘性会话请求完整携带了各级路由键:
seldon pipeline infer pipeline-add10 -s --show-headers --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
> /inference.GRPCInferenceService/ModelInfer HTTP/2
> Host: localhost:9000
> x-seldon-route:[:add10_1: :pipeline-add10.pipeline:]
> seldon-model:[pipeline-add10.pipeline]
< x-forwarded-proto:[http]
< x-envoy-expected-rq-timeout-ms:[60000]
< x-request-id:[cieovf8fh5ss739279ug]
< x-envoy-upstream-service-time:[6]
< date:[Thu, 29 Jun 2023 14:05:49 GMT]
< content-type:[application/grpc]
< x-seldon-route:[:add10_1: :pipeline-add10.pipeline: :add20_1: :pipeline-add10.pipeline:]
< server:[envoy]
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[21, 22, 23, 24]}}]}
清理全部实验与资源:
seldon experiment stop addmul10
seldon experiment stop add1020
seldon pipeline unload pipeline-add10
seldon pipeline unload pipeline-mul10
seldon model unload add10
seldon model unload add20
seldon model unload mul10
四、Model Mirror Experiment:把流量镜像到影子模型
镜像实验(Mirror)与 A/B 实验的区别在于:候选只接收响应流量(canary),而镜像目标按百分比复制全部请求但不影响线上响应。典型用途是在不影响生产结果的前提下,用真实流量对新模型做影子验证。
仍使用 iris 与 iris2 两个模型,流程与 2.1/2.2 相同(加载、等待 ModelAvailable)。创建镜像实验清单:
cat ./experiments/sklearn-mirror.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: sklearn-mirror
spec:
default: iris
candidates:
- name: iris
weight: 100
mirror:
name: iris2
percent: 100
配置解读:
candidates仅含iris且权重 100,即所有线上流量仍全部路由到iris;mirror.name = iris2、mirror.percent = 100,表示 100% 的请求在发往iris的同时,还会复制一份到iris2(影子模型)。
启动并等待就绪:
seldon experiment start -f ./experiments/sklearn-mirror.yaml
seldon experiment status sklearn-mirror -w | jq -M .
{
"experimentName": "sklearn-mirror",
"active": true,
"candidatesReady": true,
"mirrorReady": true,
"statusDescription": "experiment active",
"kubernetesMeta": {}
}
注意此处 mirrorReady: true 即表示镜像目标 iris2 已就绪——对应存储层 IsMirrorReady() 的判定(scheduler/pkg/store/experiment/experiment.go)。
执行 50 次推理,响应仍全部来自 iris(iris_1):
seldon model infer iris -i 50 \
'{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
Success: map[:iris_1::50]
但所有请求同时也被镜像到了 iris2。通过 Agent 暴露的本地 Prometheus 指标端口(0.0.0.0:9006/metrics)验证:
curl -s 0.0.0:9006/metrics | grep seldon_model_infer_total | grep iris2_1
seldon_model_infer_total{code="200",method_type="rest",model="iris",model_internal="iris2_1",server="mlserver",server_replica="0"} 50
指标行中 model="iris"(实验入口)、model_internal="iris2_1"(镜像目标)、code="200"、计数值 50 与请求次数一致,说明 50 个请求全部被镜像到 iris2_1 并成功执行。这就是影子流量验证的核心观测手段。
停止实验并卸载:
seldon experiment stop sklearn-mirror
seldon model unload iris
seldon model unload iris2
五、Pipeline Mirror Experiment:镜像整个管道
管道级镜像与模型级镜像完全对称:默认流量全量走 pipeline-add10,同时按 100% 比例把请求复制到 pipeline-mul10。此部分还展示了用 seldon pipeline inspect 检查镜像管道是否真正被调用的方法。
准备 add10/mul10 模型与两个管道(加载、等待就绪的步骤与 3.1 相同),基线推理验证语义:
seldon pipeline infer pipeline-add10 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[11, 12, 13, 14]}}]}
seldon pipeline infer pipeline-mul10 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[10, 20, 30, 40]}}]}
创建管道镜像实验:
cat ./experiments/addmul10-mirror.yaml
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: addmul10-mirror
spec:
default: pipeline-add10
resourceType: pipeline
candidates:
- name: pipeline-add10
weight: 100
mirror:
name: pipeline-mul10
percent: 100
seldon experiment start -f ./experiments/addmul10-mirror.yaml
seldon experiment status addmul10-mirror -w | jq -M .
{
"experimentName": "addmul10-mirror",
"active": true,
"candidatesReady": true,
"mirrorReady": true,
"statusDescription": "experiment active",
"kubernetesMeta": {}
}
执行 1 次 gRPC 推理,响应来自 pipeline-add10([11,12,13,14]):
seldon pipeline infer pipeline-add10 -i 1 --inference-mode grpc \
'{"model_name":"add10","inputs":[{"name":"INPUT","contents":{"fp32_contents":[1,2,3,4]},"datatype":"FP32","shape":[4]}]}'
{"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[11, 12, 13, 14]}}]}
通过 Agent 指标端口(此处为 0.0.0.0:9007/metrics)验证 mul10 与 add10 都各被调用 1 次(grpc 模式下指标 code 为 "OK"):
curl -s 0.0.0:9007/metrics | grep seldon_model_infer_total | grep mul10_1
seldon_model_infer_total{code="OK",method_type="grpc",model="mul10",model_internal="mul10_1",server="triton",server_replica="0"} 2
curl -s 0.0.0:9007/metrics | grep seldon_model_infer_total | grep add10_1
seldon_model_infer_total{code="OK",method_type="grpc",model="add10",model_internal="add10_1",server="triton",server_replica="0"} 2
再发一次 HTTP 推理(REST 协议),确认响应正常后再次检查指标计数各增至 3,说明 HTTP 请求同样被镜像:
seldon pipeline infer pipeline-add10 -i 1 \
'{"model_name":"add10","inputs":[{"name":"INPUT","data":[1,2,3,4],"datatype":"FP32","shape":[4]}]}'
{
"model_name": "",
"outputs": [
{
"data": [
11,
12,
13,
14
],
"name": "OUTPUT",
"shape": [
4
],
"datatype": "FP32"
}
]
}
curl -s 0.0.0:9007/metrics | grep seldon_model_infer_total | grep mul10_1
seldon_model_infer_total{code="OK",method_type="grpc",model="mul10",model_internal="mul10_1",server="triton",server_replica="0"} 3
curl -s 0.0.0:9007/metrics | grep seldon_model_infer_total | grep add10_1
seldon_model_infer_total{code="OK",method_type="grpc",model="add10",model_internal="add10_1",server="triton",server_replica="0"} 3
注意:指标行中
method_type="grpc"是镜像请求在 Agent 内部上报时的传输类型标记(镜像复制不改变原始请求的 grpc 语义),并不表示上文那个 REST 请求变成了 grpc 请求;两次推理后mul10_1与add10_1计数同步从 2 增至 3,与“每次线上请求都复制一份给镜像管道”的行为一致。
最后用 seldon pipeline inspect 查看 Kafka 数据流中记录的 pipeline-mul10 输入/输出消息,进一步确认镜像管道确实收到了请求并完成了推理:
seldon pipeline inspect pipeline-mul10
seldon.default.model.mul10.inputs ciep0bofh5ss73dpdiq0 {"inputs":[{"name":"INPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[1, 2, 3, 4]}}]}
seldon.default.model.mul10.outputs ciep0bofh5ss73dpdiq0 {"modelName":"mul10_1", "modelVersion":"1", "outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[10, 20, 30, 40]}}]}
seldon.default.pipeline.pipeline-mul10.inputs ciep0bofh5ss73dpdiq0 {"inputs":[{"name":"INPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[1, 2, 3, 4]}}]}
seldon.default.pipeline.pipeline-mul10.outputs ciep0bofh5ss73dpdiq0 {"outputs":[{"name":"OUTPUT", "datatype":"FP32", "shape":["4"], "contents":{"fp32Contents":[10, 20, 30, 40]}}]}
从输出可见:镜像管道 pipeline-mul10 的输入 [1,2,3,4] 与其底层模型 mul10 的输出 [10,20,30,40] 均被记录在数据流中,与实验预期完全一致。
清理实验与资源:
seldon experiment stop addmul10-mirror
seldon pipeline unload pipeline-add10
seldon pipeline unload pipeline-mul10
seldon model unload add10
seldon model unload mul10
六、实验路由与粘性会话的实现原理
通过上面的实操,可以总结出 seldon-core v2 本地实验路由的底层机制:
-
路由决策发生在 Envoy 层面:实验启动后,Scheduler 会为实验生成 Envoy 路由配置。仓库中 scheduler/pkg/envoy/processor/snapshots_testdata/experiment-candidate-mirror-routes.json 与
delete-experiment-routes.json等测试数据展示了候选路由与镜像路由的 Envoy 快照结构,其中大量出现x-seldon-route请求头与响应头的配置项——这正是候选选择与镜像复制的执行点。 -
路由键通过
x-seldon-route传递:CLI 在 REST 与 gRPC 两种协议下都通过该头携带/读取路由键。相关常量SeldonRouteHeader = "x-seldon-route"定义在 operator/pkg/cli/infer.go,请求侧的注入与统计解析也都在该文件(addStickySessionToRequest/updateSummary)。 -
粘性会话 = 本地 session 文件:CLI 会把上一次响应的路由键写入配置文件目录下的
session文件(多个键以逗号分隔),-s参数时读回并附加到后续请求(operator/pkg/cli/session.go)。因此粘性会话只在实验场景下有效——这也是 CLI 帮助文本明确标注的语义(operator/cmd/seldon/cli/flags.go:“use sticky session from last inference (only works with experiments)”)。 -
镜像验证的三类观测手段:一是 CLI 统计(
Success: map[...],仅反映响应流量);二是 Agent 暴露的seldon_model_infer_total指标(含model_internal标签,能反映镜像目标);三是seldon pipeline inspect直接查看 Kafka 数据流中的输入/输出记录(能反映镜像管道是否真实执行)。
七、总结
本文完整走通了 seldon-core v2 在非 Kubernetes 本地环境下的四类实验场景:
- 模型 A/B 实验(
experiment-sample):按权重在iris/iris2间分割流量,验证 50/50 分布; - 管道 A/B 实验(
addmul10、叠加add1020):演示管道级与模型级两层实验同时生效时的路由组合; - 模型镜像实验(
sklearn-mirror):候选 100% 承接线上流量、iris2作为影子模型全量接收副本; - 管道镜像实验(
addmul10-mirror):镜像整个管道,并通过指标与数据流检查确认影子执行。
所有资源清单均可在仓库中找到可复现的原始文件:模型定义在 samples/models(sklearn1.yaml、sklearn2.yaml、add10.yaml、mul10.yaml、add20.yaml),管道定义在 samples/pipelines(add10.yaml、mul10.yaml),实验定义在 samples/experiments(ab-default-model.yaml、addmul10.yaml、add1020.yaml、sklearn-mirror.yaml、addmul10-mirror.yaml)。如需在 Kubernetes 集群上运行同类实验,可参考仓库 docs-gb/examples/experiment-versions.md 与 samples/experiment_versions.md 中基于 CRD 的集群版流程;而本文的本地流程用于快速验证路由语义、调试粘性会话行为,是理解 seldon-core v2 实验体系最直接的上手路径。