Managing Container Resources in Kubernetes

Gaurav Kaushik
3 min readMay 15, 2021

--

Kubernetes provides various mechanisms or knobs to the user in order to define the amount of resources(CPU, Memory) which the container may require or atleast the user thinks the container may require.
Also, there are scenarios where the user may want to limit the resources to be given to containers.

In this post we will discuss such mechanisms.

  1. Resource Requests:
    An example of Resource Request in a Pod definition:

Resource Requests allow the user to define an amount of resources(such as CPU or Memory) which is expected from a container to use. The Kubernetes Scheduler will use Resource Request to avoid scheduling pods on nodes that do not have enough available resources.

Note: Containers are allowed to use more(or less) than the requested resources. Resource Requests only affect Scheduling.

Memory is measured in bytes. CPU is measured in CPU units, which are 1/1000 of 1 CPU.

2. Resource Limits:
Resource Limits provide a way for the user to limit the amount of resources your containers can use. The container runtime is responsible for enforcing these limits, and different container runtimes do this differently.
Note: Some Container runtimes will enforce these limits by terminating container processes that attempt to use more than the allowed amount of resources.
An example of Resource Limit in a Pod definition:

Lets try a hands-on which will demonstrate whether the user is able to deploy a pod with resource values(CPU, Memory) which is greater than the resources available with the Worker node.

  1. Create a pod with 10 CPUs:

2. Create this Pod & check if it goes to Running state or not:

Pod stuck in ‘Pending’ state

This is because the worker node itself doesnot have 10 CPUs, therefore the Pod will remain in the Pending state

3. Let us change the of CPU and enforce Resource Limit too:

This time the Pod will transition to ‘Running’ state as the values of the Resources is lesser than the available resources with the Worker node.

That is all for this topic.
In the next post we will understand How to monitor Container Health using Probes.

Thank You!

--

--