Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Kubernetes GPU LLM Inference: Requests & Limits Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Choosing the right Kubernetes resource requests and limits for GPU-backed Large Language Model (LLM) inference is critical for balancing cost, latency, and stability. Correct configuration ensures your pods utilize hardware efficiently without triggering OOMKill events or leaving GPUs idle. This guide provides a practical framework for sizing these resources based on model architecture and batch size.

What You'll Need

  • A running Kubernetes cluster with GPU node support (e.g., NVIDIA, AMD, or Intel GPUs).
  • Kubectl access with permissions to manage deployments and pods.
  • The specific LLM model weights and an inference server container (such as vLLM, TGI, or Triton).
  • Understanding of your model’s VRAM requirements (activation memory + KV cache).
  • Monitoring tools like Prometheus/Grafana or `nvtop` to observe real-time GPU utilization.

Step-by-Step

  1. Determine Model VRAM Footprint: Calculate the static memory required to load the model weights. For example, a 7B parameter model in FP16 requires approximately 14GB of VRAM. Add overhead for the runtime environment. This number becomes your baseline limits.nvidia.com/gpu-memory if using device plugins that support memory constraints, or simply informs your CPU/Memory requests.
  2. Set GPU Resource Limits: Define the hard limit for GPU allocation. Use the standard Kubernetes resource key nvidia.com/gpu. Set this to the exact number of GPUs needed per pod. For most single-GPU models, set limits: { nvidia.com/gpu: 1 }. Never set requests lower than limits for GPUs; if you request 1 GPU, you must limit 1 GPU.
  3. Configure Memory Requests and Limits: The host system RAM must accommodate both the model weights and the active KV cache. A safe starting point is to set requests.memory to 1.5x the model weight size and limits.memory to 2x. If your model is 14GB, request 20Gi and limit 30Gi. This prevents the host from swapping out GPU data when multiple pods share a node.
  4. Adjust CPU Requests for Pre-processing: LLM inference involves tokenization and detokenization, which are CPU-bound tasks. Set requests.cpu to at least 2-4 cores per GPU. If you expect high concurrency, increase this to 8+ cores. Ensure limits.cpu matches or slightly exceeds requests to prevent throttling during peak load spikes.
  5. Implement QoS Classes via Ratios: Kubernetes assigns Quality of Service (QoS) classes based on the relationship between requests and limits. To ensure your LLM pods are not evicted under pressure, set requests equal to limits for all resources. This forces the QoS class to "Guaranteed," providing the highest priority for scheduling and protection against eviction.
  6. Deploy with Initial Health Checks: Configure liveness and readiness probes. For GPU pods, use an exec probe that calls a lightweight API endpoint (like /health) rather than relying solely on TCP sockets. Start with aggressive timeouts (e.g., 5s period) to detect hangs early, then tune based on initial startup times.
  7. Validate with Load Testing: Before production rollout, run a synthetic load test using tools like Locust or k6. Monitor GPU memory usage via `nvidia-smi`. If memory usage consistently hits 95%+, increase your memory limits. If GPU utilization drops below 40% while waiting for tokens, increase CPU requests to handle batching more efficiently.

Common Mistakes

One of the most frequent errors is setting GPU requests lower than limits. Kubernetes cannot pre-schedule a pod if it doesn't know exactly how much GPU it needs. Another mistake is ignoring the KV cache growth. As context windows grow, memory usage expands non-linearly. Assuming static memory usage leads to sudden crashes during long conversations. Additionally, users often forget that CPU requests must account for the tokenizer overhead, leading to CPU starvation and increased latency.

Tips

Use vertical pod autoscaling cautiously with GPUs, as resizing GPU pods often requires restarting the inference engine, causing downtime. Instead, scale horizontally by adding more replicas. Always leave a small buffer (5-10%) in your memory limits to handle unexpected spikes in sequence length. Consider using dedicated GPU nodes with taints to prevent other workloads from sharing expensive hardware, ensuring consistent performance for latency-sensitive LLM tasks.

Troubleshooting

If your pod enters CrashLoopBackOff, check the logs for Out Of Memory (OOM) errors. This usually indicates insufficient limits.memory or limits.nvidia.com/gpu-memory. Increase these values incrementally. If the pod is stuck in Pending, verify that the cluster has available GPU capacity and that your node selectors or tolerations match the GPU type. High latency despite low error rates often points to CPU bottlenecks; increase CPU requests. If you see frequent evictions, ensure your QoS class is "Guaranteed" by aligning requests and limits.

Frequently Asked Questions

Can I share one GPU among multiple pods?

No, Kubernetes treats a GPU as an indivisible unit by default. While some advanced drivers support MIG (Multi-Instance GPU), standard scheduling requires exclusive access per pod.

What happens if I set requests higher than limits?

Kubernetes will reject the deployment. Requests must always be less than or equal to limits. Setting them equal is the recommended practice for stable inference workloads.

How do I monitor GPU memory usage in Kubernetes?

Use tools like Prometheus with the NVIDIA DCGM exporter. It exposes metrics for GPU memory used, free, and utilization, which can be visualized in Grafana dashboards.

Should I use spot instances for LLM inference?

Spot instances are risky for stateful inference servers due to potential interruptions. If used, implement robust checkpointing and auto-recovery mechanisms, but prefer on-demand nodes for critical services.

Does increasing CPU limits help throughput?

Only up to a point. Once tokenization is fast enough, additional CPU power yields diminishing returns. Focus on optimizing batch size and model parallelism instead.

Why are my pods being evicted?

This typically occurs when node resources are exhausted. Check if other pods are consuming excessive memory or if your GPU requests are too low, causing the scheduler to place too many pods on a single node.