Kubernetes – Running Multiple Threads in a Pod with CPU Limit

cpu-usagekubernetesmulti-threadingthreads

I have a multi cores Kubernetes cluster, inside it I have multiple pods configured with a CPU limit of 500 millicores:

        resources:
          limits:
            cpu: "500m"

It is possible to have, in a single pod, more than one threads running in parallel (simultaneously, at the same exact moment)?

From my understanding when the limit is less than 1000 millicores the pod can never have multiple threads running in parallel. Is it correct?

Best Answer

Yes, CPU limits in Kubernetes are implemented using the Linux CPU quota subsystem (at least on Linux, not sure on Windows). That system works by giving the cgroup a total count of timeslices it is allowed to run, and refilling that every few milliseconds. If a task (thread, process) is marked as runnable and the group it is in has available quota tokens then it will run just as it always would (and the bucket is decremented). If there are no tokens then it won't run and a timeslice_exceeded event is emitted.

Setting a limit of 500m means the token refill rate will average out to 0.5 seconds of runtime allowed for every 1 second of wall clock time. But if you have a million cores then you could use all those tokens in one jiffy if your tasks were all runnable.

Related Topic