Sub topic 4
Serving runtimes: how a model format maps to a server
This chapter explains how KServe decides which container image serves your model. It assumes you can read an InferenceService manifest (the previous chapter), use kubectl get, describe and apply, and know what a container image and its command-line arguments are.
What a serving runtime is
A model file such as model.joblib or model.onnx is only data. Something has to load it, expose HTTP or gRPC endpoints, and answer prediction requests. In KServe, that something is a container image described by a serving runtime.
A serving runtime is a Kubernetes custom resource. It records three things:
- Which model formats the server understands, such as
sklearn,xgboost,onnxorhuggingface. - Which container image and arguments start the server.
- Which inference protocols the server speaks.
When you write modelFormat: sklearn in an InferenceService, you are not naming an image. You are naming a format. KServe looks through the available runtimes and picks one that declares support for it. This indirection lets platform teams control images, resource defaults and versions in one place, while users only say what kind of model they have.
Two scopes: ServingRuntime and ClusterServingRuntime
The two resource kinds have the same schema and differ only in scope.
| Kind | Scope | Typical use |
|---|---|---|
| `ClusterServingRuntime` | Whole cluster | Defaults shipped with KServe, such as the scikit-learn, XGBoost and Triton runtimes |
| `ServingRuntime` | One namespace | A team's own variant, or a runtime that only one project needs |
List both kinds with these commands:
kubectl get clusterservingruntimes
kubectl get servingruntimes -n my-namespaceA namespaced runtime is visible only to InferenceServices in that namespace. If a service cannot find a runtime you created, check the namespace first.
Anatomy of a runtime
The manifest below shows a simplified scikit-learn runtime. It follows the shape of the one KServe ships, but treat the image tag and resource numbers as examples.
apiVersion: serving.kserve.io/v1alpha1
kind: ClusterServingRuntime
metadata:
name: kserve-sklearnserver
spec:
supportedModelFormats:
- name: sklearn
version: "1"
autoSelect: true
priority: 1
protocolVersions:
- v1
- v2
containers:
- name: kserve-container
image: kserve/sklearnserver:v0.13.0
args:
- --model_name={{.Name}}
- --model_dir=/mnt/models
- --http_port=8080
resources:
requests:
cpu: "1"
memory: 2Gi
limits:
cpu: "1"
memory: 2GiThe fields that matter most:
supportedModelFormatsis a list of format entries. Each has aname, an optionalversion, anautoSelectflag and an optionalpriority.protocolVersionslists the protocols the server supports,v1andv2here. A later chapter covers the protocols in detail.containersis the pod template for the server. The container is namedkserve-container, and KServe merges your InferenceService settings into it.{{.Name}}is a template placeholder that KServe fills with the InferenceService name./mnt/modelsis the conventional directory where downloaded model files appear. The storage initializer puts them there, as the next chapter explains. The server's--model_dirargument must point to the same path.
Other fields you may meet are multiModel (for multi-model serving), disabled (to take a runtime out of use without deleting it) and grpcEndpoint or grpcDataEndpoint.
How KServe picks a runtime
Runtime selection happens when you apply an InferenceService. There are two paths: automatic matching and explicit pinning.
Automatic matching
If you do not set runtime, KServe considers a runtime only when all of these hold:
- The runtime is not disabled.
- One of its
supportedModelFormatsentries has the samenameas yourmodelFormat.name. - If you set
modelFormat.version, the entry'sversionmatches it. - The entry has
autoSelect: true. - If you request a
protocolVersion, the runtime lists it inprotocolVersions.
If several runtimes qualify, KServe prefers the entry with the higher priority number. Set priorities deliberately when two runtimes claim the same format. Otherwise the winner is not something you should rely on.
Explicit pinning
Set runtime to the name of a runtime to skip matching:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
model:
modelFormat:
name: sklearn
runtime: my-sklearn-runtime
protocolVersion: v2
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelPinning is the right choice when you want a specific image, such as a patched build, or when the runtime has autoSelect: false. The pinned runtime must still list the model format, and it must support the protocol you request.
Writing your own runtime
You may want your own runtime to pin an image version, add environment variables, set resource defaults, or serve a format that KServe does not cover. A namespaced runtime for a hypothetical in-house image looks like this:
apiVersion: serving.kserve.io/v1alpha1
kind: ServingRuntime
metadata:
name: my-sklearn-runtime
namespace: my-namespace
spec:
supportedModelFormats:
- name: sklearn
version: "1"
autoSelect: false
protocolVersions:
- v2
containers:
- name: kserve-container
image: registry.example.com/ml/sklearnserver:1.4.2
args:
- --model_name={{.Name}}
- --model_dir=/mnt/models
- --http_port=8080
env:
- name: LOG_LEVEL
value: infoSetting autoSelect: false keeps this runtime out of automatic matching. Only services that name it in runtime use it, so applying it cannot change which image other services get.
Your server image must follow the contract KServe assumes. It must listen on the port given in its arguments, read the model from --model_dir, and implement the protocol you declared. The KServe Python SDK, covered in a later chapter, implements this contract for you.
Version matching
Versions in supportedModelFormats are labels you define, not semantic version ranges. A runtime entry with version: "1" matches an InferenceService that asks for version: "1" and does not match one that asks for "2". If the service omits version, the version check is skipped.
A common pattern is to ship one runtime per major framework version, for example one for pytorch version "1" and another for "2". Each points to a different image, and users pick by setting version.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Service never becomes ready. The status says no runtime was found to support the predictor's model type. | The format name is misspelled, the version has no match, or the matching runtime has `autoSelect: false`. | Compare `modelFormat` against `kubectl get clusterservingruntimes -o yaml`. Fix the name or version, or set `runtime` explicitly. |
| The pinned runtime is rejected. | The runtime does not list your format, or it does not support the requested `protocolVersion`. | Add the format or protocol to the runtime, or change the request. |
| A runtime you created is ignored. | It is a `ServingRuntime` in a different namespace from the service. | Create it in the service's namespace, or make it a `ClusterServingRuntime`. |
| The pod starts but crashes, and the logs say the model directory is empty or missing. | `--model_dir` does not match where the model is mounted, or `storageUri` points to the wrong path. | Use `/mnt/models` in the arguments and check `storageUri`. |
| After you edit a runtime, existing services still run the old image. | Pods are built when the service is reconciled, so running pods do not change automatically. | Trigger a new revision, for example by restarting the rollout or touching the InferenceService. |
When you debug, these commands show the real state:
kubectl describe inferenceservice sklearn-iris
kubectl get inferenceservice sklearn-iris -o jsonpath='{.status.conditions}'
kubectl logs deploy/sklearn-iris-predictor -c kserve-containerThe deployment name depends on your deployment mode, so list pods with kubectl get pods if the last command does not find it.
Choosing between a stock and a custom runtime
- Use the stock runtime when your framework is covered and its default image works. This is the least maintenance.
- Make a namespaced runtime to change images, environment variables or resources for one team.
- Make a cluster-wide runtime to give a whole platform a standard, versioned server.
- Write a custom predictor when no existing server can load your model or when you need custom logic in the request path.
Exercise
An InferenceService requests modelFormat.name: xgboost and does not set runtime. Two ClusterServingRuntimes list xgboost. One has autoSelect: true and priority: 1. The other has autoSelect: false and priority: 10. Which one is used, and how would you make the service use the other?
Show answer
The runtime with autoSelect: true is used. Automatic matching only considers entries with autoSelect: true, so the higher priority on the other runtime is never compared. To use the other runtime, set spec.predictor.model.runtime to its name in the InferenceService.
Exercise
Your service is stuck with a "no runtime found" status. The matching runtime exists as a ServingRuntime in the ml-team namespace, and the service is in default. What are two ways to fix this?
Show answer
Create the same runtime in the default namespace, or convert it to a ClusterServingRuntime so every namespace can see it. Moving the InferenceService into ml-team also works.