Sub topic 2
Loading models from storage with storageUri
This chapter assumes you know Kubernetes pods, Deployments, and namespaces, can apply YAML with kubectl, can call an HTTP API with curl, and have KServe installed in your cluster.
Loading a model from storage
KServe reads model files from object storage when the pod starts. You set the location with storageUri. A storage initializer, which is an init container that runs before the model server, downloads the files into the pod.
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelThe modelFormat field tells KServe which serving runtime can load the model.
Calling the endpoint: V1 and V2 protocols
KServe supports two inference protocols over HTTP. V1 takes an instances list and returns predictions. V2, also called the Open Inference protocol, sends named input tensors under inputs.
| Protocol | Predict path | Request body |
|---|---|---|
| V1 | /v1/models/NAME:predict | `instances` list |
| V2 | /v2/models/NAME/infer | `inputs` list of named tensors |
This V1 call sends two rows to the Iris model:
cat <<'EOF' > iris-input.json
{"instances": [[6.8, 2.8, 4.8, 1.4], [6.0, 3.4, 4.5, 1.6]]}
EOF
curl -H "Host: ${SERVICE_HOSTNAME}" -H "Content-Type: application/json" \
http://${INGRESS_HOST}:${INGRESS_PORT}/v1/models/sklearn-iris:predict \
-d @./iris-input.jsonChoosing a deployment mode
Serverless mode uses Knative. It supports scale to zero and canary traffic splits. RawDeployment creates plain Kubernetes Deployments and Services. It is simpler and works without Knative, but it cannot scale to zero. Select the mode with the annotation serving.kserve.io/deploymentMode: RawDeployment on the InferenceService.
Autoscaling and scale to zero
Set minReplicas and maxReplicas to bound the replica count. With scaleMetric: concurrency and scaleTarget: 10, KServe adds replicas when each one handles more than 10 concurrent requests. Setting minReplicas: 0 allows scale to zero in Serverless mode. The first request after an idle period waits for a pod to start.
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
minReplicas: 0
maxReplicas: 5
scaleMetric: concurrency
scaleTarget: 10
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelExercise
Exercise
You need a service that must answer requests within a second at any time, and you cannot run Knative. Which deployment mode do you pick, and can you set minReplicas to 0?
Show answer
Pick RawDeployment, since it does not need Knative. Do not set minReplicas to 0, because scale to zero is available only in Serverless mode, and a zero-replica service would add a cold start delay to the first request. Keep minReplicas at 1 or higher.