This blog is a continuation of previous blog series https://goglides.io/2020/03/03/limit-range-kubernetes/

Limits/Requests Ratio

If LimitRangeItem.maxLimitRequestRatio is specified in the LimitRangeSpec,

  • the named resource must have a request and limit
  • both request and limit should be non-zero (>0)
  • limit divided by request is less than or equal to the enumerated value

Following manifest (limitrange-limit-request-ratio.yaml) saying limit to be at most twice the amount of memory request,

apiVersion: v1
kind: Namespace
metadata:
  name: limitrange-demo3
---
apiVersion: v1
kind: LimitRange
metadata:
  name: limit-memory-ratio-pod
  namespace: limitrange-demo3
spec:
  limits:
  - maxLimitRequestRatio:
      memory: 2
    type: Pod

apply YAML,

kubectl apply -f limitrange-limit-request-ratio.yaml

Output:
namespace/limitrange-demo3 created
limitrange/limit-memory-ratio-pod created

Describe the limitrange,

kubectl describe -f limitrange-limit-request-ratio.yaml

Output:
Name:         limitrange-demo3
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"limitrange-demo3"}}
Status:       Active

No resource quota.

Resource Limits
 Type  Resource  Min  Max  Default Request  Default Limit  Max Limit/Request Ratio
 ----  --------  ---  ---  ---------------  -------------  -----------------------
 Pod   memory    -    -    -                -              2


Name:       limit-memory-ratio-pod
Namespace:  limitrange-demo3
Type        Resource  Min  Max  Default Request  Default Limit  Max Limit/Request Ratio
----        --------  ---  ---  ---------------  -------------  -----------------------
Pod         memory    -    -    -                -              2

Now let’s test the constraint working or not,

First test failed conditions (invalid-limit-request.yaml)

apiVersion: v1
kind: Pod
metadata:
  name: busybox3
  namespace: limitrange-demo3
spec:
  containers:
  - name: busybox-cnt01
    image: busybox
    resources:
      limits:
        memory: "300Mi"
      requests:
        memory: "100Mi"

apply YAML

kubectl apply -f invalid-limit-request.yaml

Output:
Error from server (Forbidden): error when creating "limitrange-limit-request-ratio.yaml": pods "busybox3" is forbidden: memory max limit to request ratio per Pod is 2, but provided ratio is 3.000000

Here pod is failed to deploy because 300/100 > 2.

Now test with valid conditions (valid-limit-request.yaml)

apiVersion: v1
kind: Pod
metadata:
  name: busybox4
  namespace: limitrange-demo3
spec:
  containers:
  - name: busybox-cnt01
    image: busybox
    resources:
      limits:
        memory: "150Mi"
      requests:
        memory: "100Mi"

apply YAML

kubectl apply -f valid-limit-request.yaml

Output:
pod/busybox4 created