Sub topic 3
Your first InferenceService
An InferenceService is the KServe resource that turns a stored model into a running HTTP endpoint. This chapter walks through writing one, applying it, reading its status, and sending a request.
This chapter assumes you can read Kubernetes YAML and use kubectl. It also assumes KServe is already installed in your cluster, and that you can call an HTTP API with curl. The previous chapter covered the KServe components. Here you use them.
What an InferenceService is
An InferenceService is a custom resource in the API group serving.kserve.io/v1beta1. You describe the model you want served. The KServe controller creates the Kubernetes objects needed to serve it. These include a Deployment or Knative Service, networking, and autoscaling, depending on the deployment mode covered in a later chapter.
The spec has up to three components:
| Component | Required | Purpose |
|---|---|---|
| `predictor` | Yes | Loads the model and answers inference requests |
| `transformer` | No | Pre- and post-processing around the predictor |
| `explainer` | No | Produces explanations for predictions |
This chapter uses only the predictor.
The minimal manifest
Save this as sklearn-iris.yaml:
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/modelEach field has a job:
metadata.namebecomes part of the service's URL and the model name in request paths.predictor.modelis the field to use when you want KServe to pick a server for you. You do not name a container image.modelFormat.namestates what kind of model artifact this is. KServe matches it against the available serving runtimes and picks a server that supportssklearn.storageUritells KServe where the model files are. At startup, an init container downloads them into the pod.
The model format and the storage location are separate inputs. KServe combines them, so you never write a Dockerfile for a standard model type.
What happens when you apply it
The sequence is:
- The API server stores the resource.
- The controller finds a matching serving runtime for
sklearn. - The controller creates the objects that run the pod and route traffic to it.
- The pod starts. The
storage-initializerinit container copies the model fromstorageUriinto a shared volume. - The model server container, usually named
kserve-container, starts and loads the model. - Once the server reports ready, the InferenceService becomes
Ready.
In Serverless mode the pod also contains a queue-proxy sidecar, which measures request concurrency for autoscaling.
Deploying and checking status
Apply the manifest and wait for readiness. These commands also send the first request:
kubectl apply -f sklearn-iris.yaml
kubectl get inferenceservice sklearn-iris
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.jsonRun the first two commands before you set the variables used by curl. The kubectl get output looks roughly like this once the service is up:
NAME URL READY PREV LATEST AGE
sklearn-iris http://sklearn-iris.default.example.com True 100 2mThe first deployment can take a minute or more. The image may need to be pulled, and the model has to download. READY shows False or Unknown until the predictor is up.
Setting the request variables
The curl command needs three values:
SERVICE_HOSTNAMEis the host name KServe assigned to the service. Take it fromstatus.url.INGRESS_HOSTandINGRESS_PORTlocate the cluster ingress.
The ingress lookup depends on your setup. With an Istio ingress gateway that has a load balancer, it looks like this:
SERVICE_HOSTNAME=$(kubectl get inferenceservice sklearn-iris \
-o jsonpath='{.status.url}' | cut -d "/" -f 3)
INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}')
INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway \
-o jsonpath='{.spec.ports[?(@.name=="http2")].port}')On a local cluster without a load balancer, port-forward the gateway instead. Then use INGRESS_HOST=localhost and INGRESS_PORT=8080:
kubectl -n istio-system port-forward svc/istio-ingressgateway 8080:80Your cluster may use a different ingress, such as Gateway API or Kourier. In that case, find its service and address the same way.
Reading the response
The request uses the V1 protocol, which takes a JSON object with an instances list. Each element is one input row. The server returns one prediction per row:
{"predictions": [1, 1]}For the iris model, each number is a class index. The later chapter on protocols covers V1 and V2 in detail.
The host name goes in the Host header because the ingress routes by host. You send the request to the ingress address, and the header tells it which service you mean.
Reading the status in more detail
kubectl get shows a summary. For the reason behind a state, describe the resource:
kubectl describe inferenceservice sklearn-iris
kubectl get inferenceservice sklearn-iris -o jsonpath='{.status.conditions}'The conditions you will see most often:
| Condition | Meaning |
|---|---|
| `PredictorReady` | The predictor pod is running and passing readiness checks |
| `IngressReady` | Routing to the service is set up |
| `Ready` | All required conditions are true |
If PredictorReady is false, look at the pod, not the InferenceService. KServe labels pods with the service name:
kubectl get pods -l serving.kserve.io/inferenceservice=sklearn-iris
kubectl logs <pod-name> -c storage-initializer
kubectl logs <pod-name> -c kserve-containerThe storage initializer is an init container, so you read its logs with -c storage-initializer. This works even after the main container has started.
Common errors
| Symptom | Likely cause | What to check |
|---|---|---|
| `READY` stays `False`, pod in `Init:Error` or `Init:CrashLoopBackOff` | The model could not be downloaded: wrong `storageUri`, missing credentials, or no network access | `kubectl logs <pod> -c storage-initializer` |
| Error that no runtime supports the model format | `modelFormat.name` is misspelled, or no installed runtime supports it | `kubectl get clusterservingruntimes` and `kubectl get servingruntimes` |
| `kserve-container` crashes after the init step | The artifact does not match what the server expects, such as a wrong file name or an incompatible library version | `kubectl logs <pod> -c kserve-container` |
| `curl` returns 404 | The `Host` header is missing or wrong | Compare it with `status.url` |
| `curl` cannot connect | Wrong `INGRESS_HOST` or `INGRESS_PORT` | Check the ingress service address and your port-forward |
| Request returns a 400-level error | The JSON body does not match the protocol | Confirm that the body has an `instances` key and that rows have the right shape |
Choosing the runtime explicitly
Auto-selection is convenient. It also means the server version depends on what is installed in your cluster. To pin it, name the runtime in the spec. The runtime must exist in the cluster, and its supported formats must include your model format:
spec:
predictor:
model:
modelFormat:
name: sklearn
runtime: kserve-sklearnserver
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelRuntime names depend on your KServe version. List the actual names with kubectl get clusterservingruntimes. The next chapters cover runtimes and storage in depth.
Cleaning up
Deleting the InferenceService removes the objects the controller created for it:
kubectl delete inferenceservice sklearn-irisYou do not need to delete the pods or routes yourself.
Summary of the workflow
- Write a manifest with a
predictor.modelblock that gives the format andstorageUri. - Apply it and wait for
READYto beTrue. - Get the host name from
status.url, and the ingress address from your cluster. - Send a request with the
Hostheader and a V1 JSON body. - When something fails, go from the InferenceService conditions to the pod, then to the container logs.
Exercise
You apply an InferenceService. After five minutes, kubectl get inferenceservice shows READY as False. The pod is in Init:CrashLoopBackOff. Which container's logs do you read first, and what are two likely causes?
Show answer
Read the logs of the storage-initializer init container with kubectl logs <pod> -c storage-initializer. The model server has not started yet, so kserve-container has no useful logs. Likely causes are a wrong storageUri and missing or invalid storage credentials. A network restriction that blocks access to the storage is another.
Exercise
A curl request to http://${INGRESS_HOST}:${INGRESS_PORT}/v1/models/sklearn-iris:predict returns 404, but kubectl get inferenceservice shows READY as True. What is the most likely problem and how do you fix it?
Show answer
The Host header is probably missing or wrong. The ingress routes by host name, so the request must carry the host from status.url. Set SERVICE_HOSTNAME with kubectl get inferenceservice sklearn-iris -o jsonpath='{.status.url}' | cut -d "/" -f 3 and pass it as -H "Host: ${SERVICE_HOSTNAME}".