‹ Back to all articles

CPU and Memory in Kubernetes: The Critical Difference for Your Applications

In our previous blog post we introduced Kubernetes and its key concepts. Now we're going deeper into one of the most crucial aspects: managing CPU and memory. The difference between these resources looks subtle at first, but it has far-reaching consequences for both the stability and the cost of your cloud environment.

The Basics: How Does Resource Management Work?

Every container in Kubernetes needs resources to run. The two most important ones are CPU and memory. In Kubernetes you configure them through ‘requests’ and ‘limits’:

apiVersion: v1
kind: Pod
metadata:
  name: resource-demo
spec:
  containers:
  - name: app
    resources:
      requests:
        memory: "1Gi"
        cpu: "500m"
      limits:
        memory: "2Gi"
        cpu: "2000m"

This configuration tells Kubernetes that:

  • The container needs at least 500 milliCPU (0.5 CPU) and 1GB of memory
  • The container may use at most 2 CPUs and 2GB of memory

The Fundamental Difference: Compressible vs Non-Compressible

CPU: The Flexible Resource

CPU is what we call a ‘compressible’ resource. This means the system can divide CPU time dynamically without applications crashing. When there isn’t enough CPU available:

  1. The system divides the available CPU time proportionally, according to the requests
  2. Containers are ‘throttled’ when they hit their limit: they get less CPU time
  3. As a result, applications get slower but keep working

Practical example:

Say you have two containers:

  • Container A: request: 1 CPU
  • Container B: request: 2 CPU

When CPU is scarce, Container B always gets twice as much CPU as Container A, because of the 2:1 ratio in requests.

Memory: The Hard Boundary

Memory is ‘non-compressible’. Once an application has allocated memory, the system can’t simply take it back. This leads to:

  1. The system can’t redistribute memory between containers
  2. When memory runs out, a container is stopped immediately (OOMKill)
  3. There’s no graceful degradation possible

For critical services such as databases, it’s often wise to set the memory request and limit to the same value. This prevents the container from allocating more memory than is guaranteed to be available.

The Danger of Overcommitment

A common problem is ‘overcommitment’, especially with memory. Say you have a node with 16GB of memory:

apiVersion: v1
kind: Pod
metadata:
  name: risky-config
spec:
  containers:
  - name: app-1
    resources:
      requests:
        memory: "2Gi"
      limits:
        memory: "8Gi"
  - name: app-2
    resources:
      requests:
        memory: "2Gi"
      limits:
        memory: "8Gi"

This configuration is risky because:

  • The total memory requests (4GB) fit on the node
  • The total limits (16GB) can overload the node. Don’t forget that the node still needs some memory left over for the kernel, management processes, …
  • If both containers grow to their limit, an OOMKill is guaranteed to happen

Monitoring and Optimisation

Configuring resources isn’t a one-off action. You need insight into:

  1. Current Usage:
    • What is the baseline memory usage?
    • How much CPU is actually used?
    • Are there regular peaks?
  2. Resource Pressure:
    • Are containers being throttled?
    • Are there OOMKills?
    • Are there evictions?
  3. Cost Perspective:
    • Are nodes used efficiently?
    • Are requests significantly higher than actual usage?
    • Could workloads be distributed better?

The Krane Labs Approach

At Krane Labs we often see teams struggling with resource configuration. Our expertise helps with:

  1. Analysis: We map actual usage with detailed metrics
  2. Optimisation: We adjust resource configurations based on data
  3. Prevention: We implement early warning systems for resource problems
  4. Cost savings: We identify opportunities for more efficient resource usage

Conclusion

Managing CPU and memory effectively in Kubernetes requires understanding their fundamental differences. With the right configuration and monitoring, you avoid both unnecessary costs and unexpected downtime. At Krane Labs we don’t just help you with the initial setup, we also take care of continuous optimisation as your workload evolves.

Want to know how to configure resources optimally for your specific use case? Get in touch.

Written by

Bernard Grymonpon

Technical architect. Turns abstract requirements into working, maintainable platforms.

Not sure your platform is ready for what's next?

Interested in: