CPU threshold
- Docker & Kubernetes course
- Advanced
- Lesson 39/70
- 4 min
Learn to scale based on CPU percentage.
The CPU threshold
The CPU percentage is calculated relative to the pod's request (requests.cpu), not the node capacity. That's why CPU requests must be written in the target containers for the HPA to work; otherwise <unknown> is seen in the TARGETS column:
resources:
requests:
cpu: 200m
# if the pod uses 150m, utilization is 75%
$ kubectl get hpa
NAME TARGETS REPLICAS
api cpu: <unknown>/70% 2 # no requests!Choosing the threshold
If the target is too high (95%), under a sudden load the existing pods are overwhelmed until new pods are ready; if too low (30%), resources are wasted. The longer a new pod takes to start, the lower the target is chosen. 60–75% is a commonly used range.
$ kubectl describe hpa api | grep -A3 Conditions
AbleToScale True ReadyForNewScale
ScalingActive True ValidMetricFound
ScalingLimited False DesiredWithinRangePractice A container with requests
Write a Deployment's container section (starting with containers:): container named api, image api:1.0; resources.requests cpu: 200m, memory: 256Mi; resources.limits cpu: "1".
Starter code
# buraya yazSample solution
containers:
- name: api
image: api:1.0
resources:
requests:
cpu: 200m
memory: 256Mi
limits:
cpu: "1"Expected output
deployment.apps/api configured $ kubectl get hpa api NAME TARGETS REPLICAS api cpu: 42%/70% 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the CPU utilization of a pod with requests.cpu: 500m using 400m?
- 40%
- 125%
- 20%
- 80% Correct answer
Explanation: 400 / 500 = 0.8.
2 Write a CPU request so the HPA can calculate.
resources:
____:
cpu: 200m
resources:
requests:
cpu: 200m
Explanation: Utilization is calculated against requests.
3 Match.
| Term | Meaning |
|---|---|
requests.cpu |
Percentage base |
<unknown> |
No request or metric |
high target |
Overload on spikes |
low target |
Wasted resources |