We will create four pods:
Request
cpu = 0.5 and memory = 1GLimit
cpu = 0.5 and memory = 1GLimit
cpu = 1 and memory = 1GRequest
of cpu = 1/memory = 1G and Limit
cpu = 1.8/memory=2GFollow the instructions in the module Deploy the Metrics Server to enable the Kubernetes Metrics Server.
Verify that the metrics-server deployment is running the desired number of pods with the following command.
kubectl get deployment metrics-server -n kube-system
Output:
CPU units are expressed as 1 CPU or 1000m, which equals to 1vCPU/Core. Additional details can be found here
In order to generate cpu and memory load we will use stress-ng with the following flags.
# Deploy Limits pod with hard limit on cpu at 500m but wants 1000m
kubectl run --limits=memory=1G,cpu=0.5 --image hande007/stress-ng basic-limit-cpu-pod --restart=Never -- --vm-keep --vm-bytes 512m --timeout 600s --vm 1 --oomable --verbose
# Deploy Request pod with soft limit on memory
kubectl run --requests=memory=1G,cpu=0.5 --image hande007/stress-ng basic-request-pod --restart=Never -- --vm-keep --vm-bytes 2g --timeout 600s --vm 1 --oomable --verbose
# Deploy restricted pod with limits and requests wants cpu 2 and memory at 1G
kubectl run --requests=cpu=1,memory=1G --limits=cpu=1.8,memory=2G --image hande007/stress-ng basic-restricted-pod --restart=Never -- --vm-keep --vm-bytes 1g --timeout 600s --vm 2 --oomable --verbose
# Deploy Limits pod with hard limit on memory at 1G but wants 2G
kubectl run --limits=memory=1G,cpu=1 --image hande007/stress-ng basic-limit-memory-pod --restart=Never -- --vm-keep --vm-bytes 2g --timeout 600s --vm 1 --oomable --verbose
Check if pods are running properly. It is expected that basic-limit-memory-pod is not not running due to it asking for 2G of memory when it is assigned a Limit
of 1G.
kubectl get pod
Output:
Next we check the current utilization
# After at least 60 seconds of generating metrics
kubectl top pod
Output:
Running multiple stress-ng on the same node will consume less CPU per pod. For example if the expected CPU is 1000 but only running 505 there may be other pods on the nodes consuming CPU.
Kubernetes Requests
and Limits
can be applied to higher level abstractions like Deployment
.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: stress-deployment
name: stress-deployment
spec:
replicas: 1
selector:
matchLabels:
app: stress-deployment
template:
metadata:
labels:
app: stress-deployment
spec:
containers:
- args:
- --vm-keep
- --vm-bytes
- 1500m
- --timeout
- 600s
- --vm
- "3"
- --oomable
- --verbose
image: hande007/stress-ng
name: stress-deployment
resources:
limits:
cpu: 2200m
memory: 2G
requests:
cpu: "1"
memory: 1G
Clean up the pods before moving on to free up resources
kubectl delete pod basic-request-pod
kubectl delete pod basic-limit-memory-pod
kubectl delete pod basic-limit-cpu-pod
kubectl delete pod basic-restricted-pod