Kubernetes' Magic Numbers
Published: 2022-03-30 18:16:16

Here is just a small auxiliary note. You may [return to the main text](https://dev.to/otomato_io/kubernetes-statistics-in-your-console-4182).

### Resource types

So, we know CPU and memory are each a resource type in Kubernetes. A resource type has a base unit. CPU is specified in units of cores, and memory is specified in _units_ of _bytes_.

CPU and memory are collectively referred to as _compute resources_, or just _resources_. Compute resources are measurable quantities that can be _requested, allocated, and consumed_. They are distinct from API resources. API resources, such as Pods and Services, are objects that can be read and modified through the Kubernetes API server.

### Resource requests and limits of Pod and Container

Each Container of a Pod can specify one or more of the following:

- `spec.containers[].resources.limits.cpu`
- `spec.containers[].resources.limits.memory`
- `spec.containers[].resources.requests.cpu`
- `spec.containers[].resources.requests.memory`

Although requests and limits can only be specified on individual Containers, it is convenient to talk about _Pod resource requests and limits_. A Pod resource request/limit for a particular resource type is the sum of the resource requests/limits of that type for each Container in the Pod.

### Meaning of CPU

Limits and requests for CPU resources are measured in cpu units. One cpu, in Kubernetes, is equivalent to:

- 1 ☁️ AWS vCPU
- 1 ☁️ GCP Core
- 1 ☁️ Azure vCore
- 1 Hyperthread on a bare-metal Intel processor with Hyperthreading


Fractional requests are allowed. A Container with `spec.containers[].resources.requests.cpu` of `0.5` is guaranteed half as much CPU as one that asks for `1` CPU. The expression `0.1` is equivalent to the expression `100m`, which can be read _as "one hundred millicpu"_. 

Some people say _"one hundred millicores"_, and this is understood to mean the same thing. A request with a decimal point, like `0.1`, is converted to `100m` by the API, and precision finer than `1m` is not allowed. For this reason, the form `100m` might be preferred.

CPU is always _requested as an absolute quantity_, never as a relative quantity; 0.1 is the same amount of CPU on a single-core, dual-core, or 48-core machine.

> If you are not already familiar with the concept of millicores, and how does the operating system kernel even enforce this measure, we at Otomato recommend you [to read this article](https://wbhegedus.me/understanding-kubernetes-cpu-limits/) written by Will Hegedus.

### Meaning of memory

Limits and requests for memory are measured in bytes. You can express memory as a plain integer or as a fixed-point integer using one of these SI suffixes: E, P, T, G, M, K. You can also use the power-of-two equivalents: `Ei`, `Pi`, `Ti`, `Gi`, `Mi`, `Ki`. For example, the following represent roughly the same value: `128974848`, `129e6`, `129M`, `123Mi`

Here's a YAML example. The following Pod has two Containers. Each Container has a request of 0.25 cpu and 64MiB (2^26 bytes) of memory Each Container has a limit of 0.5 cpu and 128MiB of memory. You can say the Pod has a request of 0.5 cpu and 128 MiB of memory, and a limit of 1 cpu and 256MiB of memory.

```
apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
  - name: db
    image: mysql
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
  - name: wp
    image: wordpress
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
```
If you want to find out the actual resource limit/request, then you can see the value by describing the deployment or the pod, for example: `kubectl describe deployment <name>` or `kubectl describe pod <name>`.

You may [return to the main text](https://dev.to/otomato_io/kubernetes-statistics-in-your-console-4182).