Sub topic 5
Loading models from storage with storageUri
This chapter assumes you can apply an InferenceService with kubectl, you know what an init container and a Kubernetes Secret are, and you have read the earlier chapters on the InferenceService and serving runtimes. It covers how a trained model gets from object storage into a running server pod, and what usually breaks along the way.
What storageUri does
A KServe model server image contains the serving code (for example the scikit-learn or XGBoost server) but not your model. The model lives elsewhere, and you point to it with the storageUri field:
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/modelKeeping the model out of the image has two benefits. You can ship a new model version by changing a URI, without building an image. And one runtime image serves many models.
The storage initializer
When KServe creates the predictor pod, it adds an init container named storage-initializer. This container runs before the model server starts. It does three things:
- It reads
storageUriand picks a download method based on the URI scheme. - It downloads the files into a shared volume.
- It exits. Kubernetes then starts the model server container,
kserve-container, with that volume mounted at/mnt/models.
The runtime is told to load from /mnt/models. You never write that path yourself.
Because the download happens in an init container, a pod whose download fails never becomes ready. The model server does not start at all.
Supported URI schemes
| Scheme | Source | Notes |
|---|---|---|
| `gs://bucket/path` | Google Cloud Storage | Public buckets work anonymously. Private buckets need a credentials Secret. |
| `s3://bucket/path` | Amazon S3 or S3-compatible stores such as MinIO | Needs credentials plus endpoint annotations for non-AWS stores. |
| `https://host/path/model.zip` | A web server | The URL must point to a `.zip` or `.tar.gz` archive, which is extracted. |
| `https://account.blob.core.windows.net/...` | Azure Blob Storage | Needs an Azure credentials Secret for private containers. |
| `pvc://claim-name/path` | A PersistentVolumeClaim | Mounted directly. Nothing is downloaded. |
Newer KServe releases add more sources, such as hf:// for Hugging Face Hub and oci:// for model images. Check the release notes for your installed version before relying on them.
Layout: the URI points to a directory
storageUri should name a directory, not a single file. The initializer copies the contents of that directory into /mnt/models. The runtime then looks for files with names it expects:
- The scikit-learn runtime looks for
model.joblib,model.pkl, ormodel.jbl. - The XGBoost runtime looks for
model.bst,model.json, ormodel.ubj. - TensorFlow Serving expects a numbered version directory containing a SavedModel.
- Triton expects a model repository, with one directory per model and a
config.pbtxtwhere required.
A file with the wrong name is the most common mistake. The download succeeds and the pod starts, but the model server then fails to load the model. Check the exact layout your runtime expects in its documentation.
# Upload a scikit-learn model so the runtime finds model.joblib
gsutil cp model.joblib gs://my-bucket/models/iris/v1/model.joblib
# Then use storageUri: gs://my-bucket/models/iris/v1Credentials for private storage
Public buckets need nothing extra. For private storage, KServe reads credentials from a Secret that is attached to a ServiceAccount. The InferenceService then runs under that ServiceAccount.
The following example is for S3-compatible storage such as MinIO. The annotations on the Secret tell the initializer how to reach the store.
apiVersion: v1
kind: Secret
metadata:
name: s3-creds
annotations:
serving.kserve.io/s3-endpoint: minio-service.kubeflow:9000
serving.kserve.io/s3-usehttps: "0"
serving.kserve.io/s3-region: us-east-1
type: Opaque
stringData:
AWS_ACCESS_KEY_ID: REPLACE_ME
AWS_SECRET_ACCESS_KEY: REPLACE_ME
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-models
secrets:
- name: s3-creds
---
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris-s3
spec:
predictor:
serviceAccountName: sa-models
model:
modelFormat:
name: sklearn
storageUri: s3://my-bucket/models/iris/v1Three details matter here:
- The Secret and ServiceAccount must be in the same namespace as the InferenceService.
serviceAccountNamegoes underspec.predictor, not at the top level ofspec.- The credentials in the example are placeholders. Do not commit real keys to Git. Create the Secret from a secret manager or with
kubectl create secret.
On GCP, the GCS Secret carries a service account key under the key name gcloud-application-credentials.json. On managed clusters you can often avoid long-lived keys by using workload identity (for example IAM roles for service accounts on EKS). In that case the pod's ServiceAccount is annotated for the cloud IAM role and no Secret is needed.
Loading from a PersistentVolumeClaim
If the model is already on a volume, use the pvc:// scheme:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris-pvc
spec:
predictor:
model:
modelFormat:
name: sklearn
storageUri: pvc://model-store/models/iris/v1The first path segment is the PVC name (model-store). The rest is a path inside the volume. KServe mounts the claim into the pod, so there is no download step and startup is fast, even for large files.
Points to watch:
- The PVC must be in the same namespace as the InferenceService.
- If the predictor can scale to several replicas on different nodes, the volume must support that. A
ReadWriteOncevolume can be attached to only one node, so extra replicas may stay Pending. Use aReadWriteManystorage class, or aReadOnlyManymode if the volume is only read. - Changing files in the volume does not reload a running model. Create a new revision, or restart the pods.
Cost at scale: every new pod downloads again
With object storage, each new pod runs its own initializer and downloads the full model again. This affects three situations:
- Autoscaling out adds pods that cannot serve traffic until their download finishes.
- Scale to zero makes the first request after idle wait for a pod start plus a download.
- Large models, such as LLM weights in the tens of gigabytes, can take minutes to fetch and need ephemeral disk space in the pod.
Ways to reduce the cost include storing the model in the same region as the cluster, using a PVC, using a node-local cache if your KServe version supports one, and keeping minReplicas at 1 or higher for latency-sensitive services. Also remember the init container's disk use: the downloaded files land in an emptyDir, which counts against node ephemeral storage and can trigger evictions when the model is large.
Troubleshooting a failing download
Start with the pod status, then read the init container logs:
kubectl get pods -l serving.kserve.io/inferenceservice=sklearn-iris
kubectl describe pod <pod-name>
kubectl logs <pod-name> -c storage-initializer| Symptom | Likely cause |
|---|---|
| `Init:Error` or `Init:CrashLoopBackOff` | The initializer failed. The logs show the reason, such as access denied or no such object. |
| Logs show `AccessDenied`, `403`, or `NoCredentialsError` | The Secret is missing, is not attached to the ServiceAccount, or the predictor does not use that ServiceAccount. |
| Logs show a connection or DNS error with S3-compatible storage | The `serving.kserve.io/s3-endpoint` annotation is wrong, or `s3-usehttps` does not match the server. |
| Init completes but `kserve-container` crashes | The files were downloaded but have the wrong names or format. Check `kubectl logs <pod-name> -c kserve-container`. |
| Pod stays `Pending` with a PVC | The claim does not exist in that namespace, or its access mode blocks scheduling. |
| `Init:0/1` for a long time | The download is large or slow. Check bandwidth, storage region, and ephemeral disk limits. |
When the cause is unclear, test access from inside the cluster using a temporary pod with the same credentials and the cloud CLI. That separates permission problems from KServe configuration problems.
Choosing between storage options
- Object storage (
gs://,s3://): best default. It is versioned by path, works with any cluster, and fits CI pipelines that write a model after training. You pay the download time on every pod start. - PVC: best when startup time matters and the volume is shared and read-only. It ties you to cluster storage and needs care with access modes.
- HTTPS archive: convenient for demos and public models. Avoid it for production because there is little access control or versioning.
- Model baked into a custom image: skips
storageUrientirely. It gives a self-contained artifact, but every model change needs an image build and push.
Exercise
An InferenceService uses storageUri: s3://models/iris/v1 on a MinIO server. The pod shows Init:CrashLoopBackOff, and kubectl logs <pod> -c storage-initializer shows a connection error to s3.amazonaws.com. What is the most likely cause, and what do you change?
Show answer
The initializer is contacting real AWS S3 instead of MinIO, so the endpoint is not configured. Add the serving.kserve.io/s3-endpoint annotation (for example minio-service.kubeflow:9000) and set serving.kserve.io/s3-usehttps to match the server, on the Secret holding the credentials. Make sure that Secret is listed under the ServiceAccount's secrets and that the InferenceService sets spec.predictor.serviceAccountName to that ServiceAccount.
Exercise
You upload my_model.joblib to gs://my-bucket/models/iris/v1/ and set storageUri: gs://my-bucket/models/iris/v1. The init container completes, but the model server fails to load the model. What do you check first, and why?
Show answer
Check the file name. The scikit-learn runtime looks in /mnt/models for model.joblib, model.pkl, or model.jbl. The download worked, but the runtime did not find a file with an expected name. Rename the file to model.joblib, then confirm the cause in the kserve-container logs.
Exercise
A service with minReplicas: 0 loads a 20 GB model from S3. Users complain about very slow first requests after idle periods. Name two changes that reduce this delay.
Show answer
Set minReplicas to 1 so a warm pod always exists, which removes the cold start. Alternatively, load the model from a pvc:// volume, which avoids the 20 GB download on each pod start. Placing the bucket in the cluster's region also shortens the download.