How to set default CPU and memory limits for Kubernetes pods?

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes, the popular container orchestration platform, provides the ability to set resource limits for your pods. In this tutorial, we will explore how to set default CPU and memory limits for Kubernetes pods, ensuring efficient resource utilization and preventing resource exhaustion in your Kubernetes clusters.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/edit("`Edit`") kubernetes/BasicCommandsGroup -.-> kubernetes/set("`Set`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/describe -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} kubernetes/create -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} kubernetes/get -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} kubernetes/edit -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} kubernetes/set -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} kubernetes/apply -.-> lab-415056{{"`How to set default CPU and memory limits for Kubernetes pods?`"}} end

Kubernetes CPU and Memory Limits

Kubernetes provides the ability to set CPU and memory limits for containers running in pods. These limits ensure that each container has access to the required resources and prevents them from consuming more resources than they need.

Understanding CPU and Memory Limits

In Kubernetes, CPU and memory resources are defined in the following units:

  • CPU: Measured in millicores (m). 1 core = 1000m.
  • Memory: Measured in bytes. You can use the suffixes "E", "P", "T", "G", "M", "K" to represent exponential values.

When you set a CPU or memory limit for a container, Kubernetes guarantees that the container will never exceed that limit. If a container tries to use more resources than its limit, Kubernetes will throttle the container's CPU usage or terminate the container if it exceeds the memory limit.

Applying CPU and Memory Limits

You can set CPU and memory limits for a container in the container's resource requirements. Here's an example YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: nginx
      resources:
        limits:
          cpu: 500m
          memory: 512Mi
        requests:
          cpu: 250m
          memory: 256Mi

In this example, the container has a CPU limit of 500 millicores (0.5 cores) and a memory limit of 512 MiB. The container also has CPU and memory requests, which are the minimum resources the container needs to run.

Setting Default Resource Limits

In Kubernetes, you can set default CPU and memory limits for all pods in a namespace. This ensures that every pod that is created in the namespace will have a set of default resource limits, even if the pod's manifest doesn't specify any resource limits.

Configuring Default Resource Limits

To set default resource limits for a namespace, you can use the LimitRange resource. Here's an example YAML manifest:

apiVersion: v1
kind: LimitRange
metadata:
  name: default-limit-range
spec:
  limits:
    - default:
        cpu: 500m
        memory: 512Mi
      defaultRequest:
        cpu: 250m
        memory: 256Mi
      type: Container

In this example, the LimitRange resource sets the following default limits for the namespace:

  • Default CPU limit: 500 millicores (0.5 cores)
  • Default memory limit: 512 MiB
  • Default CPU request: 250 millicores (0.25 cores)
  • Default memory request: 256 MiB

These default limits will be applied to any pod created in the namespace, unless the pod's manifest explicitly specifies different resource limits.

Verifying Default Resource Limits

You can verify the default resource limits by creating a pod in the namespace and inspecting its resource requirements:

## Create a pod without any resource limits
kubectl run my-pod --image=nginx --namespace=my-namespace

## Inspect the pod's resource requirements
kubectl get pod my-pod -o yaml --namespace=my-namespace

The output will show that the pod has the default resource limits and requests applied, even though they were not specified in the pod's manifest.

Applying Default Limits to Deployments

When you have set default resource limits for a namespace using the LimitRange resource, those limits will be automatically applied to any pods created in that namespace, including pods created by Deployments.

Deploying an Application with Default Limits

Let's deploy a simple Nginx application in a namespace with default resource limits:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: nginx

In this example, the Deployment doesn't specify any resource limits or requests. However, since we have a LimitRange resource configured in the my-namespace namespace, the pods created by this Deployment will automatically have the default resource limits and requests applied.

Verifying Default Limits on Deployments

You can verify the default resource limits by inspecting the pods created by the Deployment:

## List the pods in the namespace
kubectl get pods -n my-namespace

## Inspect the resource requirements of a pod
kubectl get pod my-pod-123 -n my-namespace -o yaml

The output will show that the pod has the default resource limits and requests applied, even though they were not specified in the Deployment manifest.

By using default resource limits, you can ensure that all pods in a namespace have a consistent set of resource constraints, making it easier to manage and monitor resource usage across your Kubernetes cluster.

Summary

By the end of this tutorial, you will have learned how to set default CPU and memory limits for Kubernetes pods, ensuring that your applications have the necessary resources to run efficiently while preventing resource exhaustion. This knowledge will help you optimize the performance and reliability of your Kubernetes-based applications.

Other Kubernetes Tutorials you may like