0
0
Kubernetesdevops~5 mins

CPU requests and limits in Kubernetes - Commands & Configuration

Choose your learning style9 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.