Kubernetes : Assign CPU Resource Defaults and Limits to Containers

1) Pod Declares 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']

resources:
limits:
cpu: "1"
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: 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+
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

--

--

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