Kubernetes: From Zero to Hero (Part 12) - Requests and Limits
Published: 2023-01-01 17:10:36

In Kubernetes, requests and limits are used to specify the minimum and maximum amount of resources (such as CPU and memory) that a pod or container should be allocated.

Requests are the minimum amount of resources that a pod or container should receive. If a pod or container has a request for a particular resource, the scheduler will ensure that the pod or container is allocated at least that much of the resource. This is useful for ensuring that your applications have the resources they need to run effectively.

Limits, on the other hand, are the maximum amount of resources that a pod or container is allowed to consume. If a pod or container exceeds its limit for a particular resource, it may be terminated or throttled to prevent it from consuming too many resources. This can be useful for preventing resource contention and overutilization in your cluster.

Here's an example of how to specify requests and limits in a pod specification:

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    resources:
      limits:
        cpu: "1"
        memory: "500Mi"
      requests:
        cpu: "0.5"
        memory: "200Mi"
```
In this example, we are specifying that the container should be allocated at least 0.5 CPU and 200 MiB of memory, and should not be allowed to consume more than 1 CPU and 500 MiB of memory.

NOTE: that requests and limits are specified in terms of millicores (m) for CPU and mebibytes (Mi) for memory. For example, the value "1" for CPU represents one millicore, and the value "500Mi" for memory represents 500 mebibytes.

It's important to note that requests and limits are not guaranteed to be the exact amount of resources that a pod or container will receive. They are used by the scheduler to make decisions about resource allocation, but the actual allocation may vary based on the available resources in the cluster and the demands of other pods and containers.

Using requests and limits can be a powerful tool for managing resources in your Kubernetes cluster. By specifying the minimum and maximum amounts of resources that your applications need, you can help ensure that they are allocated the resources they need to run effectively, and can also prevent resource contention and overutilization in your cluster.

scheduler to make decisions about resource allocation, but the actual allocation may vary based on the available resources in the cluster and the demands of other pods and containers.

One key use case for requests and limits is to ensure that your applications have the resources they need to run effectively. For example, if you know that your application requires a certain amount of CPU and memory to function properly, you can specify those requirements using requests. This will ensure that the scheduler allocates those resources to your application, and will prevent the application from being starved of resources.

Limits, on the other hand, can be useful for preventing resource contention and overutilization in your cluster. By specifying limits for your pods and containers, you can ensure that they don't consume too many resources, which can help to prevent other pods and containers from being starved of resources.

It's also worth noting that requests and limits can be specified at different levels in your Kubernetes cluster. For example, you can specify requests and limits at the pod level, which will apply to all containers in the pod. You can also specify requests and limits at the container level, which will only apply to that specific container.

In general, it's a good idea to specify both requests and limits for your pods and containers. This will help to ensure that your applications have the resources they need to run effectively, while also preventing resource contention and overutilization in your cluster.

To summarize, requests and limits are powerful tools for managing resources in your Kubernetes cluster. By specifying the minimum and maximum amounts of resources that your applications need, you can help ensure that they are allocated the resources they need to run effectively, and can also prevent resource contention and overutilization in your cluster.

🌟 🔥 If you want to switch your career into tech and you are considering DevOps, you can join our online community [here](https://chat.whatsapp.com/LARpsS1xhYi75TdJYh8TMa) for live classes and FREE tutorial videos.