Seldon Core 2 的 Kubernetes 自定义资源(CRD)体系全解析:SeldonRuntime、Server、Model、Experiment 与 Pipeline 实战指南
Seldon Core 2 的 Kubernetes 自定义资源(CRD)体系全解析:SeldonRuntime、Server、Model、Experiment 与 Pipeline 实战指南
导读:本文以 Seldon Core 2 官方 Kubernetes 资源文档(docs-gb/kubernetes/resources/README.md)为骨架,系统讲解 Seldon Core 2 面向 Kubernetes 提供的整套自定义资源(Custom Resource,CR):从按命名空间安装 Seldon 的
SeldonRuntime,到部署推理服务的Server、声明模型的Model、做流量切分与 A/B 测试的Experiment、编排数据流的Pipeline,以及面向高级定制场景的SeldonConfig与ServerConfig。读完本文,你将掌握每种资源的核心 spec 字段、完整 YAML 写法、各资源之间的引用关系,以及它们在当前仓库中对应的 CRD 定义、默认配置与真实样例路径,可直接用于自己的 Kubernetes 集群中部署与管理生产级机器学习模型。
一、资源全景:Seldon Core 2 面向 Kubernetes 的 CR 架构
Seldon Core 2 在 Kubernetes 上提供了一套**自定义资源(Custom Resources)**用于与 Seldon 交互。所有资源均属于 API 组 mlops.seldon.io/v1alpha1,对应的 CRD(CustomResourceDefinition)定义集中在仓库的 operator/config/crd/bases 目录下,共 7 个文件:
| CRD 文件 | 资源 | 用途 |
|---|---|---|
mlops.seldon.io_seldonruntimes.yaml |
SeldonRuntime |
在指定命名空间安装一套 Seldon 实例 |
mlops.seldon.io_servers.yaml |
Server |
部署一组核心推理服务器副本(MLServer 或 Triton) |
mlops.seldon.io_models.yaml |
Model |
部署单个机器学习模型、自定义转换逻辑、漂移/离群检测器与解释器 |
mlops.seldon.io_experiments.yaml |
Experiment |
在模型或 Pipeline 之间做流量切分,测试新版本 |
mlops.seldon.io_pipelines.yaml |
Pipeline |
连接多个 Model 之间的数据流 |
mlops.seldon.io_seldonconfigs.yaml |
SeldonConfig |
定义 Seldon 的核心安装配置 |
mlops.seldon.io_serverconfigs.yaml |
ServerConfig |
定义新的推理服务器类型,供 Server 资源引用 |
其中前 5 类资源是日常使用的主体;SeldonConfig 与 ServerConfig 属于高级定制能力——它们分别定义 Seldon 的核心安装配置与机器学习推理服务器配置,正常情况下无需修改,仅当组织内有特殊安装需求(例如自定义镜像、自定义 Service 类型)时才需要定制。
从源码结构看,这 7 类资源的 Go 类型定义位于 operator/apis/mlops/v1alpha1,并由 operator/controllers/mlops 中的控制器负责调谐(reconcile),将 CR 声明转换为实际的 Deployment/StatefulSet 等工作负载。
二、SeldonRuntime:按命名空间安装一套 Seldon
SeldonRuntime 资源用于在特定命名空间内创建一套 Seldon 实例。它不直接定义组件细节,而是引用一个 SeldonConfig 作为安装基线,并允许通过 overrides 对该基线做局部覆盖。
2.1 Spec 结构详解
其 Go 定义如下:
type SeldonRuntimeSpec struct {
SeldonConfig string `json:"seldonConfig"`
Overrides []*OverrideSpec `json:"overrides,omitempty"`
Config SeldonConfiguration `json:"config,omitempty"`
// +Optional
// If set then when the referenced SeldonConfig changes we will NOT update the SeldonRuntime immediately.
// Explicit changes to the SeldonRuntime itself will force a reconcile though
DisableAutoUpdate bool `json:"disableAutoUpdate,omitempty"`
}
type OverrideSpec struct {
Name string `json:"name"`
Disable bool `json:"disable,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
ServiceType v1.ServiceType `json:"serviceType,omitempty"`
PodSpec *PodSpec `json:"podSpec,omitempty"`
}
字段含义:
seldonConfig(必填):作为安装基线的SeldonConfig资源名称。当前仓库提供的默认配置名为default,定义于 operator/config/seldonconfigs/default.yaml。overrides(可选):对所选SeldonConfig中某个组件的覆盖项,每个覆盖项通过name指定组件名。其中PodSpec需要同时指定**容器名(container name)**以及要覆盖的字段,才能对某个容器生效;replicas可覆盖副本数;serviceType可覆盖 Service 类型;disable可整体停用该组件。config(可选):SeldonConfiguration类型的配置,其定义见 SeldonConfig 资源。disableAutoUpdate(可选):若设置为true,当被引用的SeldonConfig发生变化时,不会立即更新本SeldonRuntime;但对该SeldonRuntime自身的显式变更仍会触发一次调谐。
2.2 最小使用方式
最简用法只需指定 seldonConfig。例如在 seldon-mesh 命名空间中以名为 default 的 SeldonConfig 安装 Seldon:
apiVersion: mlops.seldon.io/v1alpha1
kind: SeldonRuntime
metadata:
name: seldon
namespace: seldon-mesh
spec:
seldonConfig: default
2.3 覆盖示例:调整 hodometer 组件的资源限额
下面的示例覆盖了 seldon-mesh 命名空间中 hodometer 组件的 cpu 与 memory 资源限制,其余部分(例如 default 配置)仍使用 SeldonConfig 中定义的值:
apiVersion: mlops.seldon.io/v1alpha1
kind: SeldonRuntime
metadata:
name: seldon
namespace: seldon-mesh
spec:
overrides:
- name: hodometer
podSpec:
containers:
- name: hodometer
resources:
limits:
memory: 64Mi
cpu: 20m
seldonConfig: default
2.4 Helm 安装方式与配置更新传播
官方 Helm chart seldon-core-v2-runtime(见 k8s/helm-charts/seldon-core-v2-runtime)可以便捷地创建本资源,并在某个命名空间内同时创建默认的 Server 资源,从而完成一次完整的 Seldon 安装。
关于配置传播:当一个 SeldonConfig 资源发生变更时,所有引用它的 SeldonRuntime 资源也会立即被更新。如果不希望出现该行为,可在 SeldonRuntime 中设置 spec.disableAutoUpdate: true,这样它只会在自身发生变化或其所拥有的子资源发生变化时才被更新。
三、Server:部署一组核心推理服务器副本
Server 定义了一类推理服务器,模型会被调度到这些服务器上进行推理。默认安装会自动提供两个初始 Server:一个 MLServer、一个 Triton,因此只有高级场景(例如需要自定义镜像、自定义能力标签、挂载 PVC)才需要额外定义 Server。
3.1 基础示例
最简单的 Server 定义如下,核心要求是引用一个 ServerConfig 资源(本例为 mlserver):
apiVersion: mlops.seldon.io/v1alpha1
kind: Server
metadata:
name: mlserver
spec:
serverConfig: mlserver
replicas: 1
3.2 ServerSpec 字段详解
type ServerSpec struct {
// Server definition
ServerConfig string `json:"serverConfig"`
// The extra capabilities this server will advertise
// These are added to the capabilities exposed by the referenced ServerConfig
ExtraCapabilities []string `json:"extraCapabilities,omitempty"`
// The capabilities this server will advertise
// This will override any from the referenced ServerConfig
Capabilities []string `json:"capabilities,omitempty"`
// Image overrides
ImageOverrides *ContainerOverrideSpec `json:"imageOverrides,omitempty"`
// PodSpec overrides
// Slices such as containers would be appended not overridden
PodSpec *PodSpec `json:"podSpec,omitempty"`
// Scaling spec
ScalingSpec `json:",inline"`
// +Optional
// If set then when the referenced ServerConfig changes we will NOT update the Server immediately.
// Explicit changes to the Server itself will force a reconcile though
DisableAutoUpdate bool `json:"disableAutoUpdate,omitempty"`
}
type ContainerOverrideSpec struct {
// The Agent overrides
Agent *v1.Container `json:"agent,omitempty"`
// The RClone server overrides
RClone *v1.Container `json:"rclone,omitempty"`
}
字段要点:
serverConfig(必填):引用的ServerConfig资源名称,决定服务器的镜像、容器结构与默认能力。extraCapabilities:额外的能力标签,追加到所引用ServerConfig暴露的能力之上。capabilities:直接覆盖能力列表(注意与extraCapabilities的追加语义不同)。imageOverrides:针对 Agent 与 RClone 两个伴随容器的镜像覆盖。podSpec:Pod 级覆盖;其中containers等切片字段是**追加(append)**而非整体覆盖。disableAutoUpdate:与SeldonRuntime同义——置为true后,所引用ServerConfig的变更不会立即传导到本 Server。
3.3 自定义 Server:为现有 ServerConfig 换上自定义镜像
可以利用现有 ServerConfig 快速使用自定义镜像。例如定义名为 mlserver-134 的 MLServer,使用 seldonio/mlserver:1.3.4 镜像,并额外声明能力标签 mlserver-1.3.4(见 samples/servers/custom-mlserver.yaml):
apiVersion: mlops.seldon.io/v1alpha1
kind: Server
metadata:
name: mlserver-134
spec:
serverConfig: mlserver
extraCapabilities:
- mlserver-1.3.4
podSpec:
containers:
- image: seldonio/mlserver:1.3.4
name: mlserver
随后在创建 Model 时通过 spec.server 指定该 Server 名称即可把模型调度到这台服务器上(见 samples/models/iris-custom-server.yaml):
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: iris
spec:
storageUri: "gs://seldon-models/mlserver/iris"
server: mlserver-134
3.4 带 PVC 的 Server:直接从持久卷加载模型
也可以为 Server 增加持久卷,使模型能够直接从 PVC 加载。示例见 samples/examples/k8s-pvc/server.yaml:
apiVersion: mlops.seldon.io/v1alpha1
kind: Server
metadata:
name: mlserver-pvc
spec:
serverConfig: mlserver
extraCapabilities:
- "pvc"
podSpec:
volumes:
- name: models-pvc
persistentVolumeClaim:
claimName: ml-models-pvc
containers:
- name: rclone
volumeMounts:
- name: models-pvc
mountPath: /var/models
这里的关键在于:为 Server 增加了 pvc 能力标签,并将 PVC ml-models-pvc 挂载到 rclone 容器的 /var/models 目录。之后创建一个 storageUri 指向该持久卷的 Model(见 samples/examples/k8s-pvc/iris.yaml):
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: iris
spec:
storageUri: "/var/models/iris"
requirements:
- sklearn
- pvc
注意 Model 的 requirements 中同时声明了 sklearn 与 pvc,这两个标签都能被上述 Server 匹配。完整的端到端示例参见 k8s-pvc 实战文档。
对于更复杂的需求(例如希望把 Server 定义标准化、统一维护),可以自行创建 ServerConfig。
四、Model:部署机器学习工件的基础原子单元
Model 是 Seldon Core 2 中最核心的原子构建块,它声明一个机器学习工件,该工件会被加载到某个正在运行的 Server 上。一个 Model 可以是:
- 标准的机器学习推理组件,例如 TensorFlow 模型、PyTorch 模型或 SKLearn 模型;
- 推理转换组件,例如一个 SKLearn pipeline 或一段自定义 Python 逻辑;
- 监控组件,例如离群检测器(outlier detector)或漂移检测器(drift detector);
- 一个 alibi-explain 模型解释器。
下面是一个用于鸢尾花(iris)分类的 SKLearn 模型示例(见 samples/models/sklearn-iris-gs.yaml):
apiVersion: mlops.seldon.io/v1alpha1
kind: Model
metadata:
name: iris
spec:
storageUri: "gs://seldon-models/scv2/samples/mlserver_1.6.0/iris-sklearn"
requirements:
- sklearn
memory: 100Ki
其 spec 有两个核心要素:
storageUri:指定工件所在位置,可以是任意 rclone URI 规范(例如gs://、s3://、/var/models/...等)。rclone 是 Seldon 下载工件所依赖的组件。requirements:一组标签(tag),需要被能够运行该工件类型的Server所匹配。默认安装 Seldon 时会提供一组覆盖多种工件类型的 Server(MLServer 与 Triton),它们的默认能力标签分别定义在对应的ServerConfig中(见下文第七节)。
关于 Model 的更多部署方式(多版本管理、调度、安全端点等),参见 docs-gb/models/README.md。
五、Experiment:模型与 Pipeline 之间的流量切分
Experiment 定义了在多个 Model 或 Pipeline 之间的流量切分(traffic split),用于测试模型与 Pipeline 的新版本。
5.1 Spec 的三个部分
一份 experiment spec 包含三个部分:
candidates(必填):一组候选模型,用于切分流量。每个候选都有一个流量权重(weight),实际流量百分比 = 该权重 / 所有权重之和。default(可选):一个已存在的候选,其端点将被修改以按candidates定义的权重切分流量。用于把新版本(如iris2)挂到已有端点(如iris)上做测试。mirror(可选):一个被镜像(mirror)到候选组的模型。镜像流量的响应不会返回给调用方。
从源码看(operator/apis/mlops/v1alpha1/experiment_types.go),ExperimentSpec 对应的结构为:
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"`
}
其中 ResourceType 取值为 model 或 pipeline(见同文件 ResourceType 常量定义),默认是模型级实验。控制器通过 AsSchedulerExperimentRequest 将 CR 转换为 scheduler 协议中的 Experiment 请求,把候选、镜像与资源类型下发给调度器执行。
5.2 挂载到已有端点:default + candidates
下面的示例在 iris 与 iris2 两个模型之间切分 50% / 50% 流量,并复用 iris 模型已有的端点(见 samples/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
default: iris 定义了端点归属的模型。该 Experiment 会在两个底层模型都进入 Ready 状态后变为 active。
5.3 新端点 + curl 调用
另一种做法是不复用已有端点,而是为两个独立模型暴露一个新的 API 端点(见 samples/experiments/ab.yaml):
apiVersion: mlops.seldon.io/v1alpha1
kind: Experiment
metadata:
name: experiment-iris
spec:
candidates:
- name: iris
weight: 50
- name: iris2
weight: 50
调用该端点时,需要附加请求头 seldon-model: <experiment-name>.experiment。以 curl 为例:
curl http://${MESH_IP}/v2/models/experiment-iris/infer \
-H "Content-Type: application/json" \
-H "seldon-model: experiment-iris.experiment" \
-d '{"inputs": [{"name": "predict", "shape": [1, 4], "datatype": "FP32", "data": [[1, 2, 3, 4]]}]}'
更多示例见 本地实验 notebook。
5.4 Pipeline Experiments:在 Pipeline 之间切分流量
Pipeline 级实验与模型级非常相似,区别仅在于需要设置 resourceType: pipeline,此时 candidates 与 mirror 引用的是 Pipeline 名称。示例见 samples/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
5.5 Mirror Experiments:镜像流量
Mirror 可轻松添加到模型级或 Pipeline 级实验中。模型级镜像实验示例见 samples/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
Pipeline 级镜像实验示例见 samples/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
5.6 Sticky Sessions:保持用户会话一致性
为了让同一用户群体(cohort)在实验中获得一致的视角,每次推理请求都会在响应中带回一个 x-seldon-route 响应头。后续请求可以将该头传回实验,从而绕过随机流量切分,直接命中初次请求所使用的那条模型 / Pipeline 序列。
注意:必须同时传递常规的 seldon-model 请求头与 x-seldon-route 请求头。
Caveat:被选中的模型是相同的,但并不保证命中相同的副本实例(replica)。因此在当前实现下,该特性不适用于需要命中同一模型副本实例的有状态(stateful)模型。
该特性在 本地实验 notebook 中有完整演示。
5.7 服务网格(Service Mesh)方案
作为替代方案,如果使用了支持基于请求头路由做流量切分的流行服务网格(如 Istio、Ambassador、Traefik),也可以选择在服务网格层面运行实验。进一步讨论见 docs-gb/kubernetes/service-meshes/README.md。
六、Pipeline:连接模型间的数据流
Pipeline 允许将经过 Model 组件转换后的推理数据流连接起来。可以定义一个有向无环图(DAG),将多个模型以步骤(step)的方式串联。每个 Model 都需要能够接收 V2 推理请求并返回 V2 推理响应。
6.1 DAG 与 tensorMap:模型无法直接串联时的张量重命名
以 samples/pipelines/tfsimples-join.yaml 为例:
apiVersion: mlops.seldon.io/v1alpha1
kind: Pipeline
metadata:
name: join
spec:
steps:
- name: tfsimple1
- name: tfsimple2
- name: tfsimple3
inputs:
- tfsimple1.outputs.OUTPUT0
- tfsimple2.outputs.OUTPUT1
tensorMap:
tfsimple1.outputs.OUTPUT0: INPUT0
tfsimple2.outputs.OUTPUT1: INPUT1
output:
steps:
- tfsimple3
该 steps 列表包含三个模型:tfsimple1、tfsimple2 和 tfsimple3。三个模型各接收两个整型张量 INPUT0 与 INPUT1,并产生两个输出 OUTPUT0(输入之和)与 OUTPUT1(第二个输入减第一个输入)。
tfsimple1与tfsimple2直接接收 Pipeline 的输入(未显式定义inputs时的默认假设);tfsimple3从tfsimple1与tfsimple2的输出中各取一个 V2 张量作为输入;- 由于
tfsimple1/tfsimple2的输出张量名为OUTPUT0/OUTPUT1,需要重命名为目标模型期望的输入张量名,这一步通过tensorMap完成(例如tfsimple1.outputs.OUTPUT0 → INPUT0); - Pipeline 的输出为
tfsimple3模型的输出。
只有当模型无法直接串联时才需要 tensorMap 做张量重命名。
6.2 循环 Pipeline(Cyclic Pipelines)与防死循环机制
Seldon Core 2 支持循环 Pipeline,允许在推理图中构建反馈回路(feedback loop)。但循环 Pipeline 必须谨慎使用,错误的配置可能导致非预期行为。
风险点:可能发生跨迭代消息的 join——例如某次迭代 t 的消息,在 join 窗口内与来自 t-1, t-2, ..., 1 的消息被 Kafka Streams 跨迭代 join,从而触发非预期的消息传播。
为应对这一问题,Seldon Core 2 提供了两个机制:
allowCycles:在 Pipeline manifest 中设置allowCycles: true以启用循环;maxStepRevisits:限制单次 Pipeline 执行中某个步骤被重访(revisit)的最大次数。一旦达到上限,Pipeline 执行将终止并返回错误。该特性对循环 Pipeline(例如由 LLM 决定控制流的 Agentic 工作流中可能出现无限循环)尤其有用,可防止非预期死循环。默认值为 0(即不允许循环),可根据使用场景调整。
启用示例:
apiVersion: mlops.seldon.io/v1alpha1
kind: Pipeline
metadata:
name: pipeline
spec:
allowCycles: true
maxStepRevisits: 100
...
6.3 Pipeline 详细 Go 规范
type PipelineSpec struct {
// External inputs to this pipeline, optional
Input *PipelineInput `json:"input,omitempty"`
// The steps of this inference graph pipeline
Steps []PipelineStep `json:"steps"`
// Synchronous output from this pipeline, optional
Output *PipelineOutput `json:"output,omitempty"`
// Dataflow specs
Dataflow *DataflowSpec `json:"dataflow,omitempty"`
// Allow cyclic pipeline
AllowCycles bool `json:"allowCycles,omitempty"`
// Maximum number of times a step can be revisited
MaxStepRevisits uint32 `json:"maxStepRevisits,omitempty"`
}
type DataflowSpec struct {
// Flag to indicate whether the kafka input/output topics
// should be cleaned up when the model is deleted
// Default false
CleanTopicsOnDelete bool `json:"cleanTopicsOnDelete,omitempty"`
}
// +kubebuilder:validation:Enum=inner;outer;any
type JoinType string
const (
// data must be available from all inputs
JoinTypeInner JoinType = "inner"
// data will include any data from any inputs at end of window
JoinTypeOuter JoinType = "outer"
// first data input that arrives will be forwarded
JoinTypeAny JoinType = "any"
)
type PipelineStep struct {
// Name of the step
Name string `json:"name"`
// Previous step to receive data from
Inputs []string `json:"inputs,omitempty"`
// msecs to wait for messages from multiple inputs to arrive before joining the inputs
JoinWindowMs *uint32 `json:"joinWindowMs,omitempty"`
// Map of tensor name conversions to use e.g. output1 -> input1
TensorMap map[string]string `json:"tensorMap,omitempty"`
// Triggers required to activate step
Triggers []string `json:"triggers,omitempty"`
// +kubebuilder:default=inner
InputsJoinType *JoinType `json:"inputsJoinType,omitempty"`
TriggersJoinType *JoinType `json:"triggersJoinType,omitempty"`
// Batch size of request required before data will be sent to this step
Batch *PipelineBatch `json:"batch,omitempty"`
}
type PipelineBatch struct {
Size *uint32 `json:"size,omitempty"`
WindowMs *uint32 `json:"windowMs,omitempty"`
Rolling bool `json:"rolling,omitempty"`
}
type PipelineInput struct {
// Previous external pipeline steps to receive data from
ExternalInputs []string `json:"externalInputs,omitempty"`
// Triggers required to activate inputs
ExternalTriggers []string `json:"externalTriggers,omitempty"`
// msecs to wait for messages from multiple inputs to arrive before joining the inputs
JoinWindowMs *uint32 `json:"joinWindowMs,omitempty"`
// +kubebuilder:default=inner
JoinType *JoinType `json:"joinType,omitempty"`
// +kubebuilder:default=inner
TriggersJoinType *JoinType `json:"triggersJoinType,omitempty"`
// Map of tensor name conversions to use e.g. output1 -> input1
TensorMap map[string]string `json:"tensorMap,omitempty"`
}
type PipelineOutput struct {
// Previous step to receive data from
Steps []string `json:"steps,omitempty"`
// msecs to wait for messages from multiple inputs to arrive before joining the inputs
JoinWindowMs uint32 `json:"joinWindowMs,omitempty"`
// +kubebuilder:default=inner
StepsJoin *JoinType `json:"stepsJoin,omitempty"`
// Map of tensor name conversions to use e.g. output1 -> input1
TensorMap map[string]string `json:"tensorMap,omitempty"`
}
关键字段速览:
- Join 类型(
JoinType):inner表示必须从所有输入都拿到数据才能 join;outer表示在窗口结束时汇总任意输入的数据;any表示首个到达的输入数据被直接转发。默认值为inner。 joinWindowMs:等待多个输入消息到达后执行 join 的毫秒窗口。triggers/triggerJoinType:步骤激活所需的触发条件及其 join 语义。batch:数据在发送到本步骤前所需累积的请求批次(size、windowMs、rolling)。dataflow.cleanTopicsOnDelete:模型删除时是否清理对应的 Kafka 输入/输出主题,默认false。
七、高级定制:SeldonConfig 与 ServerConfig
官方文档将这两类资源明确标注为高级用法:SeldonConfig 用于定义 Seldon 在每个命名空间中的安装方式,ServerConfig 用于定义新的推理服务器类型。官方建议:核心运维(DevOps)人员使用默认的 SeldonConfig 或在其基础上定制;各命名空间的独立安装则通过 SeldonRuntime 配合少量 overrides 完成特殊定制。
7.1 SeldonConfig:定义核心安装组件
SeldonConfig 定义了 Seldon 安装的核心组件:每个核心组件的 PodSpec,以及通用配置段——包括为 Agent(rclone 默认值)、Kafka 与 Tracing(OpenTelemetry)创建的 ConfigMap。
type SeldonConfigSpec struct {
Components []*ComponentDefn `json:"components,omitempty"`
Config SeldonConfiguration `json:"config,omitempty"`
}
type SeldonConfiguration struct {
TracingConfig TracingConfig `json:"tracingConfig,omitempty"`
KafkaConfig KafkaConfig `json:"kafkaConfig,omitempty"`
AgentConfig AgentConfiguration `json:"agentConfig,omitempty"`
ServiceConfig ServiceConfig `json:"serviceConfig,omitempty"`
}
type ServiceConfig struct {
GrpcServicePrefix string `json:"grpcServicePrefix,omitempty"`
ServiceType v1.ServiceType `json:"serviceType,omitempty"`
}
type KafkaConfig struct {
BootstrapServers string `json:"bootstrap.servers,omitempty"`
ConsumerGroupIdPrefix string `json:"consumerGroupIdPrefix,omitempty"`
Debug string `json:"debug,omitempty"`
Consumer map[string]intstr.IntOrString `json:"consumer,omitempty"`
Producer map[string]intstr.IntOrString `json:"producer,omitempty"`
Streams map[string]intstr.IntOrString `json:"streams,omitempty"`
TopicPrefix string `json:"topicPrefix,omitempty"`
}
type AgentConfiguration struct {
Rclone RcloneConfiguration `json:"rclone,omitempty" yaml:"rclone,omitempty"`
}
type RcloneConfiguration struct {
ConfigSecrets []string `json:"config_secrets,omitempty" yaml:"config_secrets,omitempty"`
Config []string `json:"config,omitempty" yaml:"config,omitempty"`
}
type TracingConfig struct {
Disable bool `json:"disable,omitempty"`
OtelExporterEndpoint string `json:"otelExporterEndpoint,omitempty"`
OtelExporterProtocol string `json:"otelExporterProtocol,omitempty"`
Ratio string `json:"ratio,omitempty"`
}
type ComponentDefn struct {
Name string `json:"name"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
PodSpec *v1.PodSpec `json:"podSpec,omitempty"`
VolumeClaimTemplates []PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
}
组件级 Labels 与 Annotations:可以在 ComponentDefn 级别设置,它们会与 SeldonConfig 中定义的 labels/annotations 合并,并附加到该组件对应的 Deployment 或 StatefulSet 上。某些配置项还可以通过 SeldonRuntime 按命名空间覆盖。
仓库默认的 SeldonConfig(名为 default,见 operator/config/seldonconfigs/default.yaml)定义了 6 个核心组件及其默认副本数与资源:
| 组件名 | 默认副本 | 默认资源请求 | 主要职责 |
|---|---|---|---|
seldon-dataflow-engine |
1 | cpu 100m / memory 1G | Kafka Streams 数据流引擎,连接 Pipeline 步骤(镜像 seldonio/seldon-dataflow-engine:latest) |
seldon-envoy |
1 | cpu 100m / memory 128Mi | Envoy 边缘代理,承担外部流量入口(端口 9000/9003,带 Prometheus 注解) |
hodometer |
1 | cpu 1m / memory 32Mi | 用量计量上报组件(上报至 http://hodometer.seldon.io) |
seldon-modelgateway |
1 | cpu 100m / memory 1G | 模型网关,为模型负载提供入口(含 MODELGATEWAY_MAX_NUM_CONSUMERS=100 等环境变量) |
seldon-pipelinegateway |
1 | cpu 100m / memory 1G | Pipeline 网关,暴露 HTTP/9010、gRPC/9011、metrics/9006 端口 |
seldon-scheduler |
1 | cpu 100m / memory 1G | 调度器,管理模型/服务器/自动扩缩容,含 1G 的 scheduler-state 持久卷 |
7.2 ServerConfig:定义推理服务器类型
ServerConfig 定义了如何创建一台推理服务器。默认提供两个:Seldon MLServer 与 NVIDIA Triton Inference Server。两者都支持 Open Inference Protocol(所有推理服务器的硬性要求)。它们定义了 Kubernetes 工作负载的完整形态,其中包含 Seldon Agent 反向代理以及用于为服务器下载工件的 Rclone 服务器。
以 MLServer 的 ServerConfig 为例(operator/config/serverconfigs/mlserver.yaml),Pod 由三个容器组成:
- rclone:以
rcd --rc-no-auth --config=/rclone/rclone.conf --rc-addr=0.0.0.0:5572启动,负责从storageUri下载模型工件,并将模型目录(mlserver-modelsPVC,默认 1Gi)挂载到/mnt/agent。 - agent:Seldon Agent 反向代理。通过
SELDON_SERVER_CAPABILITIES=mlserver,alibi-detect,alibi-explain,huggingface,lightgbm,mlflow,python,sklearn,spark-mlib,xgboost声明能力,通过SELDON_OVERCOMMIT_PERCENTAGE=10、SELDON_MODEL_INFERENCE_LAG_THRESHOLD=30、SELDON_MODEL_INACTIVE_SECONDS_THRESHOLD=600、SELDON_SCALING_STATS_PERIOD_SECONDS=20等环境变量参与模型调度与自动扩缩容统计,并通过SELDON_SERVER_HTTP_PORT=9000、SELDON_REVERSE_PROXY_HTTP_PORT=9001等端口把请求转发给 MLServer。 - mlserver:实际推理容器,使用
MLSERVER_HTTP_PORT=9000、MLSERVER_GRPC_PORT=9500、MLSERVER_MODELS_DIR=/mnt/agent/models等环境变量,模型目录与 agent/rclone 共享mlserver-models卷。
Triton 的 ServerConfig(operator/config/serverconfigs/triton.yaml)结构一致,能力标签为 triton,dali,fil,onnx,openvino,python,pytorch,tensorflow,tensorrt,并额外声明了 /dev/shm(dshm,256Mi 内存型 emptyDir)卷以支持 Triton 的共享内存需求。更复杂的自定义场景(或需要将 Server 定义标准化)可参考这两个文件自行创建 ServerConfig。
八、结语:一张图理解资源之间的协作关系
至此,Seldon Core 2 的 Kubernetes 自定义资源体系已经清晰:
SeldonConfig是安装蓝本(默认default),SeldonRuntime按命名空间实例化它并允许按组件覆盖;ServerConfig是服务器类型蓝本(默认mlserver与triton),Server实例化一组服务器副本,并可通过extraCapabilities与podSpec做定制;Model声明工件(storageUri+requirements),由调度器匹配到具备相应能力标签的Server上;Pipeline以 DAG 把多个Model的数据流串起来(支持tensorMap重命名、join 窗口、批处理与循环图);Experiment在多个 Model 或 Pipeline 之间做流量切分(candidates / default / mirror),并支持 sticky session 与镜像流量。
所有资源的 CRD 定义可查阅 operator/config/crd/bases,Go 类型定义位于 operator/apis/mlops/v1alpha1,配套的可运行样例则分布在 samples/models、samples/servers、samples/pipelines 与 samples/experiments 中,可作为你编写自定义配置的直接参考模板。