Back to blog

Managing Kubernetes Resources in Your Cluster (CPU, Memory and More)

Itay Gershon

Product Manager, Intel Granulate

What Are Kubernetes Resources, Requests and Limits? 

In Kubernetes, you can specify the amount of CPU and memory that a pod is allowed to use—known as resources—using resource limits and requests. These limits and requests are specified in the pod’s resource configuration. 

The limits field in the pod manifest defines the maximum resources that can be used by the container, while the resources field defines the minimum guaranteed resources that will be reserved for it.

https://granulate.io/gmaestro

For example, to specify the amount of CPU that a pod is allowed to use, you can set the limits.cpu and requests.cpu fields in the resources section of the pod manifest. The values for these fields can be specified in millicores (e.g., 500m for 500 millicores) or as a fraction of a CPU (e.g., 0.5 for half a CPU).

Here is an example of a resource configuration that specifies the pod is allowed to use a maximum of 500 millicores of CPU and 500 megabytes of memory, and it will always be allocated at least 100 millicores of CPU and 100 megabytes of memory:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  – name: my-container
    image: my-image
    resources:
      limits:
        cpu: 500m
        memory: 500Mi
      requests:
        cpu: 100m
        memory: 100Mi

This is a part of a series of articles about Kubernetes architecture.

In this article:

Why You Need to Manage Kubernetes Resources

Managing resources like CPU and memory in Kubernetes clusters, using resource limits and requests or other methods, is important for several reasons:

  • Ensures that pods have the resources they need to run properly: Pods that are allocated too few resources may not be able to perform their intended functions, while pods that are allocated too many resources may waste resources that could be used by other pods.
  • Prevents resource contention within a cluster: If multiple pods are competing for the same resources, it can lead to performance issues and instability. By setting resource limits and requests, you can ensure that pods are only using the resources they need and avoid resource contention.
  • Helps optimize the cost and efficiency of your cluster: By setting appropriate resource limits and requests, you can ensure that your pods are using resources efficiently and not wasting resources. This can help you optimize the cost of running your cluster, especially if you are using a cloud provider that charges for resource usage.
  • Ensures applications have predictable performance: By setting appropriate resource limits and requests, you can ensure that your pods have the resources they need to perform consistently, which can be important for applications that need to meet specific performance or availability requirements.

Kubernetes Resource Types and How to Optimize Them 

CPU

In Kubernetes, the CPU resource refers to the processing power of a node in the cluster. CPU resources are used by pods to execute their workloads, and are typically measured in millicores (1/1000 of a CPU core).

To optimize the use of CPU resources in a Kubernetes cluster, you can follow these best practices:

  • Set appropriate resource limits and requests for your pods: By setting resource limits and requests, you can ensure that your pods are only using the CPU resources they need and avoid resource contention.
  • Use horizontal pod autoscaling (HPA) to automatically scale the number of replicas of your pods based on CPU usage. This can help ensure that your pods have the resources they need to meet the demands of your workload.
  • Use pod priority and preemption to prioritize the most important pods in your cluster. This can help ensure that important pods have the resources they need to function properly, even if other pods are consuming a large amount of resources.
  • Monitor the CPU usage of your pods and nodes to identify potential bottlenecks or resource contention. Tools like the Kubernetes dashboard and the kubectl top command can be helpful for monitoring resource usage.
  • Use tools like kubectl cpulimit and kubectl cgroup-limits to set limits on the CPU usage of specific pods or containers. This can be useful for limiting the CPU usage of resource-intensive workloads or preventing individual pods from consuming too many resources.
  • Use node affinity, taints and tolerations to ensure that pods are scheduled onto nodes with sufficient CPU resources. This can help prevent resource contention and ensure that your pods have the resources they need to function properly.

Memory

In Kubernetes, the memory resource refers to the physical memory available on a node in the cluster. Memory resources are used by pods to store and process data, and are typically measured in bytes.

To optimize the use of memory resources in a Kubernetes cluster, you can follow these best practices:

  • Set appropriate resource limits and requests for your pods: This can ensure pods are only using the memory resources they need and avoid resource contention.
  • Use horizontal pod autoscaling (HPA) to automatically scale the number of replicas of your pods based on memory usage.
  • Monitor memory usage of your pods using tools like the Kubernetes dashboard and the kubectl top command can be helpful for monitoring resource usage.
  • Use tools like kubectl memlimit and kubectl cgroup-limits to set limits on the memory usage of specific pods or containers.
  • Use node affinity, taints and tolerations to ensure that pods are scheduled onto nodes with sufficient memory resources.
  • Use memory-efficient data structures and algorithms in your applications to reduce the overall memory usage of your pods and improve the efficiency of your cluster.
https://granulate.io/gmaestro

Ephemeral Storage and Extended Resources

Ephemeral storage, also known as emptyDir in Kubernetes, is a type of storage that is created dynamically when a pod is scheduled on a node and deleted when the pod is removed from the node. It is typically used to store temporary data that is needed by the containers within the pod, such as log files or application configuration files.

Extended resources refer to resources that are not natively supported by Kubernetes, such as GPU, FPGA, or other specialized hardware. These resources can be used by pods in the same way as CPU and memory, but they require additional configuration and setup to be used.

To optimize the use of ephemeral storage and extended resources, you can:

  • Set requests and limits like you would do with CPU and memory.
  • Carefully consider the requirements of your applications and to allocate the appropriate amount of resources to each pod.
  • Monitor the resource usage of your pods and to adjust the resource limits and requests as needed to ensure that they are being used efficiently. 
  • Carefully plan the deployment and scaling of your pods to ensure that they are placed on nodes that have the resources they need to run. 
Optimize application performance.

Save on cloud costs.

Start Now
Back to blog