Understanding LimitRange in Kubernetes
Kubernetes LimitRange is a resource that allows you to set constraints on the amount of resources (CPU, memory, storage, etc.) that can be consumed by a single pod or container within a namespace. This is particularly useful in multi-tenant environments where you want to ensure that one tenant's workload doesn't consume all the available resources, leaving none for other tenants.
What is LimitRange?
LimitRange is a Kubernetes resource that allows you to set the following types of limits:
- Default Request/Limit: Specifies the default CPU and memory requests and limits for containers if they are not explicitly set.
- Minimum and Maximum Request/Limit: Specifies the minimum and maximum CPU and memory requests and limits for containers.
- Default Container Limit: Specifies the default CPU and memory limits for a container if they are not explicitly set.
- Maximum Limit/Request Ratio: Specifies the maximum ratio between a container's limit and request for CPU and memory.
By setting these limits, you can ensure that your cluster resources are used efficiently and that no single workload can monopolize the available resources.
Applying LimitRange
To apply a LimitRange to your Kubernetes cluster, you need to create a LimitRange object and apply it to a namespace. Here's an example LimitRange configuration:
apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
max:
memory: 1Gi
min:
memory: 5Mi
type: Container
In this example, the LimitRange sets the following limits:
- Default memory request and limit for containers is 256Mi and 512Mi, respectively.
- Minimum memory limit for containers is 5Mi.
- Maximum memory limit for containers is 1Gi.
You can apply this LimitRange to a namespace using the kubectl apply
command:
kubectl apply -f limit-range.yaml -n my-namespace
After applying the LimitRange, any pods created in the my-namespace
namespace will be subject to the specified limits.
Verifying LimitRange
You can verify the LimitRange settings by using the kubectl describe limitrange
command:
kubectl describe limitrange mem-limit-range -n my-namespace
This will show you the details of the LimitRange, including the configured limits.
By understanding and properly configuring LimitRange, you can ensure that your Kubernetes cluster resources are used efficiently and that no single workload can monopolize the available resources.