Day 3: The Embedding Service on Kubernetes
Goal: The embedding model moves from the host into the cluster: as a deployment with its own volume for the models, reachable through a service, ready immediately after every restart – and you measure what a CPU limit costs · Duration: approx. 90 min · Prerequisite: Day 2
State before this chapter
# on rag1
kubectl get nodesExpected: rag1 and rag2,
both Ready.
# on rag1
ls ~/kurs/01/karten-anfrage.jsonExpected: the file from Day 1.
Introduction
Until yesterday, Ollama ran as a service directly on rag1. That is enough for trying things out, but not for operations: nobody except rag1 can reach it, a crash is not caught by Kubernetes, and nobody limits how much CPU and memory it takes. Today the embedding service becomes a perfectly ordinary Kubernetes service – and along the way you get to know the three places where model services differ from a web server: models are large and belong on a volume, they have to be loaded before they are fast, and they devour CPU.
Today you learn:
- to run Ollama as a deployment with its own volume for the models,
- to call the service through a Service from another node,
- why a pod can be “ready” even though the first call takes nine seconds – and how a startup probe fixes that,
- what a CPU limit does to a model running on the CPU.
Background
The picture for today: the workshop moves into the main building
So far, the workshop with the classification system stood in a shed next to the main building: whoever needed it had to come by on rag1. Today it moves into a room of its own in the house (the pod). The heavy tools – the models – go into a tool cabinet that stays in place when the room is refurnished (the volume). At the door hangs a doorbell with a fixed name that works from every building (the Service). And an “open” sign only goes up on the door once the tool really lies on the table (the startup probe).
The terms
| Term | Meaning | in the picture |
|---|---|---|
| PersistentVolumeClaim (PVC) | request for storage that survives a restart of the pod | tool cabinet |
local-path |
k3s storage: a folder on the node | cabinet fixed in one building |
| Service | fixed name and fixed address for a group of pods | doorbell with name plate |
| Startup probe | check that Kubernetes repeats until the pod has started; only then do the other checks count | “open” sign |
| Readiness probe | check whether the pod may receive requests | doorbell is switched on |
requests / limits |
what a pod is guaranteed / may use at most | promised / maximum power supply |
OLLAMA_KEEP_ALIVE |
how long Ollama keeps a model loaded after the last request | tool stays on the table |
Remember: A model service is only ready when the model is in memory – not when the process answers. You have to close this gap yourself.
Drill 1: Making room and the manifests
Where you are: on rag1.
What you have afterwards: the host service switched off and four manifests in
~/kurs/03.
Step 1 – switch off the host service. Two Ollama instances with one loaded model each do not fit well into 8 GB. The service from the preparation goes off and no longer starts at boot:
# on rag1
sudo systemctl disable --now ollamaExpected:
Removed "/etc/systemd/system/multi-user.target.wants/ollama.service".
disable --now removes the autostart and stops the
service immediately. The models in /usr/share/ollama stay
where they are.
Step 2 – today’s folder.
# on rag1
mkdir -p ~/kurs/03# on rag1
cd ~/kurs/03Step 3 – the namespace.
# on rag1
nano namespace.yamlapiVersion: v1
kind: Namespace
metadata:
name: ragFrom today on, everything that belongs to the RAG system lives in the
namespace rag.
Step 4 – the tool cabinet.
# on rag1
nano pvc.yamlapiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: modelle
namespace: rag
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 10Gi| Setting | Meaning |
|---|---|
ReadWriteOnce |
one node may write |
storageClassName: local-path |
k3s creates a folder on the node where the pod lands |
storage: 10Gi |
room for several models (bge-m3 alone is 1.2 GB) |
Step 5 – the deployment.
# on rag1
nano deployment.yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: embed
namespace: rag
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: embed
template:
metadata:
labels:
app: embed
spec:
nodeSelector:
kubernetes.io/hostname: rag1
containers:
- name: ollama
image: ollama/ollama:0.40.19
env:
- name: OLLAMA_KEEP_ALIVE
value: "-1"
ports:
- containerPort: 11434
resources:
requests:
cpu: "2"
memory: 2Gi
limits:
memory: 3Gi
readinessProbe:
httpGet:
path: /api/version
port: 11434
periodSeconds: 5
volumeMounts:
- name: modelle
mountPath: /root/.ollama
volumes:
- name: modelle
persistentVolumeClaim:
claimName: modelle| Setting | Meaning |
|---|---|
strategy: Recreate |
stop the old pod first, then start the new one – the RWO volume cannot be attached to two pods |
nodeSelector: rag1 |
the pod stays on rag1; that is where the local-path
folder with the models lives |
image: ollama/ollama:0.40.19 |
the same version as in the preparation |
OLLAMA_KEEP_ALIVE: "-1" |
never unload the model – no cold requests after a five-minute pause (diagnosis from the preparation) |
requests.cpu: "2", memory: 2Gi |
what the pod is guaranteed to get |
limits.memory: 3Gi |
no more memory than this – otherwise the kernel kills the pod (OOMKilled) |
readinessProbe on /api/version |
requests only once Ollama answers |
mountPath: /root/.ollama |
that is where Ollama stores its models inside the container |
A CPU limit is deliberately missing. You measure why in Drill 5.
Step 6 – the service.
# on rag1
nano service.yamlapiVersion: v1
kind: Service
metadata:
name: embed
namespace: rag
spec:
selector:
app: embed
ports:
- port: 11434
targetPort: 11434The service looks for all pods with the label app: embed
and is reachable in the cluster as embed.rag.
Remember: Models belong on a volume, not in the image and not in the container – otherwise you download 1.2 GB again after every restart.
Save your progress? Sign up for free
Drill 2: Rolling out and loading the model
Where you are: on rag1, in
~/kurs/03.What you have afterwards: the running pod with bge-m3 on its volume.
Step 1 – namespace and volume.
# on rag1
kubectl apply -f namespace.yamlExpected:
namespace/rag created
# on rag1
kubectl apply -f pvc.yaml# on rag1
kubectl get pvc -n ragExpected:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
modelle Pending local-path <unset> 1s
What you see: Pending is correct here.
local-path only creates the folder when a pod needs the
volume – then it knows on which node.
Step 2 – deployment and service.
# on rag1
kubectl apply -f deployment.yaml# on rag1
kubectl apply -f service.yaml# on rag1
kubectl -n rag rollout status deploy/embed --timeout=900sExpected: after a few minutes
deployment "embed" successfully rolled out
In the lab it took 7 minutes 10 seconds: the image is 3.4 GB because it brings libraries for graphics cards.
Step 3 – pod and volume.
# on rag1
kubectl get pods,pvc -n rag -o wideExpected (excerpt):
NAME READY STATUS RESTARTS AGE IP NODE
embed-6997987848-mshl8 1/1 Running 0 7m10s 10.42.0.7 rag1
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS
modelle Bound pvc-b6dff131-03cb-4e19-8d59-38108fa2d28e 10Gi RWO local-path
Step 4 – the model into the cabinet. The volume is empty. You pull bge-m3 inside the container:
# on rag1
kubectl -n rag exec deploy/embed -- ollama pull bge-m3kubectl exec deploy/embed -- <command> runs the
command in a pod of the deployment. The output ends with
success; in the lab it took 1 minute 40 seconds.
# on rag1
kubectl -n rag exec deploy/embed -- ollama listExpected:
NAME ID SIZE MODIFIED
bge-m3:latest 790764642607 1.2 GB 3 seconds ago
Remember: An empty volume means: load the model into it once. After that it stays there – even across restarts of the pod, as you will measure in a moment.
Save your progress? Sign up for free
Drill 3: Asking from another node
Where you are: on rag1, in
~/kurs/03.What you have afterwards: a helper pod on rag2 that reaches the service by its name, and the times cold and warm.
Step 1 – a pod with curl on rag2.
# on rag1
kubectl run -n rag curl --image=curlimages/curl:8.17.0 --restart=Never --overrides='{"spec":{"nodeSelector":{"kubernetes.io/hostname":"rag2"}}}' --command -- sleep 3600| Option | Meaning |
|---|---|
--image=curlimages/curl:8.17.0 |
a small image with curl |
--restart=Never |
a single pod, not a deployment |
--overrides='{…}' |
adds to the pod specification: here a nodeSelector for
rag2 |
--command -- sleep 3600 |
the pod sleeps for an hour and waits for your commands |
# on rag1
kubectl -n rag get pod curl -o wideExpected:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
curl 1/1 Running 0 8s 10.42.1.2 rag2 <none> <none>
Step 2 – ask through the service.
# on rag1
kubectl -n rag exec curl -- curl -s http://embed.rag:11434/api/embed -d '{"model":"bge-m3","input":"Wie starte ich den Druckdienst neu?"}' -o /tmp/e.jsonembed.rag is the name of the service embed
in the namespace rag. The response lands in the helper pod
as /tmp/e.json.
Step 3 – get the response out.
# on rag1
kubectl cp rag/curl:/tmp/e.json kalt.jsonExpected:
tar: removing leading '/' from member names
kubectl cp <namespace>/<pod>:<path> <target>
copies a file out of the pod. The note from tar is harmless
– kubectl cp uses tar internally.
# on rag1
jq '.embeddings[0][0:4]' kalt.jsonExpected:
[
-0.07397491,
0.0012907916,
-0.034456816,
-0.029466601
]
What you see: exactly the numbers from the preparation. Same model, same text, same vector – no matter whether Ollama runs on the host or in the pod.
# on rag1
jq '{total_duration, load_duration}' kalt.jsonExpected:
{
"total_duration": 9080141235,
"load_duration": 8830380945
}
Cold: 9.1 s, 8.8 s of it loading.
Step 4 – warm. Only the total duration, straight
from curl:
# on rag1
kubectl -n rag exec curl -- curl -s -o /dev/null -w '%{time_total}\n' http://embed.rag:11434/api/embed -d '{"model":"bge-m3","input":"Wie starte ich den Druckdienst neu?"}'-o /dev/null discards the response,
-w '%{time_total}\n' prints the duration in seconds.
Expected (three calls in the lab):
0.174897
0.234451
0.203140
Step 5 – the model stays.
# on rag1
kubectl -n rag exec deploy/embed -- ollama psExpected:
NAME ID SIZE PROCESSOR CONTEXT RUNNER UNTIL
bge-m3:latest 790764642607 1.2 GB 100% CPU 4096 llamacpp Forever
UNTIL Forever – that is the effect of
OLLAMA_KEEP_ALIVE=-1.
Remember: Through the service, the embedding service is reachable from every node as
embed.rag:11434. Warm, a question costs about 0.2 s – as on the host.
Save your progress? Sign up for free
Drill 4: Ready, but not done
Where you are: on rag1, in
~/kurs/03.What you have afterwards: a startup probe that reports the pod as ready only once the model is loaded.
Step 1 – restart.
# on rag1
kubectl -n rag rollout restart deploy/embed# on rag1
kubectl -n rag rollout status deploy/embed --timeout=300sIn the lab, the new pod was ready after 9 seconds.
Step 2 – is the model still there?
# on rag1
kubectl -n rag exec deploy/embed -- ollama listExpected:
NAME ID SIZE MODIFIED
bge-m3:latest 790764642607 1.2 GB 3 hours ago
The cabinet survived the move. But:
# on rag1
kubectl -n rag exec deploy/embed -- ollama psExpected:
NAME ID SIZE PROCESSOR CONTEXT RUNNER UNTIL
Nothing loaded.
Step 3 – the first call after the restart. As in
Drill 3, steps 2 and 3, with the target file
nach-neustart.json:
# on rag1
jq '{total_duration, load_duration}' nach-neustart.jsonExpected:
{
"total_duration": 8761808814,
"load_duration": 8585742063
}
What you see: the pod was “ready”, yet the first
user still waited almost nine seconds. The readiness probe only asks
/api/version – which answers as soon as the process is
running.
Step 4 – a command that loads the model. The image
contains neither curl nor wget. But
ollama run also works with an embedding model:
# on rag1
kubectl -n rag exec deploy/embed -- ollama run bge-m3 "probe"Expected (excerpt, the beginning of a long line):
[-0.010453617,0.016800608,-0.041640993,0.0040788096,-0.014982172,0.02015856,
ollama run <model> "<text>" loads the model
and, for an embedding model, prints the vector. After that the model
appears in ollama ps.
Step 5 – the startup probe.
# on rag1
cp deployment.yaml deployment-v1.yaml# on rag1
nano deployment.yamlInsert six lines before the readinessProbe:
startupProbe:
exec:
command: ["ollama", "run", "bge-m3", "probe"]
periodSeconds: 10
timeoutSeconds: 60
failureThreshold: 6
readinessProbe:
httpGet:
path: /api/version
port: 11434
periodSeconds: 5| Setting | Meaning |
|---|---|
exec.command |
the command from step 4; exit code 0 means started |
periodSeconds: 10 |
try every ten seconds |
timeoutSeconds: 60 |
one attempt may take a minute – loading takes nine seconds |
failureThreshold: 6 |
after six failed attempts, Kubernetes restarts the container |
As long as the startup probe has not passed, the pod does not count as ready, and the service sends it no requests.
Step 6 – apply and measure.
# on rag1
kubectl apply -f deployment.yaml# on rag1
kubectl -n rag rollout status deploy/embed --timeout=300sIn the lab: 16 seconds until ready.
# on rag1
kubectl -n rag exec deploy/embed -- ollama psExpected:
NAME ID SIZE PROCESSOR CONTEXT RUNNER UNTIL
bge-m3:latest 790764642607 1.2 GB 100% CPU 4096 llamacpp Forever
The first call afterwards (as in Drill 3, target file
nach-probe.json):
{
"total_duration": 225736279,
"load_duration": 3267436
}
0.23 s instead of 8.8 s.
Remember: The startup probe moves the loading in front of the “open” sign. Seven seconds longer until ready, but no user has to wait.
Save your progress? Sign up for free
Drill 5: What a CPU limit costs
Where you are: on rag1, in
~/kurs/03.What you have afterwards: the duration for four cards with and without a CPU limit – and the throttling numbers from the cgroup.
Step 1 – the four cards into the helper pod.
# on rag1
kubectl cp ../01/karten-anfrage.json rag/curl:/tmp/karten.jsonStep 2 – without a limit.
# on rag1
kubectl -n rag exec curl -- curl -s -o /dev/null -w '%{time_total}\n' http://embed.rag:11434/api/embed -d @/tmp/karten.jsonExpected (three calls in the lab):
1.740670, 1.826998, 2.081524.
Step 3 – a limit of one core.
# on rag1
cp deployment.yaml deployment-cpu1.yaml# on rag1
nano deployment-cpu1.yaml resources:
requests:
cpu: "1"
memory: 2Gi
limits:
cpu: "1"
memory: 3GiBoth values belong together: a request may never be
larger than the limit.
# on rag1
kubectl apply -f deployment-cpu1.yaml# on rag1
kubectl -n rag rollout status deploy/embed --timeout=300sStep 4 – measure with the limit. The same command as in step 2.
Expected (three calls in the lab):
13.530233, 13.926897,
14.043043.
What you see: about seven times as long. One core instead of four does not explain that on its own.
Step 5 – see the throttling.
# on rag1
kubectl -n rag exec deploy/embed -- cat /sys/fs/cgroup/cpu.maxExpected:
100000 100000
100,000 µs of computing time per 100,000 µs – that is, one core.
# on rag1
kubectl -n rag exec deploy/embed -- cat /sys/fs/cgroup/cpu.statExpected (excerpt – look for the lines with
nr_ and throttled):
nr_periods 626
nr_throttled 531
throttled_usec 124249304
| Line | Value | Meaning |
|---|---|---|
nr_periods |
626 | time windows of 100 ms each in which the pod wanted CPU |
nr_throttled |
531 | windows in which it was paused because its quota was used up |
throttled_usec |
124,249,304 | a total of 124 s of forced pause |
What you see: throttling hit 85 % of the windows. In
the lab, the model process llama-server ran with 13 threads
on a node with four cores. Together they use up the quota of one core
after a fraction of the window – and then wait out the rest.
Step 6 – back again.
# on rag1
kubectl apply -f deployment.yamlAfter that, four cards took 2.3 s again in the lab.
Remember: A CPU limit on a model service does not cost proportionally but a multiple – the threads run into the quota and wait. For CPU models: set
requests, leave outlimits.cpu, or limit the threads to match.
If it does not work
kubectl applyreportsThe Deployment "embed" is invalid: spec.template.spec.containers[0].resources.requests: Invalid value: "2": must be less than or equal to cpu limit of 1: this happened in the lab when only the limit was set.requests.cpuhas to come down as well.- During startup,
kubectl -n rag get eventsshows warnings likeReadiness probe failed: … connect: connection refusedorcontext deadline exceeded. In the lab they appeared while the model was loading and went away on their own afterwards.
Save your progress? Sign up for free
Diagnosis task
After a maintenance window, users report: “The first search in the
morning hangs for nine seconds.” The pod is running,
kubectl get pods shows 1/1. You have ten
minutes.
Solution:
kubectl -n rag exec deploy/embed -- ollama ps – if the list
is empty, the model is not loaded.
kubectl -n rag get deploy embed -o yaml shows whether a
startupProbe is missing (Drill 4). If
OLLAMA_KEEP_ALIVE is not set to -1, Ollama
additionally unloads the model after five minutes without a request
(Preparation, diagnosis task).
Challenge
Additionally pull nomic-embed-text into the pod and
extend the startup probe so that both models are loaded before the
“open” sign goes up. How long does the restart take then, and how much
memory does kubectl top pod -n rag show?
Save your progress? Sign up for free
Checkpoint
# on rag1
kubectl -n rag get deploy embed -o jsonpath='{.spec.template.spec.containers[0].startupProbe.exec.command}{"\n"}'Expected:
["ollama","run","bge-m3","probe"]
# on rag1
kubectl -n rag exec curl -- curl -s -o /dev/null -w '%{http_code}\n' http://embed.rag:11434/api/versionExpected:
200
Test yourself
- Why is the PVC
modellePendingat first? - What does
ollama psshow right after a restart, and what does that mean for the first user? - Why is a readiness probe on
/api/versionnot enough? - How much slower did four cards get with
limits.cpu: 1, and why more than proportionally? - Why does the deployment say
strategy: Recreate?
Answers:
local-pathonly creates the folder when a pod uses the volume – before that it is unclear on which node.- An empty list: the model is not loaded; the first call waits about nine seconds for loading.
- It only checks whether the process answers, not whether the model is in memory.
- About seven times (14 instead of 2 s). The model process works with many threads (13 in the lab); they use up the quota of one core quickly and wait out the rest of each 100 ms window (531 of 626 windows throttled).
- The volume is
ReadWriteOnce. A new pod could not mount it while the old one is running.
Interview note
“What is different about model services on Kubernetes?” – Three things: models are large and belong on a volume or in a cache, not in the image; “ready” means “model loaded”, which needs its own startup or readiness check; and CPU limits hit model servers hard because they start many threads. A measured number to go with it – 14 instead of 2 seconds with a one-core limit – is more convincing than any rule.
Enterprise note
In production, models often live in an internal registry or on shared storage and are brought onto the nodes by an init container or an image cache. Embedding services scale to several replicas – then every replica needs its loaded models, and the startup probe prevents a rollout from briefly sending a whole batch of cold pods into the race.
Summary
The embedding service now runs as a deployment in the namespace
rag, with bge-m3 on its own volume and reachable as
embed.rag:11434. It delivers the same vectors as on the
host, warm in about 0.2 s. After a restart the pod was ready but the
model was not loaded – the first call took 8.8 s. A startup probe with
ollama run bge-m3 "probe" closes this gap. A CPU limit of
one core made four cards seven times slower; the cgroup showed 531 of
626 windows throttled.
Commands at a glance
| Command | What it does | Parameters |
|---|---|---|
sudo systemctl disable --now ollama |
stop the host service and remove its autostart | --now stop immediately |
kubectl apply -f <file> |
apply a manifest | – |
kubectl -n rag rollout status deploy/embed |
wait for the rollout | --timeout |
kubectl -n rag exec deploy/embed -- ollama pull <model> |
load a model into the pod | – |
kubectl -n rag exec deploy/embed -- ollama ps |
loaded models | – |
kubectl run -n rag curl --image=… --overrides='…' --command -- sleep 3600 |
helper pod on a specific node | --overrides add pod fields |
kubectl cp rag/curl:/tmp/e.json kalt.json |
copy a file out of a pod | – |
curl -s -o /dev/null -w '%{time_total}\n' … |
measure only the duration | -w format |
kubectl -n rag rollout restart deploy/embed |
restart the pods | – |
kubectl -n rag exec deploy/embed -- cat /sys/fs/cgroup/cpu.stat |
throttling of the pod | – |
kubectl top pod -n rag |
CPU and memory per pod | – |