Kubernetes : Assign CPU Resource Defaults and Limits to Containers

  • How to declare the CPU resource requirements of your Pods
  • How Kubernetes administrators define default CPU resource requirements
  • How Kubernetes administrators define CPU resource usage limits
  • Pod declares CPU request and limits
  • Pod defines using half a CPU — Millicores syntax
  • Pod defines using half a CPU — fractional syntax
  • Pod declares no CPU request and limits
  • CPU LimitRanges
  • LimitRange with defaults and min, max values
  • Pod that does not adhere to CPU limits
  • LimitRanges and Namespaces

1) Pod Declares CPU Request and Limits

  • Pod declares it needs 1 full CPU at maximum
  • Pod declares it needs half a CPU at minimum
  • 1, 2, 2.5 … defines 1, 2 and 2.5 CPUs
  • 1000m, 2500m, 150m … defines 1 CPU, 2.5 CPUs and 0.150 CPUs
nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: "1"
requests:
cpu: 500m

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
  • — threads=1 … run using 1 thread
  • — cpu-max-prime=8005005 … calculation of prime numbers up to this value
  • — verbosity=0 … do not show details while doing the run
  • cpu run … run the CPU run benchmark included in sysbench
kubectl exec mybench-pod -i -t -- /bin/bash
sysbench --threads=1 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 203 total,   1 running, 202 sleeping,   0 stopped,   0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 1.7 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 1.7 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
31866 root 20 0 87.8m 5.1m 100.0 0.3 0:03.37 S sysbench --threads=1 --cpu-max-prime=8005005 --verbosity+
  • press letter o … this starts its filter functionality
  • in our specific case: enter COMMAND=sysbench and press enter
  • only sysbench processes shown
  • press l to hide the load average line
  • press m to hide the memory and swap line.
sysbench --threads=2 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 203 total,   1 running, 202 sleeping,   0 stopped,   0 zombie
%Cpu0 : 51.4 us, 0.0 sy, 0.0 ni, 48.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 50.0 us, 0.0 sy, 0.0 ni, 50.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 1.4 us, 0.0 sy, 0.0 ni, 98.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni, 98.6 id, 0.0 wa, 0.0 hi, 1.4 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
32589 root 20 0 87.9m 5.1m 102.7 0.3 0:03.61 S sysbench --threads=2 --cpu-max-prime=8005005 --verbosity+
sysbench --threads=3 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 203 total,   1 running, 202 sleeping,   0 stopped,   0 zombie
%Cpu0 : 32.5 us, 0.9 sy, 0.0 ni, 66.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 32.2 us, 0.0 sy, 0.0 ni, 67.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 32.2 us, 0.9 sy, 0.0 ni, 67.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.9 us, 0.9 sy, 0.0 ni, 98.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
335 root 20 0 88.0m 5.0m 97.4 0.3 0:08.41 S sysbench --threads=3 --cpu-max-prime=8005005 --verbosity+
sysbench --threads=4 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 203 total,   1 running, 202 sleeping,   0 stopped,   0 zombie
%Cpu0 : 25.5 us, 0.9 sy, 0.0 ni, 73.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 26.1 us, 0.9 sy, 0.0 ni, 72.1 id, 0.0 wa, 0.0 hi, 0.9 si, 0.0 st
%Cpu2 : 26.6 us, 0.9 sy, 0.0 ni, 72.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 26.4 us, 0.9 sy, 0.0 ni, 71.8 id, 0.0 wa, 0.0 hi, 0.9 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
678 root 20 0 88.0m 5.1m 100.9 0.3 0:05.22 S sysbench --threads=4 --cpu-max-prime=8005005 --verbosity+
kubectl delete -f myBench-Pod.yaml --force --grace-period=0pod "mybench-pod" force deleted

2) Pod Defines Using Half a CPU — Millicores Syntax

nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: "500m"
requests:
cpu: 500m

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl exec mybench-pod -i -t -- /bin/bash
sysbench --threads=1 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 208 total,   1 running, 207 sleeping,   0 stopped,   0 zombie
%Cpu0 : 1.1 us, 1.1 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 38.6 us, 1.1 sy, 0.0 ni, 60.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 11.5 us, 0.0 sy, 0.0 ni, 88.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 1.1 us, 0.0 sy, 0.0 ni, 98.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
4210 root 20 0 87.8m 5.1m 50.0 0.3 0:02.55 S sysbench --threads=1 --cpu-max-prime=8005005 --verbosity+
kubectl delete -f myBench-Pod.yaml --force --grace-period=0pod "mybench-pod" force deleted

3) Pod Defines Using Half a CPU — Fractional Syntax

nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: .5
requests:
cpu: .5

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl exec mybench-pod -i -t -- /bin/bash
sysbench --threads=1 --cpu-max-prime=8005005 --verbosity=0 cpu runTasks: 208 total,   1 running, 207 sleeping,   0 stopped,   0 zombie
%Cpu0 : 1.5 us, 1.5 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 1.6 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.0 us, 1.6 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 52.3 us, 0.0 sy, 0.0 ni, 47.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
5639 root 20 0 87.8m 5.1m 50.8 0.3 0:01.49 S sysbench --threads=1 --cpu-max-prime=8005005 --verbosity+
kubectl describe pod/mybench-podName:               mybench-pod
Containers:
mybench-container:
State: Running
Started: Fri, 11 Jan 2019 09:17:09 +0200
Ready: True
Limits:
cpu: 500m
Requests:
cpu: 500m
kubectl delete -f myBench-Pod.yaml --force --grace-period=0pod "mybench-pod" force deleted

4) Pod Declares No CPU Request and Limits

nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl exec mybench-pod -i -t -- /bin/bashsysbench --threads=4 --cpu-max-prime=8005005 --verbosity=0 cpu run%Cpu0  : 95.3 us,  4.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.4 si,  0.0 st
%Cpu1 : 97.8 us, 1.8 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
%Cpu2 : 96.8 us, 2.9 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
%Cpu3 : 97.8 us, 1.4 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.7 si, 0.0 st
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
9142 root 20 0 88.0m 5.1m 392.4 0.3 0:33.63 S sysbench --threads=4 --cpu-max-prime=8005005 --verbosity+
kubectl delete -f myBench-Pod.yaml --force --grace-period=0pod "mybench-pod" force deleted

5) CPU LimitRanges

nano mycpu-limit-range.yamlapiVersion: v1
kind: LimitRange
metadata:
name: mycpu-limit-range
spec:
limits:
- default:
cpu: 0.75
defaultRequest:
cpu: 0.25
type: Container
kubectl create -f mycpu-limit-range.yamllimitrange/mycpu-limit-range created
nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl describe pod/mybench-podName:               mybench-podAnnotations:        kubernetes.io/limit-ranger: LimitRanger plugin set: cpu request for container mybench-container; cpu limit for container mybench-containerContainers:
mybench-container:
State: Running
Started: Fri, 11 Jan 2019 09:48:24 +0200
Ready: True
Limits:
cpu: 750m
Requests:
cpu: 250m
kubectl delete limits/mycpu-limit-rangelimitrange "mycpu-limit-range" deleted

6) LimitRange with Defaults and Min, Max Values

nano mycpu-limit-range.yamlapiVersion: v1
kind: LimitRange
metadata:
name: mycpu-limit-range
spec:
limits:
- default:
cpu: 0.75
defaultRequest:
cpu: 0.25
max:
cpu: "2000m"
min:
cpu: "200m"
type: Container
kubectl create -f mycpu-limit-range.yaml
nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: "1000m"
requests:
cpu: 300m

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl describe pod/mybench-podName:               mybench-podContainers:
mybench-container:
State: Running
Ready: True
Limits:
cpu: 1
Requests:
cpu: 300m

7) Pod That Does Not Adhere to CPU Limits

nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: "3000m"
requests:
cpu: 100m

restartPolicy: Never
kubectl create -f myBench-Pod.yamlError from server (Forbidden): error when creating "myBench-Pod.yaml": pods "mybench-pod" is forbidden: [minimum cpu usage per Container is 200m, but request is 100m., maximum cpu usage per Container is 2, but limit is 3.]
nano myBench-Pod.yamlapiVersion: v1
kind: Pod
metadata:
name: mybench-pod
spec:
containers:
- name: mybench-container
image: mytutorials/centos:bench
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The CPU Bench Pod is Running ; sleep 3600']

resources:
limits:
cpu: "1500m"
requests:
cpu: 250m

restartPolicy: Never
kubectl create -f myBench-Pod.yaml 

pod/mybench-pod created
kubectl delete -f myBench-Pod.yaml --force --grace-period=0pod "mybench-pod" force deleted
kubectl delete limits/mycpu-limit-rangelimitrange "mycpu-limit-range" deleted

8) LimitRanges and Namespaces

Conclusion

  • Limit and request
  • Limit and without request
  • No limit and with request
  • Using just one Pod, request much more CPU cores than those available on your node.
  • Create 3 Pods that in total request CPU resources that exceed the number of CPU cores on the node
  • Create 3 Pods that in total limit CPU resources that exceed the number of CPU cores on the node
  • Since Pod may have several containers, create a Pod with 3 containers (each with different limits) and investigate how those limits are enforced. For example, if 3 containers in a Pod request 1 CPU each, the total Pod demand is 3 CPUs. You must attempt to run 3 Pods simultaneously if you want to test Pods that collectively exceed node cpu capability.

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Performance Testing with Postman

Real-Time Marketing Data Analysis Based on Hologres + Flink

Crafting some of the most sophisticated reading experiences on the web

Parallel Programming

Team Lovbot Wins!

Cloud Computing Made Really Easy, with SAS from Alibaba Cloud

🔥 DeSpace Protocol Announces NFT Collaborations with Major Celebrities

Fox and Snakes Coding Question

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Kubernetes Architecture from 10,000 feet

Produce and consume messages from a Kafka topic using docker

ELK Architecture Part-1

Kubernetes Liveness Probes — Examples & Common Pitfalls