Resource Requirements and Limits

Resource Requirements and Limits

In this tutorial we will discuss about Resource Requirements and Limits in Kubernetes. Requirements and limits are the mechanisms Kubernetes uses to control resources such as CPU and memory.

Resource Requirements and Limits

Let us look at a 3 node Kubernetes cluster. Each node has a set of CPU, memory and Disk resources available. Every POD consumes a set of resources. In this case 2 CPU’s, one memory and some disk space.

Whenever a POD is placed on a node, it consume resources available to that node. As we have discussed before, it is the Kubernetes scheduler that decides which node a POD goes to. The scheduler takes into consideration, the amount of resources required by a POD and those available on the nodes.

In this case, the scheduler schedules a new POD on node 2. If the node has no sufficient resources, the scheduler avoids placing the POD on that node. Instead places the POD on one where the sufficient resources are available.

If there is no sufficient resources available on any of the node, Kubernetes holds back scheduling the POD, and you will see the POD in pending state.

Let us now focus on the resource requirements for each POD. What are these blocks and what are their values?

Resource Requests

By default, Kubernetes assumes that a POD or a container within a POD requires 0.5 CPU and 256 Mebibyte of memory. This is known as the resource request for a container. The minimum amount of CPU or memory requested by the container.

Resource Requirements and Limits

When the scheduler tries to place the POD on a node, it uses these numbers to identify a node which has sufficient amount of resources available.

Now if you know that your application will need more than these you can modify these values by specifying them in your POD or deployment definition files.

apiVersion: v1
kind: Pod
metadata:
  name: my-webapp
  labels:
    name: my-webapp
spec:
  caontainers:
    - name: my-webapp
      image: my-webapp:2.0.0
      ports:
        - containerPort : 8080
      resources:
        requests:
          memory: "512Mi"
          cpu: 1 
Resource Limits

Let’s now look at a container running on a node. In the docker world a docker container has no limit to the resources it can consume on a node. Say a container starts with 1 vCPU on a node, it can go up and consume as much resource as it requires.

However you can set a limit for the resource usage on these PODs by default. Kubernetes sets a limit of 1 vCPU to containers. So if you don’t specify explicitly, a container will be limited to consume only 1 vCPU from the Node.

The same goes with memory. By default, Kubernetes sets a limit of 512 Mebibyte on containers. If you don’t like the default limits, you can change them by adding limit section under the resources section in you POD definition file.

apiVersion: v1
kind: Pod
metadata:
  name: my-webapp
  labels:
    name: my-webapp
spec:
  caontainers:
    - name: my-webapp
      image: my-webapp:2.0.0
      ports:
        - containerPort : 8080
      resources:
        requests:
          memory: "512Mi"
          cpu: 1 
        limits:
          memory: "1Gi"
          cpu: 2

Please note that the limits and requests are set for each container within the POD.

Exceed Limits

What happens when a POD tries to exceed resources beyond its specified limit.

In case of the CPU, Kubernetes throttles the CPU so that it doesn’t go beyond the specified limit. A container cannot use more CPU resources than its limit.

However, this is not the case with memory. A container can use more memory resources than its limit. So if a POD tries to consume more memory than its limit constantly, the POD will be terminated.

Resource Requirements and Limits
Scroll to top