Sam YC
Sam YC

Reputation: 11637

What is the meaning of CPU and core in Kubernetes?

I don't really understand this after reading through the document. Some use a term like "CPU", but some use "core".

I am running Kubernetes in my laptop for testing purpose. My laptop has one CPU (2.2 GHz) and four cores.

If I want to set the CPU request/limit for pod, should the maximum resource that I have be 1000m or 4000m?

What is the difference (CPU vs. core) here in a Kubernetes context?

Upvotes: 47

Views: 98138

Answers (4)

prisoner_of_azkaban
prisoner_of_azkaban

Reputation: 758

As mentioned in Assign CPU Resources to Containers and Pods, it clearly says that:

The CPU resource is 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

So, setting argument -cpus "2" tells the container to attempt to use two CPUs.

Also, 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.

Upvotes: 1

Rico
Rico

Reputation: 61699

To clarify what's described here in the Kubernetes context, 1 CPU is the same as a core (Also more information here).

1000m (milicores) = 1 core = 1 vCPU = 1 AWS vCPU = 1 GCP Core.
100m (milicores) = 0.1 core = 0.1 vCPU = 0.1 AWS vCPU = 0.1 GCP Core.

For example, an Intel Core i7-6700 has four cores, but it has Hyperthreading which doubles what the system sees in terms of cores. So in essence, it will show up in Kubernetes as:

8000m = 8 cores = 8 vCPUs

Some extra information: These resources are managed by the kube-scheduler using the Completely Fair Scheduler (CFS), and there are no guarantees in terms of overruns within the same machine and your pod may be moved around.

If you'd like to have stronger guarantees, you might consider the --cpu-manager-policy=static (CPU Manager) option in the kubelet. More information is here and here.

For more details on what your system sees as a CPU (and number of CPUs) on a Linux system you can see how many vCPUs you have by running cat /proc/cpuinfo.

Upvotes: 68

Sam YC
Sam YC

Reputation: 11637

I personally think that the apisim solution is more suitable for my problem.

I run the commands and find out I only have two allocatable cores:

Enter image description here

I couldn't understand why until I found this:

Enter image description here

Rico is right about my laptop should have eight cores due to hyper-threading. But the Docker default settings throttle it to two.

Upvotes: 2

apisim
apisim

Reputation: 4606

To remove any guesswork regarding your laptop or any other environment, execute:

kubectl get nodes

...and then this for a particular node:

kubectl describe node <node-name>

Look for cpu under Allocatable (which may have the same value as under 'Capacity').

Take into consideration that

1 CPU = 1000 millicores/millicpu

when setting "fractional" resources.requests.cpu and resources.limits.cpu for containers.

Upvotes: 10

Related Questions