Kubernetes: Resource Requirements and Limits

In the last article we studied about node Affinity which is a way which allows us to put pods on a node which meets certain criteria. Now resource management ( CPU, memory etc) is also an important consideration while deploying pod and it should be adequate i.e no more no less.

The resource availability is taken care by kubernetes scheduler as it place the pod on a node with sufficient amount of resources.If no node is there where resource requirements are met, then kubernetes hold back scheduling the pod and we get to see that pod is in pending state.

By default Kubernetes assumes that each pod requires 0.5 CPU and 256Mi of memory.

We can change this default values by mentioning the new ones in pod definition file.

Lowest value that can be specified for CPU is 0.1 which is also equal to 100m (mili)

The default nature of container is that it will start with the default resource quantity and can consume as much as it wants from the host machine and can ultimately suffocate the resources on the host machine. To prevent this, its good to set resource limits.We can definitely set our own limits but kubernetes by default sets a limit of 1 vcpu and 512 Mib of memory.These limits are set for each container in a pod.

If a container tries to consume more cpu than its limit, kubernetes throttles the request for CPU but this is not the case with memory, container is alloewd to consume more memory but if it does so continuously, will be terminated.

Let's look at below pod definition file

[root@node1 kubernetes]# cat cpu_request_limits.yaml
apiVersion: v1
kind: Pod
  name: cpu-demo
  namespace: default
  - name: cpu-demo-ctr
    image: vish/stress
        cpu: "1"
        cpu: "0.5"
    - -cpus
    - "2"

[root@node1 kubernetes]# kubectl create -f cpu_request_limits.yaml
[root@node1 kubernetes]# kubectl get pods | grep -i cpu
cpu-demo                            1/1     Running   0          7m57s

Now the pod is running , let us check how much resource is it consuming.

[root@node1 kubernetes]# kubectl get pod cpu-demo -o yaml

Note that although in the pod definition file we specified in the args that pod has to start with 2 cpu but it didn't and will not be able to as we have set limits as maximum of 1 cpu and it can request 0.5 cpu.

So that is how resource limits are set on the pods. Hope this was helpful. Next article we are going to cover Daemon sets.

So Stay tuned !!!!

200 views0 comments

Recent Posts

See All