kubernetes Resource Limits - ghdrako/doc_snipets GitHub Wiki
apply resource requests and limits for a Pod
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: app
image: images.my-company.example/app:v4
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
- name: log-aggregator
image: images.my-company.example/log-aggregator:v6
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Resource requests and limits are specified for each container. The pod also tracks this information in totality using two fields:
- Resource.requested: equal to the sum of requested resources for all of its containers
- Resources.limited: equal to the sum of limited resources for all of its containers.
These pod fields are used to locate which node is best for scheduling the pod. The Kubernetes control plane’s algorithms compare the resource requests to the available resources on each node in the cluster, and automatically assign a node to the pod being provisioned.
CPU Resource Requests and Limits
Limits and request for CPU resources are measured in millicores. If your container needs one full core to run, you would put the value 1000m.
If your container only needs ¼ of a core, you would put a value of 250m.
If you accidentally provide a value larger than the core count of your biggest node your pod will never be scheduled.
CPU is considered as a compressible resource, instead of terminating the container, Kubernetes starts throttling it which could result in a slow-running application that is difficult to diagnose.
Memory Resource Requests and Limits
Limits and requests for Memory resources are defined in bytes. Placing a memory request larger than the amount of memory on your nodes causes the pod to not get scheduled. Unlike how a container throttles its pod when CPU limits have been met, pod might simply terminate when its memory limit is met. If the pod is restartable, the kubelet will restart it.
Setting Requests & Limits Via Namespaces
only create 2 pods in the default namespace:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ResourceQuota
metadata:
name: pod-examples
spec:
hard:
pods: "2"
EOF
Limit Ranges
Preventing one object to utilize all of the available resources, essentially starving out the others.
specify a maximum limit of 1GB and a minimum limit of 500MBs
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: LimitRange
metadata:
name: min-max-memory-demo
spec:
limits:
- max:
memory: 1Gi
min:
memory: 500Mi
type: Container
EOF
When you create a pod with memory less than the min-memory or greater than the max memory, the pod creation will fail and display the following error:
Error from server (Forbidden): error when creating "STDIN": pods "memory-demo-pod" is forbidden: [minimum memory usage per Container is 500Mi, but request is 100Mi, maximum memory usage per Container is 1Gi, but limit is 1536Mi]