keepcalculator

06 Numbers and Powers

Kubernetes Resources by Workload, Not Object Name

Card No. 06·74172 · Updated · Standard formula, shown below

Choose Kubernetes resource requests and limits based on the workload’s actual behavior—CPU and memory usage patterns, scaling needs, and latency requirements—not on the object’s name or type. Names like “nginx” or “redis” don’t tell you how much CPU or memory that particular deployment needs; only observed metrics and load testing can. This article explains how to determine appropriate resource values, common mistakes, and how to validate your choices.

Why Object Names Are Misleading

Kubernetes objects—pods, deployments, statefulsets—are named by humans for readability, not for resource sizing. A deployment named “web-server” might be a tiny static site or a heavy image-processing API. The same image name can have wildly different resource needs depending on the workload: a Redis cache used for session storage uses far less memory than a Redis instance used as a primary database with large datasets. Relying on the object name or the container image name alone leads to either over-provisioning (wasting money) or under-provisioning (causing OOMKills and throttling).

Understanding Requests and Limits

Resource requests and limits are the two knobs Kubernetes gives you to control how much CPU and memory a container gets. The request is the amount of CPU or memory that Kubernetes guarantees—it uses requests to schedule pods onto nodes with enough free capacity. The limit is the maximum amount the container is allowed to use; for CPU, the limit is enforced by throttling, and for memory, by killing the container (OOMKill) if it exceeds the limit.

Key points:

  • CPU requests are measured in cores (or millicores, 1000m = 1 core). The scheduler uses requests to place pods.
  • Memory requests are measured in bytes (Ki, Mi, Gi). The scheduler also uses memory requests for placement.
  • CPU limits are throttled—the container can’t use more than the limit, but it won’t be killed.
  • Memory limits are enforced by the kernel—if a container exceeds its memory limit, the kernel kills it (OOMKill).

How to Determine Resource Needs by Workload

To choose the right values, you need to observe the workload under real conditions. Here’s a step-by-step approach:

  1. Start with no limits and modest requests—for example, request 100m CPU and 128Mi memory, but set no limits initially so you can see the actual peak usage.
  2. Run load tests that simulate realistic traffic, including spikes. Use tools like k6, JMeter, or wrk to generate load.
  3. Monitor with metrics—use kubectl top pods, Prometheus, or any metrics server to record CPU and memory usage over time.
  4. Set requests to the 50th-70th percentile of observed usage, and limits to the 90th-95th percentile or a bit above the peak, leaving headroom for temporary spikes.
  5. Re-evaluate after each release—workloads change, so re-run the process periodically.

For example, if a web API pod consistently uses 250m CPU and 500Mi memory under normal load, with peaks of 400m and 700Mi, you might set requests to 300m and 512Mi, and limits to 500m and 1Gi. This gives enough headroom without wasting resources.

Common Mistakes and Misconceptions

Even experienced engineers make these errors:

  • Setting requests equal to limits—this guarantees your pod will be throttled or killed if it ever needs a little more, and it wastes node capacity.
  • Setting limits but no requests—the scheduler ignores limits for placement, so your pod might land on a node that can’t actually provide the limit when needed.
  • Using the same values for all replicas—different replicas may have different usage patterns (e.g., one handles a heavy client).
  • Ignoring memory usage of caches—a cache that grows with traffic can cause OOMKills if you set a limit too low.
  • Not considering the cost of OOMKills—a restart may be acceptable for stateless apps, but for stateful workloads it can cause data loss.

Comparing Workload Types

Different workload categories have distinct resource profiles:

  • Web services—usually CPU-bound, with memory usage proportional to concurrent connections. Requests should cover baseline CPU, limits should allow for bursts.
  • Batch jobs—often CPU-intensive for short periods. Use requests for the expected peak, and limits slightly higher to avoid throttling.
  • Stateful databases—memory is critical; set requests to the working set size, and limits to a bit above that to avoid OOMKills. CPU is less critical.
  • Machine learning inference—can be both CPU and memory heavy; use GPU if needed, but for CPU, set requests based on model size and batch size.

Cost and Efficiency Implications

Over-requesting resources directly increases your cloud bill because you pay for the reserved capacity, not just the used amount. Under-requesting can cause performance issues and downtime, which also costs money. The sweet spot is to request what you need and allow limits to absorb spikes. Use Kubernetes’ Vertical Pod Autoscaler (VPA) to automatically adjust requests based on usage, but be aware it only works for workloads that can tolerate restarts.

Safety and Validation

Before rolling out resource changes to production, test in a staging environment that mirrors production. Use kubectl apply --dry-run=server to validate that the new requests don’t exceed node capacity. Also, set up alerts for OOMKills and CPU throttling so you catch issues early. Remember that memory limits are enforced by the kernel—if you set a limit too low, your container will be killed, and if you set it too high, you might cause node pressure that affects other pods.

Frequently Asked Questions

Q: Should I set the same request and limit for CPU?
A: No. Setting them equal means your container is immediately throttled if it tries to use more than the request, even if the node has spare CPU. It’s better to set a lower request and a higher limit to allow bursts.

Q: How do I find the right memory request for my app?
A: Run the app under load and monitor its memory usage with kubectl top pod or Prometheus. Set the request to the 50th-70th percentile of observed usage, and the limit to about 20-30% above the peak.

Q: What happens if I set no limits?
A: The container can use as much CPU and memory as the node has, which can starve other pods. It’s risky for production; always set limits to protect the node.

Q: Can I use the same values for all containers in a pod?
A: No. Each container has its own resource needs. For example, a sidecar proxy uses less memory than the main app. Set values per container based on its own usage.

Q: Does the object name (like “redis”) affect scheduling?
A: No. The scheduler only looks at requests and limits, not names. Names are for humans; resources are for the scheduler.

Q: How often should I review resource settings?
A: At least after every major release or when traffic patterns change. Use VPA for continuous adjustment if your workload can tolerate restarts.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →