Bird
Raised Fist0
Kubernetesdevops~5 mins

CPU requests and limits in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
When running applications in Kubernetes, you need to tell the system how much CPU your app needs and the maximum it can use. This helps the system share CPU fairly and avoid one app slowing down others.
When you want to make sure your app always gets enough CPU to run smoothly.
When you want to prevent your app from using too much CPU and affecting other apps.
When you run many apps on the same cluster and want to share CPU fairly.
When you want to avoid your app being killed because it uses too much CPU.
When you want to optimize resource costs by not over-allocating CPU.
Config File - pod-cpu.yaml
pod-cpu.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cpu-demo-pod
spec:
  containers:
  - name: cpu-demo-container
    image: busybox
    command: ["sh", "-c", "while true; do echo hello; sleep 10; done"]
    resources:
      requests:
        cpu: "250m"
      limits:
        cpu: "500m"

This file creates a pod named cpu-demo-pod with one container.

The resources section sets CPU requests to 250 milliCPU (0.25 CPU) and CPU limits to 500 milliCPU (0.5 CPU).

Requests tell Kubernetes the minimum CPU guaranteed for the container.

Limits tell Kubernetes the maximum CPU the container can use.

Commands
This command creates the pod with CPU requests and limits defined in the YAML file.
Terminal
kubectl apply -f pod-cpu.yaml
Expected OutputExpected
pod/cpu-demo-pod created
This command checks that the pod is running after creation.
Terminal
kubectl get pods cpu-demo-pod
Expected OutputExpected
NAME READY STATUS RESTARTS AGE cpu-demo-pod 1/1 Running 0 10s
This command shows detailed information about the pod, including CPU requests and limits.
Terminal
kubectl describe pod cpu-demo-pod
Expected OutputExpected
Name: cpu-demo-pod Namespace: default Node: minikube/192.168.49.2 Start Time: Thu, 01 Jun 2023 12:00:00 +0000 Labels: <none> Annotations: <none> Status: Running Containers: cpu-demo-container: Container ID: docker://abcdef123456 Image: busybox Image ID: docker-pullable://busybox@sha256:abcdef123456 Port: <none> Host Port: <none> Command: sh -c while true; do echo hello; sleep 10; done State: Running Started: Thu, 01 Jun 2023 12:00:05 +0000 Ready: True Restart Count: 0 Limits: cpu: 500m Requests: cpu: 250m Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: <none> QoS Class: Burstable Node-Selectors: <none> Tolerations: <none> Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: CPU requests guarantee minimum CPU for your app, and CPU limits cap the maximum CPU it can use.

Common Mistakes
Setting CPU limits lower than CPU requests
Kubernetes will reject the pod because limits must be equal or higher than requests.
Always set CPU limits equal to or higher than CPU requests.
Not setting CPU requests at all
Without requests, Kubernetes may not reserve CPU, causing your app to get less CPU than needed.
Always set CPU requests to ensure your app gets enough CPU.
Setting very high CPU limits without reason
This can cause your app to use too much CPU and affect other apps.
Set realistic CPU limits based on your app's needs.
Summary
Create a pod YAML file with CPU requests and limits under the resources section.
Apply the YAML file with kubectl apply to create the pod.
Use kubectl get pods to check the pod status.
Use kubectl describe pod to see detailed CPU requests and limits.
CPU requests guarantee minimum CPU; CPU limits cap maximum CPU usage.

Practice

(1/5)
1. What does setting a cpu request in a Kubernetes pod specification do?
easy
A. It guarantees a minimum amount of CPU for the container.
B. It limits the maximum CPU the container can use.
C. It sets the CPU priority for the container.
D. It reserves memory for the container.

Solution

  1. Step 1: Understand CPU request meaning

    CPU request is the minimum CPU resource Kubernetes guarantees to a container.
  2. Step 2: Differentiate from CPU limit

    CPU limit sets the maximum CPU usage, not the minimum guaranteed.
  3. Final Answer:

    It guarantees a minimum amount of CPU for the container. -> Option A
  4. Quick Check:

    CPU request = minimum CPU guaranteed [OK]
Hint: Requests guarantee minimum CPU, limits cap maximum CPU [OK]
Common Mistakes:
  • Confusing CPU request with CPU limit
  • Thinking requests set maximum CPU
  • Mixing CPU with memory resource
2. Which of the following is the correct way to specify CPU requests and limits in a pod YAML?
easy
A. resources: cpu: request: 500m limit: 1
B. resources: requests: cpu: 500 limits: cpu: 1000
C. resources: requests: cpu: "500m" limits: cpu: "1"
D. resources: requests: cpu: "1.0" limits: cpu: "0.5"

Solution

  1. Step 1: Check correct YAML structure for resources

    The correct keys are requests and limits under resources, with CPU values as strings with units like "500m" or "1".
  2. Step 2: Validate resources: requests: cpu: "500m" limits: cpu: "1" format

    resources: requests: cpu: "500m" limits: cpu: "1" uses correct keys and units: "500m" means 0.5 CPU, "1" means 1 CPU core.
  3. Final Answer:

    resources: requests: cpu: "500m" limits: cpu: "1" -> Option C
  4. Quick Check:

    Correct YAML keys and CPU units = resources: requests: cpu: "500m" limits: cpu: "1" [OK]
Hint: Use 'requests' and 'limits' keys with CPU as string with units [OK]
Common Mistakes:
  • Using 'request' instead of 'requests'
  • Specifying CPU as numbers without quotes or units
  • Mixing up 'cpu' and 'memory' keys
3. Given this pod spec snippet:
resources:
  requests:
    cpu: "200m"
  limits:
    cpu: "500m"
What happens if the container tries to use 600m CPU?
medium
A. The container will be killed immediately.
B. The container will be throttled to 500m CPU.
C. The container will be allowed to use 600m CPU.
D. The container will be throttled to 200m CPU.

Solution

  1. Step 1: Understand CPU limit effect

    The CPU limit is the maximum CPU the container can use. Here it is 500m (0.5 CPU).
  2. Step 2: Predict container behavior when exceeding limit

    If the container tries to use 600m CPU, Kubernetes throttles it to the limit, 500m CPU.
  3. Final Answer:

    The container will be throttled to 500m CPU. -> Option B
  4. Quick Check:

    CPU limit caps usage = throttled to 500m [OK]
Hint: CPU usage above limit gets throttled to limit [OK]
Common Mistakes:
  • Thinking container can exceed CPU limit
  • Confusing throttling with killing container
  • Assuming CPU request is the max limit
4. You wrote this pod spec:
resources:
  requests:
    cpu: "100m"
  limits:
    cpu: "50m"
What is wrong with this configuration?
medium
A. CPU requests and limits cannot be set together.
B. CPU request is less than CPU limit, which is invalid.
C. CPU values must be integers, not strings.
D. CPU limit is less than CPU request, which is invalid.

Solution

  1. Step 1: Compare CPU request and limit values

    CPU request is 100m (0.1 CPU), CPU limit is 50m (0.05 CPU).
  2. Step 2: Validate Kubernetes rules for requests and limits

    CPU limit must be equal or greater than CPU request. Here limit < request, which is invalid.
  3. Final Answer:

    CPU limit is less than CPU request, which is invalid. -> Option D
  4. Quick Check:

    Limit >= Request rule violated = CPU limit is less than CPU request, which is invalid. [OK]
Hint: Limit must be >= request, else config is invalid [OK]
Common Mistakes:
  • Setting limit less than request
  • Thinking CPU values must be integers
  • Believing requests and limits can't coexist
5. You want to ensure a container always gets at least 250m CPU but never uses more than 750m CPU. Which YAML snippet correctly sets this with CPU requests and limits?
hard
A. resources: requests: cpu: "250m" limits: cpu: "750m"
B. resources: requests: cpu: "750m" limits: cpu: "250m"
C. resources: requests: cpu: 0.25 limits: cpu: 0.75
D. resources: requests: cpu: "0.75" limits: cpu: "0.25"

Solution

  1. Step 1: Identify correct CPU units and values

    CPU values can be specified as millicores with 'm' suffix, e.g., "250m" for 0.25 CPU.
  2. Step 2: Check request and limit order

    Request must be less than or equal to limit. resources: requests: cpu: "250m" limits: cpu: "750m" sets request to 250m and limit to 750m correctly.
  3. Final Answer:

    resources: requests: cpu: "250m" limits: cpu: "750m" -> Option A
  4. Quick Check:

    Request 250m ≤ Limit 750m with correct units = resources: requests: cpu: "250m" limits: cpu: "750m" [OK]
Hint: Use millicores with 'm' and keep request ≤ limit [OK]
Common Mistakes:
  • Swapping request and limit values
  • Using decimal numbers without quotes
  • Not using millicores for fractional CPU
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard