Summary

It is important to remember that you cannot set requests that are larger than resources provided by your nodes. Soft limit = Request. Hard limit = Limit.

Refs

https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-resource-requests-and-limits

Vocabulary

CPU

CPU resources are defined in millicores. 1000m = 1 core

  • Example

    If your container needs two full cores to run, you would put the value “2000m”. If your container only needs ¼ of a core, you would put a value of “250m”.

Memory

mebibyte=megabyte but you can give anything from bytes to petabytes.

Requests and Limits, Pods and container level

limit >= requests Requests and limits are the mechanisms Kubernetes uses to control resources such as CPU and memory. Requests are what the container is guaranteed to get. If a container requests a resource, Kubernetes will only schedule it on a node that can give it that resource. Limits, on the other hand, make sure a container never goes above a certain value. The container is only allowed to go up to the limit, and then it is restricted.

It is important to remember that the limit can never be lower than the request. If you try this, Kubernetes will throw an error and won’t let you run the container.

Check resource limitations applied

apiVersion: apps/v1
kind: Deployment
metadata:
  name: assessment-deployment
  namespace: assessment
  labels:
    app: node
spec:
  replicas: 1
  selector:
    matchLabels:
      app: node
  template:
    metadata:
      labels:
        app: node
    spec:
      containers:
      - name: assessment
        image: jaaved9/assessment:latest
        ports:
        - containerPort: 3000
        resources:
            limits:
              memory: 512Mi
            requests:
              memory: 256Mi
 kubectl get pods assessment-deployment-747f9ff76d-c7cft -o jsonpath='range .spec.containers[*]"Container Name: ".name"\n""Requests:".resources.requests"\n""Limits:".resources.limits"\n"end' -n assessment
# result
Container Name: assessment
Requests:"cpu":"200m","memory":"256Mi"
Limits:"cpu":"250m","memory":"512Mi"

ResourceQuotas, namespace level

 apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-resources
spec:
  hard:
    pods: "4"
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi

LimitRange, namespace and container level

You can also create a LimitRange in your Namespace. Unlike a Quota, which looks at the Namespace as a whole, a LimitRange applies to an individual container.

apiVersion: v1
kind: LimitRange
metadata:
  name: mem-min-max-demo-lr
spec:
  limits:
  - max:
      memory: 1Gi
    min:
      memory: 500Mi
    type: Container