LimitRange is an API object that limits resource usage per container or Pod in a Namespace It uses three relevant options:
type: specifies whether it applies to Pods or containers defaultRequest: the default resources the application will request default: the maximum resources the application can use
I will create a specific deployment and i will set up high memory usage to exceed limit range and see what happen
apiVersion:apps/v1kind:Deploymentmetadata:name:busybox-deploynamespace:limitedspec:selector:matchLabels:app:busybox-podtemplate:metadata:labels:app:busybox-podspec:containers: - name:busyboximage:busyboxcommand: - sh - -c - sleep 3600resources:requests:memory:"700Mi"cpu:"500m"limits:memory:"700Mi"cpu:"500m"
Let’s see what happend
The pod is not created, and if i check the replicaset (since it is the close on to the pod)
touk@k8smaster:~/Documents/k8s/ResourceQuota&LimitRange$ k describe replicaset.apps/busybox-deploy-794845987 -n limitedName:busybox-deploy-794845987Namespace:limitedSelector:app=busybox-pod,pod-template-hash=794845987Labels:app=busybox-podpod-template-hash=794845987Annotations:deployment.kubernetes.io/desired-replicas:1deployment.kubernetes.io/max-replicas:2deployment.kubernetes.io/revision:1Controlled By:Deployment/busybox-deployReplicas:0 current / 1 desiredPods Status:0 Running / 0 Waiting / 0 Succeeded / 0 FailedPod Template:Labels:app=busybox-podpod-template-hash=794845987Containers:busybox:Image:busyboxPort:<none>Host Port:<none>Command:sh-csleep 3600Limits:cpu:500mmemory:700MiRequests:cpu:500mmemory:700MiType Status Reason---- ------ ------ReplicaFailure True FailedCreateEvents:Type Reason Age From Message---- ------ ---- ---- -------Warning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-sl449" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-hbdfn" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-n97gq" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-bc7s4" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-sbhtx" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-t55kl" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-9dvxh" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 86s replicaset-controller Error creating:pods "busybox-deploy-794845987-jcgmg" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 85s replicaset-controller Error creating:pods "busybox-deploy-794845987-bpmj4" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700MiWarning FailedCreate 4s (x6 over 84s) replicaset-controller (combined from similar events):Error creating:pods "busybox-deploy-794845987-f9xh5" is forbidden:maximum memory usage per Container is 500Mi, but limit is 700Mi
The ReplicSet failed to create the pod becuse it’s demands are higher than what the limit range set
Quota
Quota is an API object that limits total resources available in a Namespace If a Namespace is configured with Quota, applications in that Namespace must be configured with resource settings in pod.spec.containers.resources
Where the goal of the LimitRange is to set default restrictions for each application running in a Namespace, the goal of Quota is to define maximum resources that can be consumed within a Namespace by all
I will define a resource quota manifest file that wil be bound to a new name space called ‘team’
This will limit resources of memory and cpu on the team namespcae
I will create a deployment
apiVersion:apps/v1kind:Deploymentmetadata:name:busybox-deploynamespace:teamspec:selector:matchLabels:app:busybox-podtemplate:metadata:labels:app:busybox-podspec:containers: - name:busyboximage:busyboxcommand: - sh - -c - sleep 3600resources:requests:memory:"100Mi"
So when i try to scale it up to 7 replicas, it wont, because each pod is requesting 100Mi from memory and 5 pods will be the maximum and that’s why i have only 5 pods