> This page is for version 2.0.4-pb6.
> For other versions, use one of these documentation indexes:
> - 2.1.6 (Latest) (default): https://docs.nvidia.com/nim/large-language-models/2.1.6/llms.txt
> - 2.1.5: https://docs.nvidia.com/nim/large-language-models/2.1.5/llms.txt
> - 2.1.4: https://docs.nvidia.com/nim/large-language-models/2.1.4/llms.txt
> - 2.0.13: https://docs.nvidia.com/nim/large-language-models/2.0.13/llms.txt
> - 2.0.12: https://docs.nvidia.com/nim/large-language-models/2.0.12/llms.txt
> - 2.0.11: https://docs.nvidia.com/nim/large-language-models/2.0.11/llms.txt
> - 2.0.10: https://docs.nvidia.com/nim/large-language-models/2.0.10/llms.txt
> - 2.0.9: https://docs.nvidia.com/nim/large-language-models/2.0.9/llms.txt
> - 2.0.8: https://docs.nvidia.com/nim/large-language-models/2.0.8/llms.txt
> - 2.0.4-pb6: https://docs.nvidia.com/nim/large-language-models/2.0.4-pb6/llms.txt
> - Archive: https://docs.nvidia.com/nim/large-language-models/archive/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nim/large-language-models/llms.txt.

# KServe

This page describes how to deploy NVIDIA NIM for LLMs on KServe in a Kubernetes environment.

## Prerequisites

Before deploying NIM on KServe, make sure you have the following:

* A Kubernetes cluster with KServe enabled and GPU-capable nodes
* Configured `kubectl` access to the target cluster
* An NGC API key for pulling NIM container images and downloading model artifacts
* A storage class that supports persistent volumes for model caching

> **Note**
>
> If your Kubernetes cluster is provisioned with NVIDIA Cloud Native Stack (CNS), refer to [Enable KServe on CNS](https://github.com/NVIDIA/cloud-native-stack/blob/master/playbooks/readme.md#enable-kserve-on-cns).
> For other environments, refer to the [KServe quickstart environment guide](https://kserve.github.io/website/docs/getting-started/quickstart-guide#install-kserve-quickstart-environment).

## Create Common Resources

Create the same credentials and cache PVC that a Helm deployment uses.

1. Set the NGC API key in your shell:

   ```shell
   export NGC_API_KEY="nvapi-..."
   ```

2. Create the image pull secret:

   ```shell
   kubectl create secret docker-registry ngc-secret \
     --docker-server=nvcr.io \
     --docker-username='$oauthtoken' \
     --docker-password="${NGC_API_KEY}"
   ```

3. Create the NGC API key secret:

   ```shell
   kubectl create secret generic nvidia-nim-secrets \
     --from-literal=NGC_API_KEY="${NGC_API_KEY}"
   ```

4. Create the cache PVC manifest:

   ```shell
   cat <<'EOF' > nvidia-nim-cache-pvc.yaml
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: nvidia-nim-cache-pvc
   spec:
     accessModes:
       - ReadWriteMany
     storageClassName: nfs-client
     resources:
       requests:
         storage: 200Gi
   EOF
   ```

5. Apply the cache PVC manifest:

   ```shell
   kubectl apply -f nvidia-nim-cache-pvc.yaml
   ```

> **Note**
>
> Set `storageClassName` to a StorageClass that is available in your Kubernetes cluster.

> **Tip**
>
> Adjust the PVC `storage` value based on your model size and expected cache usage.

## Deploy NIM on KServe

Complete the following steps to deploy the minimal KServe example:

1. Create `kserve-nim.yaml` with a `ClusterServingRuntime` and an `InferenceService`:

   ```yaml
   apiVersion: serving.kserve.io/v1alpha1
   kind: ClusterServingRuntime
   metadata:
     name: nvidia-nim-llama-3.1-8b-instruct
   spec:
     supportedModelFormats:
       - name: nvidia-nim-llama-3.1-8b-instruct
         version: "2.0.4-pb6.10"
         autoSelect: true
         priority: 1
     protocolVersions:
       - v2
     imagePullSecrets:
       - name: ngc-secret
     containers:
       - name: kserve-container
         image: <NIM_LLM_MODEL_SPECIFIC_IMAGE>:2.0.4-pb6.10
         env:
           - name: NIM_CACHE_PATH
             value: /opt/nim/.cache
           - name: NGC_API_KEY
             valueFrom:
               secretKeyRef:
                 name: nvidia-nim-secrets
                 key: NGC_API_KEY
         ports:
           - containerPort: 8000
         livenessProbe:
           httpGet:
             path: /v1/health/live
             port: 8000
         readinessProbe:
           httpGet:
             path: /v1/health/ready
             port: 8000
         volumeMounts:
           - name: dshm
             mountPath: /dev/shm
           - name: nim-cache
             mountPath: /opt/nim/.cache
     volumes:
       - name: dshm
         emptyDir:
           medium: Memory
           sizeLimit: 16Gi
       - name: nim-cache
         persistentVolumeClaim:
           claimName: nvidia-nim-cache-pvc
   ---
   apiVersion: serving.kserve.io/v1beta1
   kind: InferenceService
   metadata:
     name: llama-3-1-8b-instruct-1xgpu
   spec:
     predictor:
       minReplicas: 1
       model:
         runtime: nvidia-nim-llama-3.1-8b-instruct
         modelFormat:
           name: nvidia-nim-llama-3.1-8b-instruct
         resources:
           requests:
             nvidia.com/gpu: "1"
           limits:
             nvidia.com/gpu: "1"
   ```

2. Apply the manifest:

   ```shell
   kubectl apply -f kserve-nim.yaml
   ```

This manifest is intentionally minimal and works as a starting point in most clusters.

## Enable LoRA on KServe

Optional: Use the same LoRA setup from [Enable LoRA with Helm](/nim/large-language-models/2.0.4-pb6/deployment/kubernetes-deployment/helm-k8s#helm-k8s-lora) to create the LoRA PVC and populate `/loras` with adapter files.

1. Update runtime settings in `kserve-nim.yaml`:

   ```yaml
   env:
     - name: NIM_PEFT_SOURCE
       value: /loras
   volumeMounts:
     - name: lora-adapter
       mountPath: /loras
   volumes:
     - name: lora-adapter
       persistentVolumeClaim:
         claimName: nvidia-nim-lora-pvc
   ```

2. Reapply the manifest:

   ```shell
   kubectl apply -f kserve-nim.yaml
   ```

## Verify Deployment

Verify that the `InferenceService` and pod are ready.

1. Check the `InferenceService` status:

   ```bash
   kubectl get inferenceservice llama-3-1-8b-instruct-1xgpu
   ```

2. Check that the pod is running:

   ```bash
   kubectl get pods -l serving.kserve.io/inferenceservice=llama-3-1-8b-instruct-1xgpu
   ```

3. Review detailed `InferenceService` status and recent events:

   ```bash
   kubectl describe inferenceservice llama-3-1-8b-instruct-1xgpu
   ```

A successful rollout shows the `InferenceService` in a ready state.