Reputation: 742
I am currently building a system that run services in individual deployments in a kubernetes cluster and require the limitation of visible cores inside the container.
Node Specs:
8 Cores
16 GB memory
When I deploy, log into the container and inspect the core count with python's multiprocessing
module I get the following output:
In [1]: import multiprocessing
In [2]: multiprocessing.cpu_count()
Out[2]: 8
This output is correct seeing that my node has 8 cores.
What I would like is for the container to return a core count of 1 as the core count when running the same multiprocessing.cpu_count()
command.
The example use case for this is as follows:
Developer creates an api services running python and gunicorn, gunicorn's default behaviour is to create workers for the number of cores available.
The above will be a problem when the pod is only allowed 100m CPU and 300m Mem as the 8 workers will at some point fill up the memory and the pod will crash, I also would like to hide this for the sake of potential confusion and unexpected behaviour when detecting resources programmatically.
I have tried using the --cpuset-cpus=0
flag as it stood out as a potential solution, but from what I understand and experienced is that this will merely limit the container to the specified core, which does not hide the other 7 cores.
Is there any way of doing this, any help will be appreciated.
Upvotes: 2
Views: 1512
Reputation: 1489
You could have an environment variable NUM_CPUS
that if it's present you should use it instead of multiprocessing.cpu_count()
. Something like:
import os
num_cpus = os.getenv("NUM_CPUS")
if num_cpus is not None:
cpu_count = max(round(num_cpus), 1)
else:
cpu_count = multiprocessing.cpu_count()
Upvotes: -1