Request pods with default compute resource limits

We could also specify default resource requests and limits for a namespace. The default setting will be used if we don't specify the requests and limits during pod creation. The trick is using a LimitRange object, which contains a set of defaultRequest (requests) and default (limits).

LimitRange is controlled by the LimitRange admission controller plugin. Be sure that you enable this if you launch a self-hosted solution. For more information, check out the Admission Controller section of this chapter.

The following is the example that can be found at chapter8/8-3_management/limit_range.yml:

apiVersion: v1
kind: LimitRange
metadata:
name: limitcage-container
namespace: team-capybara
spec:
limits:
- default:
cpu: 0.5
memory: 512Mi
defaultRequest:
cpu: 0.25
memory: 256Mi
type: Container

When we launch pods inside this namespace, we don't need to specify the cpu and memory requests and limits anytime, even if we have a total limitation set inside the ResourceQuota.

We can also set minimum and maximum CPU and memory values for containers in LimitRange. LimitRange acts differently from default values. Default values are only used if a pod spec doesn't contain any requests and limits. The minimum and maximum constraints are used to verify whether a pod requests too many resources. The syntax is spec.limits[].min and spec.limits[].max. If the request exceeds the minimum and maximum values, forbidden will be thrown from the server:

...
spec:
limits:
- max:
cpu: 0.5
memory: 512Mi
min:
cpu: 0.25
memory: 256Mi

Other than type: Container, there are also Pods and PersistentVolumeClaim types of LimitRange. For a Container type limit range, it asserts containers of Pods individually, so the Pod limit range checks all containers in a pod as a whole. But unlike the Container limit range, Pods and PersistentVolumeClaim limit ranges don't have default and defaultRequest fields, which means they are used only for verifying the requests from associated resource types. The resource for a PersistentVolumeClaim limit range is storage. You can find a full definition at the chapter8/8-3_management/limit_range.yml template.

Aside from absolute requests and the limit constraints mentioned previously, we can also restrict a resource with a ratio: maxLimitRequestRatio. For instance, if we have maxLimitRequestRatio:1.2 on the CPU, then a pod with a CPU of requests:50m and a CPU of limits: 100m would be rejected as 100m/50m > 1.2.

As with ResourceQuota, we can view the evaluated settings by describing either Namespace or LimitRange:

$ kubectl describe namespaces <namespace name>
...
Resource Limits
Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
---- -------- --- --- --------------- ------------- -----------------------
Container memory - - 256Mi 512Mi -
Container cpu - - 250m 500m -
Pod cpu - - - - 1200m
PersistentVolumeClaim storage 1Gi 10Gi - - -
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset