Sub topic 2
Writing CPU and memory quantities correctly
This chapter assumes you can apply YAML manifests with kubectl and know what a pod and a container are. It also assumes you know that a CPU has cores and a machine has memory. Here you learn how to write the numbers in requests and limits. A wrong suffix is a common cause of pods that never schedule or that get killed.
CPU units
Kubernetes measures CPU in cores. One CPU unit equals one physical or virtual core, depending on the node.
You can write CPU in two ways:
- As a decimal number of cores:
1,0.5,2. - As millicores, using the
msuffix:500m,250m,100m.
One core is 1000 millicores. So 0.5 and 500m mean the same thing. Kubernetes prefers the millicore form, and the API rounds anything finer than 1m up to 1m. The value 0.0001 becomes 1m.
CPU is an absolute amount, not a percentage. 500m means half a core on a 2-core node and half a core on a 64-core node.
Memory units
Memory is measured in bytes. You write it as a plain integer or with a suffix. There are two families of suffixes, and they differ in size.
| Suffix | Meaning | Bytes |
|---|---|---|
| `Ki` | kibibyte | 1,024 |
| `Mi` | mebibyte | 1,048,576 |
| `Gi` | gibibyte | 1,073,741,824 |
| `K` | kilobyte | 1,000 |
| `M` | megabyte | 1,000,000 |
| `G` | gigabyte | 1,000,000,000 |
Use the binary forms (Mi, Gi) unless you have a reason not to. They match how operating systems count memory, and most Kubernetes documentation uses them. The difference is small at one megabyte but grows with size. 1Gi is about 7% larger than 1G.
A worked example
This pod requests a quarter of a core and 128 MiB of memory. It is capped at half a core and 256 MiB.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 500m
memory: 256MiRead each value like this:
cpu: 250munderrequestsis 0.25 core. The scheduler reserves it when placing the pod.memory: 128Miunderrequestsis 134,217,728 bytes.cpu: 500munderlimitsis the most CPU time the container may use.memory: 256Miunderlimitsis the most memory the container may hold.
Common mistakes
The most damaging mistake is the lowercase m on memory. In memory, a bare m means milli-bytes, which is one thousandth of a byte. The value memory: 128m is therefore about 0.128 bytes. The API accepts it, and the container is then limited to almost nothing.
The other mistakes are easier to spot:
- Writing
memory: 256MBor256mb. Kubernetes rejects this. The valid suffix isMorMi. - Writing
cpu: 500M. A capitalMmeans mega, so this asks for 500 million cores and the pod can never be scheduled. - Mixing up
GandGiwhen you compare against node capacity. A node reporting16Gihas more memory than16G. - Writing a number without quotes in places where YAML might parse it as a float. This is rarely a problem for
cpu: 1, but quote values such as"4"in a ResourceQuota to be safe.
Check what Kubernetes stored
After you apply a manifest, read the values back to confirm they parsed as you intended:
kubectl get pod web -o jsonpath='{.spec.containers[0].resources}'Kubernetes may display a normalized form. For example, 0.5 can come back as 500m. That is the same quantity.
Exercise
A manifest sets cpu: 1500m and memory: 2G in requests. How many cores is that, and why might 2Gi be a better choice for memory?
Show answer
1500m is 1.5 cores. 2G is 2,000,000,000 bytes, while 2Gi is 2,147,483,648 bytes. The binary form matches how nodes report memory, so it is easier to compare against node capacity and avoids a roughly 7% undercount.
Exercise
A container is killed immediately after start, and its manifest says memory: 512m under limits. What is wrong, and what is the fix?
Show answer
The lowercase m means milli-bytes, so the limit is about half a byte. Change the value to 512Mi.