Sub topic 1
Why serving models needs more than a Deployment
A trained model is a file. To be useful, other systems must call it over the network and get predictions back quickly and reliably. Kubernetes already runs network services, so a plain Deployment looks like a natural fit. It works for a first version. This chapter shows where it stops working, and those gaps explain what KServe is for.
Prerequisites
This chapter assumes you already know:
- Kubernetes basics: pods, Deployments, Services, and namespaces.
- How to read and apply YAML manifests with
kubectl. - How to train and save a machine learning model, for example a scikit-learn model written to disk.
- Basic HTTP and JSON, such as calling an API with
curl.
You do not need a cluster for this chapter. A cluster helps if you want to try the manifest below.
What "serving" means
Model serving is the work of keeping a trained model loaded in a process that answers prediction requests. A serving process has these jobs:
- Fetch the model artifacts from storage.
- Load them into memory, and onto a GPU if one is used.
- Expose an endpoint that accepts input and returns predictions.
- Report when it is ready, so no traffic arrives before loading finishes.
- Handle many concurrent requests without running out of memory or time.
Training is a batch job that you run and discard. Serving is a long-running service with latency targets, and it needs the same operations as any production service: scaling, rollouts, monitoring, and rollback.
The plain Deployment approach
The most direct approach is to write a small web server, bake the model into a container image, and run it under a Deployment. A slightly better version downloads the model at startup with an init container, so the image does not change when the model does.
The manifest below does that. The image registry.example.com/iris-server:1.0 is a placeholder for a server you would write yourself. The model location is a placeholder too.
apiVersion: apps/v1
kind: Deployment
metadata:
name: iris-server
spec:
replicas: 2
selector:
matchLabels:
app: iris-server
template:
metadata:
labels:
app: iris-server
spec:
volumes:
- name: model
emptyDir: {}
initContainers:
- name: fetch-model
image: google/cloud-sdk:slim
command: ["gsutil", "cp", "-r", "gs://example-bucket/models/iris/v1/*", "/models"]
volumeMounts:
- name: model
mountPath: /models
containers:
- name: server
image: registry.example.com/iris-server:1.0
ports:
- containerPort: 8080
volumeMounts:
- name: model
mountPath: /models
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
resources:
requests:
cpu: "1"
memory: 2Gi
limits:
memory: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: iris-server
spec:
selector:
app: iris-server
ports:
- port: 80
targetPort: 8080This manifest is valid Kubernetes, and for one model and a small team it can be enough. Each part of it is also something you wrote and now maintain. The next sections show what a team ends up adding.
What a Deployment does not give you
The table compares what model serving needs with what a Deployment and Service provide.
| Need | Plain Deployment | What you would build yourself |
|---|---|---|
| Model loading from storage | Init container or baked-in image | Download scripts, credentials, caching, checksum checks |
| Server per model type | One image per framework | A server for scikit-learn, one for XGBoost, one for PyTorch, and so on |
| Request format | Whatever your server defines | A stable API contract that clients can rely on across models |
| Scaling on load | CPU-based HorizontalPodAutoscaler (HPA) | Custom metrics for in-flight requests; no scale to zero by default |
| Safe model upgrades | Rolling update by pod count | Traffic splitting, a promotion step, and a rollback procedure |
| Pre- and post-processing | Inside the same server code | Separate components that scale on their own |
| Metrics and logs | Whatever you instrument | Request counts, latency, and payload logging for every model |
Four of these rows deserve a closer look.
One server per model, and one API per server
If every team writes its own server, each model ends up with a different URL scheme, input shape, and error behavior. A client that calls a scikit-learn service cannot call a PyTorch service without code changes. Serving frameworks solve this with a shared protocol and a set of ready-made servers keyed on model format. Later chapters cover the protocols and the runtime mapping.
Readiness depends on the model, not the process
A container is "started" long before a large model is loaded. If the readiness probe only checks that a port is open, Kubernetes sends traffic to a pod that cannot answer yet, and clients see errors during every rollout and scale-up. The probe must report ready only after the model is in memory. Model loading can take minutes for large artifacts, so the probe delay and timeouts must match the real load time.
CPU is a poor scaling signal
The HPA scales on CPU or memory by default. A model server spends much of its time waiting on a queue or a GPU, so CPU may look low while requests pile up and latency grows. A better signal is the number of requests in flight per replica, which is concurrency, or requests per second. You can feed these into an HPA through custom metrics, but that needs a metrics adapter and careful tuning.
The HPA also does not scale to zero in a default cluster. Scaling a Deployment from one replica to none, and back on the first request, needs a component that holds requests while a pod starts. For models used rarely, such as nightly or per-customer models, idle replicas waste memory and GPUs.
Rolling updates are not canary rollouts
A Deployment's rolling update replaces pods gradually. This looks like a traffic split but is not one. You cannot say "send 10 percent of requests to the new model and watch the error rate." The split follows replica counts and readiness. Rolling back means editing the manifest again and waiting for another rollout.
A new model version can be healthy as a process yet wrong as a predictor. It can return valid JSON with poor predictions. You need a small, controlled share of traffic first, and a way to return to the previous version at once.
Common errors with the plain approach
These problems appear often when teams move from a notebook to a Deployment.
- Traffic arrives before the model loads. The probe checks the port, not the model. Fix the probe so it tests a path that returns success only after loading.
- Out-of-memory kills. The memory limit covers the model plus working memory per request. A pod that fits at idle can be killed under concurrent load. Set
requestsequal tolimitsfor memory and load test with realistic payloads. - A new model ships inside the image. Every model change then needs an image build and a push. Keeping the model in object storage and fetching it at startup separates the two release cycles.
- Credentials are copied into the pod spec. Storage access should come from a service account or a Secret that is referenced, not pasted into environment variables in the manifest.
- Silent version drift. With
latesttags or a mutable storage path, two replicas can run different models after a restart. Use immutable versions in paths and tags.
You can check the first problem yourself during a rollout:
kubectl rollout status deployment/iris-server
kubectl get pods -l app=iris-server -w
kubectl logs deployment/iris-server -c server --tail=20If pods show Running with READY 1/1 before the logs say the model has loaded, the readiness probe is not tied to loading.
Where KServe fits
KServe is a Kubernetes add-on that supplies the missing pieces as one resource type. You declare a model, its format, and where it lives. KServe then creates the pods, the model download, the endpoint, scaling rules, and traffic routing for you. It standardizes the request protocol and provides ready-made servers, so a client calls every model in the same way. It also supports concurrency-based autoscaling, scale to zero, and percentage-based canary rollouts.
The diagram shows the shift in responsibility. Everything in the left column is code and configuration your team owns with a plain Deployment. In the right column, KServe owns most of it.
KServe does not remove the need to understand Kubernetes. It adds its own components to install and operate, and it has behaviors you must learn, such as its deployment modes and how revisions work. It pays off when you serve more than a model or two, when you need scale to zero or traffic splitting, or when many teams must call models consistently. For a single stable model with steady traffic, a Deployment can remain the simpler choice.
The next chapter looks at how KServe is put together, so you know what is running in your cluster once you install it.
Exercise
A model server's pods show READY 1/1 as soon as the container starts, but the first requests after each rollout fail for about 90 seconds. Name the likely cause and the fix.
Show answer
The readiness probe checks only that the port is open, so Kubernetes routes traffic before the model finishes loading. Change the probe to call an endpoint that returns success only once the model is loaded, and set the probe's initial delay and failure threshold to cover the real load time.
Exercise
Your service has low CPU use but rising latency under load, and the HPA never adds replicas. Why, and what signal would work better?
Show answer
The server waits on a queue or accelerator, so CPU stays low even when requests pile up. Scaling on in-flight requests per replica (concurrency) or requests per second reflects load more accurately than CPU.
Exercise
Why is a Deployment's rolling update not equivalent to sending 10 percent of traffic to a new model version?
Show answer
A rolling update replaces pods by count and readiness, so the traffic share follows replica numbers and you cannot set an exact percentage or observe the new version on a small slice before promoting it. A canary rollout fixes a percentage of requests for the new version and keeps the rest on the old one.