When you start using Kubernetes, one of the first things you come across is the settings for memory and CPU usage. It's strongly recommended to set these correctly for every container in your pods. This is crucial for optimising the performance and reliability of your applications. On top of that, Kubernetes can make better decisions and use machines more efficiently, so you get an optimal environment.

Containers, Pods and Nodes
Once you’ve built an application, it gets packaged as an image (a container image or docker image). This image is used to start a container, which in turn is part of a pod.
Put simply, you can see Kubernetes as a system that runs applications on a number of machines (nodes, in Kubernetes-speak). Kubernetes knows the total CPU and memory of each machine. What Kubernetes doesn’t know, though, are the specific needs of your application. Without that information, assigning applications to machines becomes more of a calculated guess than a well-considered choice. And that’s obviously something we want to avoid…
CPU and Memory
We’ll focus first on the two basic resources: CPU and memory. Bear in mind, though, that there are more resources, such as storage and GPUs, each with their own complexity and use. In later blog posts we’ll go deeper into storage and GPU usage, for example.
Viewing Resources
You can look up the available resources of each machine in your Kubernetes cluster with the following command:
kubectl get nodes -o json | jq '.items[].status.capacity'
This gives output like:
{
"cpu": "4",
"ephemeral-storage": "20414Mi",
"hugepages-1Gi": "0",
"hugepages-2Mi": "0",
"memory": "7981416Ki",
"pods": "58"
}
Here we see roughly 8 GB of memory and 4 CPUs available. If you don’t have the jq tool, just run kubectl describe nodes and look for “Capacity” under “Status”.
Units
When you set CPU and memory for your containers in Kubernetes, it’s important to understand and use the units correctly.
CPU
CPU resources in Kubernetes are expressed in “cpu”/“core” or “millicpu”/“millicore” (m). One CPU unit in Kubernetes corresponds to one virtual CPU (vCPU, a “core”) or one physical CPU core on the underlying node. The exact computing power that comes with it depends on the hardware.
A few examples:
cpu: 1: This equals 1 CPU. If you specify a value of 1 or 1000m, your container gets one full CPU core assigned.cpu: 500m: This equals 0.5 CPU. With 500m you give your container half the capacity of a single CPU core.cpu: 2.5: This equals the computing power of 2.5 CPUs.
Using millicpu lets you allocate CPU resources precisely. You can’t allocate less than 1 millicpu (or millicore). Values like “0.5m” or “0.0005” are therefore not valid. The smallest allocation is “1m”, or 0.001.
Memory
Working memory, or memory, or “RAM” is expressed in bytes, but you can also use larger units such as kilobytes, megabytes and gigabytes. This lets you allocate memory to your containers precisely, depending on what your application needs.
The units used are binary units (the IEC prefixes Ki, Mi, Gi). That’s why we don’t talk about “megabytes” and “gigabytes” but about “mebibytes” and “gibibytes”. Because 5 GB is actually equal to 5000 MB, but 5 Gi is 5368.7 MB.
A few examples:
memory: 128Mi: This stands for 128 mebibytes. The ‘Mi’ suffix stands for mebibytes, where 1 Mi equals 2^20 bytes or 1,048,576 bytes.memory: 1Gi: This stands for 1 gibibyte. The ‘Gi’ suffix stands for gibibytes, where 1 Gi equals 2^30 bytes or 1,073,741,824 bytes.
“Once upon a time, computer professionals noticed that 2^10 was very nearly equal to 1000 and started using the SI prefix “kilo” to mean 1024. That worked well enough for a decade or two because everybody who talked kilobytes knew that the term implied 1024 bytes. But, almost overnight a much more numerous “everybody” bought computers, and the trade computer professionals needed to talk to physicists and engineers and even to ordinary people, most of whom know that a kilometer is 1000 meters and a kilogram is 1000 grams.”
You can find more on the why behind the different units on NIST’s SI site under “Historical context”.
Pro tip. Everyone has run into this at some point, but remember not to use the “cpu” unit for memory. Say you define memory: 128m as a value, then we’re talking about 0.128 bytes, which is very little indeed… Maybe (presumably) you meant 128Mi.
An Example of Resource Settings
Here’s an example of a simple pod:
apiVersion: v1
kind: Pod
metadata:
name: voorbeeld-pod
spec:
containers:
- name: voorbeeld-container
image: voorbeeld-image
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
In this example we see the two important parts of the container’s “resources” block: “requests” and “limits”. We’ll focus on the “requests” first.
Requests: The Basic Requirements
When you set a “request”, you ask Kubernetes to place your application on a machine where at least that amount of CPU and memory is available. For each machine, Kubernetes looks at the pods already assigned to it and calculates how much CPU and memory is still available. Your application will only be assigned to a machine with enough capacity. This is the job of the “scheduler” within Kubernetes.
“The scheduler ensures that, for each resource type, the sum of the resource requests of the scheduled containers is less than the capacity of the node.”
Limits: The Maximum Boundaries
Once the application has started, the “limits” come into play. A hard limit is set at the specified values. If the application tries to exceed these limits, it isn’t allowed to. This is essential to prevent a single application from using too many resources and affecting other applications on the same machine.
And what if you don’t define everything?
If you specify a limit but no request, Kubernetes also uses the limits as the request. If you specify a request but no limit, the request is used when choosing a machine, and no limits are set on CPU and memory usage once your application is running.
Conclusion
Setting CPU and memory correctly for your containers is very important for the performance and reliability of your Kubernetes cluster. By setting both “requests” and “limits”, you make sure your applications are placed efficiently and keep running within safe boundaries. This is only the beginning; in other blog posts we’ll go deeper into the details.

