NIM microservices
What an NVIDIA NIM is, how to cache and run one on an OpenShift GPU node, and how to call its health and OpenAI-compatible APIs.
An NVIDIA NIM is a container that serves one model behind a small HTTP API. You pull it from NGC, it picks an engine profile for the GPU it can see, and it answers health checks while it loads. For the language and embedding models we use, that API is the OpenAI shape: /v1/models, /v1/chat/completions, and /v1/embeddings. The RAG lab calls those paths. The blueprints lab is a packaged pipeline that calls them too.
This lab runs Llama 3.2 1B Instruct as a NIM, because a single small GPU can hold it. A Nemotron NIM is the same two objects with a different image. Which models exist, and when to serve one yourself instead, is the NVIDIA models lab.
Prerequisites
- OpenShift 4.x and
oc - A worker with a supported NVIDIA GPU, labelled
nvidia.com/gpu.present=true. The GPU Operator'sClusterPolicyis what advertisesnvidia.com/gpu - The NVIDIA NIM Operator (
nimservices.apps.nvidia.comandnimcaches.apps.nvidia.com) - An NGC API key that is allowed to pull the NIM image, exported as
NGC_API_KEYin your shell for the secret commands below - A storage class for the cache volume. On our OpenShift Data Foundation cluster that is
ocs-storagecluster-ceph-rbd.oc get storageclassis the source of the name
oc get nodes -l nvidia.com/gpu.present=true
oc get clusterpolicy
oc get crd nimservices.apps.nvidia.com nimcaches.apps.nvidia.com
Both CRDs must exist before the manifests below will apply. A node list that is empty means the device plugin has not labelled a machine yet. Fix that before you debug the NIM.
Pull secrets
NGC's registry user is the literal string $oauthtoken. The password is the API key. One secret pulls the image. The other is the key the cache and the server use to fetch the model profile.
oc new-project nim
oc create secret docker-registry ngc-secret \
--docker-server=nvcr.io \
--docker-username='$oauthtoken' \
--docker-password="$NGC_API_KEY" \
-n nim
oc create secret generic ngc-api-secret \
--from-literal=NGC_API_KEY="$NGC_API_KEY" \
-n nim
The single quotes around $oauthtoken are required. Without them the shell expands an empty variable and the pull fails with a 401 that looks like a bad key.
Cache, then serve
A NIM that downloads its model on every start spends the first many minutes of each pod doing that download. A NIMCache does it once, onto a PVC. engine: vllm asks for a vLLM profile matched to the GPU on the node, with tensor parallelism 1. Leave the profile list empty and let the operator choose. profiles: all pulls every engine build and needs a much larger volume; NVIDIA's own all-profile example sizes that case at 300Gi.
apiVersion: apps.nvidia.com/v1alpha1
kind: NIMCache
metadata:
name: llama-1b
namespace: nim
spec:
nodeSelector:
nvidia.com/gpu.present: "true"
source:
ngc:
authSecret: ngc-api-secret
pullSecret: ngc-secret
modelPuller: nvcr.io/nim/meta/llama-3.2-1b-instruct:1.8.3
model:
engine: vllm
tensorParallelism: "1"
storage:
pvc:
create: true
size: 50Gi
storageClass: ocs-storagecluster-ceph-rbd
volumeAccessMode: ReadWriteOnce
oc apply -f nimcache.yaml
oc get nimcache -n nim -w
Wait until STATUS is Ready. The cache pod's log names the profile it selected. If it exits because no profile matches this GPU, that log is the reason: pick another engine, or another NIM image, rather than resizing the pod and hoping. If it exits because the volume is full, raise size and apply again. Change storageClass to a class oc get storageclass lists before the first apply. A bound PVC keeps the old class.
The service uses the cache and one GPU. Empty probe objects tell the operator to install its own probes.
apiVersion: apps.nvidia.com/v1alpha1
kind: NIMService
metadata:
name: llama-1b
namespace: nim
spec:
authSecret: ngc-api-secret
image:
repository: nvcr.io/nim/meta/llama-3.2-1b-instruct
tag: "1.8.3"
pullPolicy: IfNotPresent
pullSecrets:
- ngc-secret
inferencePlatform: standalone
replicas: 1
resources:
limits:
nvidia.com/gpu: "1"
requests:
nvidia.com/gpu: "1"
nodeSelector:
nvidia.com/gpu.present: "true"
storage:
nimCache:
name: llama-1b
expose:
service:
type: ClusterIP
port: 8000
env:
- name: NGC_API_KEY
valueFrom:
secretKeyRef:
name: ngc-api-secret
key: NGC_API_KEY
livenessProbe: {}
readinessProbe: {}
startupProbe: {}
The cache name and the service name match on purpose. The image tag matches modelPuller. Pin both in Git the way every other image in these labs is pinned, and record the digest of the pull you actually tested.
oc apply -f nimservice.yaml
oc get nimservice,pods -n nim
oc logs -n nim deploy/llama-1b --tail=50
If the operator named the deployment differently, oc get pods -n nim shows the pod to pass to oc logs. Follow the log until it stops reporting that it is loading the profile.
Health, then a completion
The process answers two different questions. Live means the server is up. Ready means the model can take a request. Send traffic only when ready returns success. A pod can be Running and not ready for a long time on a cold cache; that is the load, not a deadlock.
oc port-forward -n nim svc/llama-1b 8000:8000
curl -sS -o /dev/null -w "live %{http_code}\n" http://localhost:8000/v1/health/live
curl -sS -o /dev/null -w "ready %{http_code}\n" http://localhost:8000/v1/health/ready
200 on ready is the gate. 503 means wait and try again. Then take the model id from the server. Do not guess it. The string the container serves is the string the client must send.
curl -sS http://localhost:8000/v1/models
MODEL=$(curl -sS http://localhost:8000/v1/models \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["data"][0]["id"])')
curl -sS http://localhost:8000/v1/chat/completions \
-H 'Content-Type: application/json' \
-d "{\"model\":\"${MODEL}\",\"messages\":[{\"role\":\"user\",\"content\":\"Reply with one sentence.\"}],\"max_tokens\":64}"
An embedding NIM is the same service shape and the same health paths. The call is /v1/embeddings with an input array, and the width of data[0].embedding is the dimension the vector database lab must use. A speech or vision NIM often has another schema. Its health paths are still the first thing to probe; its model card is where the request body is written down. The models lab sorts those out.
A route is optional, and it is for callers already inside the network:
oc expose svc/llama-1b -n nim --port=8000
oc get route llama-1b -n nim -o jsonpath='{.spec.host}{"\n"}'
That route is plain HTTP on the cluster's apps domain. Treat it like any other model API: network policy, and an audience that is allowed to send prompts. Port-forward is enough to prove the lab.
When it will not start
| What you see | What it means |
|---|---|
ImagePullBackOff and a 401 from nvcr.io |
ngc-secret user is not $oauthtoken, or the key cannot pull this image |
| Cache stays in progress, then the pod errors on space | The PVC is smaller than the profile. Raise size |
| Cache logs that no profile matched | This GPU has no build for the engine you set. Change engine, or change the NIM image |
Pod Pending, event Insufficient nvidia.com/gpu |
Nothing free advertises a GPU. The request is already 1; a larger number will not schedule either |
Ready stays 503 and the log is still loading |
Wait. Kill the pod only if the log has stopped moving |
| Chat returns an unknown model | model does not match /v1/models. Use the id the server printed |
Air-gapped clusters pull nvcr.io/nim/meta/llama-3.2-1b-instruct:1.8.3 through the registry mirror you already use for other images, and the cache PVC is filled while that mirror is reachable. After that the service starts from the PVC. The blueprint images follow the same mirror. The blueprints lab assumes this endpoint, or one like it, is already Ready.
What's next
- NVIDIA models: language, vision, audio and video, and when a NIM is the wrong package.
- Blueprints: a full pipeline that calls this API.
- RAG: embeddings, reranking and the LLM, each as a NIM.
- Vector databases: the index those embeddings are written into.
One NIM, one model, one GPU, a Ready check, then a single completion. Scale and other models are the same manifest with a different image and a different cache.