In previous Kubernetes based posts I have created a deployment of replicas, a load balancer service and demonstrated how to test that traffic flows to different pods. But what if I wanted to control how much resource a deployment should use?

Resources: Requests and Limits

Kubernetes has an resources option for requests and limits, as per: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

The deployment I have previously used can be modified to use requests and limits:

apiVersion: apps/v1
kind: Deployment
metadata:
name: dep-flask-hello
spec:
replicas: 5
selector:
matchLabels:
name: geektechstuff-flask
template:
metadata:
labels:
name: geektechstuff-flask
spec:
containers:
- name: flask-hello-world
image: geektechstuff/flask-hello-world:latest
ports:
- containerPort: 5000
resources:
requests:
cpu: 0.2
memory: 128Mi
limits:
cpu: 1.0
memory: 256Mi

What Requests and Limits do

Requests informs the Kubernetes controller how much resource the container may need. It’s not a limit and does not stop the pod from asking for more resources. CPU is measured in units with 1 unit being the equivalent of 1 CPU core, and fractional requests are allowed. In the above example I am advising Kubernetes that the pod requires at least 20% of a CPU core. Memory can be asked for in various powers, so caution is recommended. M, m, Mi are all different. In my example I have advised Kubernetes that at least 128 mebibytes (1024 bytes * 128).

Limits informs Kubernetes how much resource the container can use before Kubernetes should take action against the pod (e.g. giving the container an out of memory message). In my example above I have limited it to 1 CPU core and 256 mebibytes.

Container or Pod level

My example is setting the requests and limits at the Container level, e.g. in the spec for the container. However, requests and limits can also be set at the Pod level, e.g. in the spec for the pod.

Why Requests?

Setting Limits seems to have an obvious reason (e.g. to stop a pod or container using all of a nodes resources) buy why use Requests? Informing Kubernetes of how much resource a Pod or Container needs helps Kubernetes to schedule which Node or Nodes the Pod or Containers should be deployed to, which works alongside any Label selectors in use.

Related Posts