Part 3 - Managing Resources

Part 3 - Managing Resources

Kubernetes for Beginners series

To each Pod, it will be assigned CPU and Memory resources. A resource request for containers in a Pod is used by the Kube-scheduler [1] to allocate the Pod in the appropriate K8s node. A Pod can use more resources than the ones requested as they are available on the node. But defining resources limits the Pod cannot use more than those limits. The Kubelet enforces those limits and reserves the same amount of resources on the node.

CPU and Memory

CPU represents compute processing and is specified in units of Kubernetes CPUs. One K8s CPU unit is equivalent to 1 physical CPU core or one virtual core depending if the K8s node is a physical host or a virtual machine. The CPU units can be fractional: if you request 0.5 CPU time means that you request half of a CPU core time. CPU units also can be expressed with the suffix "m", so a 100m means "one hundred milicpu". A 0.1 in fractional is equivalent to a 100m.

Memory limits and resources are measured in bytes. [2] You can express memory as a plain integer or as a fixed-point number using one of these quantity suffixes: E, P, T, G, M, k. You can also use the power-of-two equivalents: Ei, Pi, Ti, Gi, Mi, Ki. For example, the following represent roughly the same value:

128974848, 129e6, 129M, 128974848000m, 123Mi

Resource Request and Resource Limit

When defining a Resource Request you are setting the minimum amount of computing resources needed to start a Pod. This setting is used by the Kubescheduler to select the appropriate node to allocate the pod. If no node with sufficient resources is found, the pod will be in an eternal pending status:

> kubectl get pod <pod_name>
NAME                        READY   STATUS    RESTARTS   AGE
my-pod-resources-requests   0/1     Pending   0          20m

Code block 1: Status of a pod with insufficient computing resources

> kubectl describe pod <pod_name>

Events:
  Type     Reason            Age                  From               Message
  ----     ------            ----                 ----               -------
  Warning  FailedScheduling  17m                  default-scheduler  0/1 nodes are available: 1 Insufficient cpu, 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..  
  Warning  FailedScheduling  7m22s (x2 over 12m)  default-scheduler  0/1 nodes are available: 1 Insufficient cpu, 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..

Code block 2: Detailed description of a Pod with insufficient computing resources

As the Resource Request will allow for a Pod to consume all the node's free resources it is needed to limit that consumption. That is done with Resource Limits.

A Resource Limit is the maximum amount of computing resources that a pod can consume from a node.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-resources-requests
spec:
  containers:
    - name: demo
      image: alpine
      resources:
        requests: # minimum reources to start the pod
          memory: 100Mi
          cpu: "0.5"
        limits:   # maximum amount of resources the pod can consume
          memory: 600Mi
          cpu: 1
      command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]

References

[1] “kube-scheduler,” Kubernetes. Available: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-scheduler/. [Accessed: Dec. 03, 2023]

[2] “Resource Management for Pods and Containers,” Kubernetes. https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/