Kubernetes LimitRange provides a way to limit the maximum number of resources that can be allocated to a pod.
This can be used to restrict the amount of CPU, Memory, and Storage that a pod can consume. This can be useful in cases where you want to prevent a pod from consuming too many resources and affecting the performance of other pods on the same node.
The default behaviour is that a pod can consume all the resources available to it. So if we don’t put any limit on Kubernetes, then there would be no guarantee that our pod will do well and won’t get terminated or restarted anytime because of insufficient resource availability.
Create a LimitRange
To create a LimitRange, we need to create a manifest file (.YAML) with the LimitRange configuration (CPU, RAM, etc.). Below I have a limit range (256MB) for new containers that are being deployed to the dev namespace.
apiVersion: v1 kind: LimitRange metadata: name: limit-range-ram spec: limits: - default: memory: 256Mi defaultRequest: memory: 256Mi type: Container
To deploy the LimitRange, I will run the following command.
kubectl apply -f limitrange.yaml --namespace dev
Once the limit is set, every new container deployed to the dev namespace will get it.