Kubernetes CPU Management Deep Dive: Requests, Limits, and Performance Optimization

On this page
- The Foundations: Linux CFS Scheduler
- Kubernetes CPU Requests Explained
- Kubernetes CPU Limits Explained
- Quality of Service (QoS) Classes
- CPU Manager Policies
- Advanced CPU Management Techniques
- Common CPU Management Antipatterns
- Best Practices for CPU Management
- Practical Implementation Examples
- CPU Metrics to Monitor
- Conclusion: A Balanced Approach to CPU Management
- Additional Resources
Understanding how Kubernetes manages CPU resources is essential for achieving optimal performance, cost efficiency, and stability in production environments. This deep dive explores the intricacies of Kubernetes CPU management, the underlying Linux scheduling mechanisms, and best practices for configuring workloads.
The Foundations: Linux CFS Scheduler
Kubernetes CPU management is built on the Linux Completely Fair Scheduler (CFS), which is responsible for distributing CPU time among competing processes. To understand Kubernetes CPU behavior, we must first understand how CFS works.
CFS Fundamentals
The Completely Fair Scheduler uses a time-sharing approach:
- CPU Time Slices: The CPU’s time is divided into small slices (typically 100ms by default).
- Proportional Allocation: During contention, processes receive CPU time proportional to their assigned “shares.”
- Virtual Runtime: CFS tracks the CPU time each process has received and prioritizes processes with the least runtime.
- Throttling: CFS can enforce usage limits through CPU quotas.
In Kubernetes, container CPU requests and limits are translated into CFS shares and quotas:
- CPU Request → CFS Shares
- CPU Limit → CFS Quota
Kubernetes CPU Requests Explained
A CPU request in Kubernetes defines the minimum guaranteed CPU resources a container should receive during contention. It’s expressed in cores or millicores (m), where 1000m equals 1 core.
resources:
requests:
cpu: "500m" # 500 millicores = 0.5 coresHow CPU Requests Work
- Scheduling Impact: The Kubernetes scheduler uses CPU requests to find nodes with sufficient available CPU.
- Resource Reservation: The requested CPU is reserved for the container, even if not fully utilized.
- Proportional Allocation: During contention, containers receive CPU time proportional to their requests.
Example: CPU Request Distribution
Consider a node with 1 CPU core (1000m) running three pods:
# Pod A
resources:
requests:
cpu: "200m"
# Pod B
resources:
requests:
cpu: "400m"
# Pod C
resources:
requests:
cpu: "200m"When all pods actively compete for CPU:
- Pod A receives: 200m / 800m total = 25% of CPU time
- Pod B receives: 400m / 800m total = 50% of CPU time
- Pod C receives: 200m / 800m total = 25% of CPU time
No Contention Scenario
When pods don’t fully use their requested CPU:
Available: 1000m (1 core)
Pod A uses: 100m
Pod B uses: 300m
Pod C uses: 150mTotal usage is 550m, leaving 450m available. Any pod can use this excess capacity beyond its request.
For example, if Pod A suddenly needs 300m, it can use its 200m request plus 100m from the available excess, assuming the other pods don’t increase their usage simultaneously.
Kubernetes CPU Limits Explained
While CPU requests guarantee minimum resources, CPU limits set a maximum ceiling on CPU usage:
resources:
requests:
cpu: "500m"
limits:
cpu: "1000m"How CPU Limits Work
- CFS Quota Translation: Kubernetes converts CPU limits to CFS quota periods.
- Throttling Mechanism: When a container attempts to use more CPU than its limit, the kernel throttles it.
- Enforcement Regardless of Capacity: Limits are enforced even if the node has spare CPU capacity.
Example: CPU Limit Enforcement
Consider a pod with the following configuration:
resources:
requests:
cpu: "500m"
limits:
cpu: "800m"This pod:
- Is guaranteed 500m CPU during contention
- Cannot use more than 800m CPU, even if the node has idle capacity
- Will be throttled if it attempts to exceed 800m
Throttling: The Hidden Performance Killer
CPU throttling occurs when a container tries to use more CPU than its limit. The symptoms include:
- Increased Latency: Requests take longer to process due to CPU starvation
- Reduced Throughput: Fewer operations per second
- Jitter: Inconsistent performance with periodic slowdowns
To detect throttling, monitor these metrics:
container_cpu_cfs_throttled_periods_total
container_cpu_cfs_throttled_seconds_totalA significant number of throttled periods indicates that your CPU limits may be too restrictive.
Quality of Service (QoS) Classes
Kubernetes assigns QoS classes to pods based on their resource specifications:
1. Guaranteed
Pods with equal requests and limits for all resources:
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "500m"
memory: "512Mi"Benefits:
- Lowest probability of eviction during node pressure
- Eligible for exclusive CPU assignment with the static CPU Manager policy
2. Burstable
Pods with requests less than limits:
resources:
requests:
cpu: "200m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"Characteristics:
- Medium priority for eviction
- Can burst beyond requests up to limits
3. BestEffort
Pods with no resource requests or limits:
# No resource specificationsCharacteristics:
- Highest probability of eviction
- No resource guarantees
- Can consume any available resources
CPU Manager Policies
Kubernetes provides two CPU Manager policies that determine how CPU resources are allocated to containers:
1. Default Policy (none)
The default policy relies entirely on the CFS scheduler:
- Dynamic CPU Assignment: Containers can be scheduled on any available CPU
- No CPU Pinning: Containers may be moved between CPUs
- Proportional Allocation: CPU time is distributed according to requests
This works well for most general-purpose workloads but can lead to performance variability due to:
- Context switching overhead
- CPU cache misses
- Non-uniform memory access (NUMA) effects
2. Static Policy
The static policy provides exclusive CPU allocation for pods in the Guaranteed QoS class:
# Pod eligible for exclusive CPUs
resources:
requests:
cpu: "2" # Integer value required
memory: "2Gi"
limits:
cpu: "2" # Equal to request
memory: "2Gi" # Equal to requestBenefits:
- CPU Pinning: Containers run on dedicated cores
- Reduced Context Switching: No other containers run on these CPUs
- Cache Efficiency: Better CPU cache utilization
- NUMA Locality: Improved memory access performance
To enable the static policy, configure the kubelet:
--cpu-manager-policy=static
--cpu-manager-reconcile-period=5s
--reserved-cpus=0,1 # Reserve CPUs for system processesExample: Static Policy Implementation
When the static policy is enabled:
- CPUs 0 and 1 are reserved for system processes
- A pod with a request for 2 CPUs is assigned exclusive use of CPUs 2 and 3
- Shared pods (non-Guaranteed or with non-integer CPU requests) run on the remaining CPUs
This approach is ideal for performance-sensitive workloads like:
- Data processing engines
- Real-time applications
- Latency-sensitive services
- CPU-intensive machine learning workloads
Advanced CPU Management Techniques
Beyond basic requests and limits, consider these advanced techniques:
1. CPU Affinity with Topology Manager
The Topology Manager coordinates CPU, memory, and device allocation to optimize for NUMA locality:
--topology-manager-policy=single-numa-nodeThis ensures containers are scheduled on CPUs and memory from the same NUMA node, reducing latency for memory-intensive workloads.
2. CPU Sets
For more granular control, you can use the Downward API to expose CPU allocation information to containers:
env:
- name: CPU_LIMITS
valueFrom:
resourceFieldRef:
containerName: app
resource: limits.cpuYour application can then use this information to set thread affinity using sched_setaffinity().
3. Custom CPU Profiles
For workloads with specific CPU governor requirements, consider using a DaemonSet to configure CPU frequency scaling:
echo performance > /sys/devices/system/cpu/cpu*/cpufreq/scaling_governorCommon CPU Management Antipatterns
Avoid these common mistakes in CPU resource configuration:
1. Setting CPU Limits Too Low
Setting CPU limits significantly lower than actual peak requirements leads to throttling and unpredictable performance.
# Antipattern
resources:
requests:
cpu: "200m"
limits:
cpu: "300m" # Too restrictive if actual peak usage is 500m2. Using Round Number Requests Without Profiling
Arbitrarily setting CPU requests without measuring actual usage wastes resources or causes contention.
# Antipattern
resources:
requests:
cpu: "1" # Arbitrary value without profiling3. Identical Configurations for Different Workloads
Using the same resource configuration for all services ignores their unique characteristics.
# Antipattern
# Same config for database, API server, and batch job
resources:
requests:
cpu: "500m"
limits:
cpu: "1000m"Best Practices for CPU Management
Follow these guidelines to optimize your Kubernetes CPU management:
1. Profile Before Configuring
Measure actual CPU usage patterns in various scenarios:
# Using kubectl top
kubectl top pod <pod-name> --containers
# Using metrics-server API
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/<pod-name>"Set requests based on P90 (90th percentile) of normal usage and limits based on peak usage patterns.
2. Implement Proper Monitoring
Deploy comprehensive monitoring for CPU-related metrics:
# Prometheus query examples
# CPU usage rate
rate(container_cpu_usage_seconds_total{container_name!="POD",pod_name=~"app-.*"}[5m])
# CPU throttling
sum(rate(container_cpu_cfs_throttled_periods_total{container_name!="POD",pod_name=~"app-.*"}[5m])) /
sum(rate(container_cpu_cfs_periods_total{container_name!="POD",pod_name=~"app-.*"}[5m]))Set alerts for sustained high throttling rates, which indicate insufficient CPU limits.
3. Right-Size Based on Workload Type
Different workload types require different CPU configurations:
Batch Processing Jobs
resources:
requests:
cpu: "500m" # Lower request to improve scheduling
limits:
cpu: "2" # Higher limit for burst capacityWeb Servers / API Services
resources:
requests:
cpu: "200m" # Based on average load
limits:
cpu: "1" # Allows for traffic spikesDatabases / Stateful Services
resources:
requests:
cpu: "1" # Higher request for stability
limits:
cpu: "1" # Equal to request for Guaranteed QoS4. Consider Omitting CPU Limits
For non-critical workloads in trusted environments, consider omitting CPU limits to avoid throttling:
resources:
requests:
cpu: "200m" # Still set a reasonable request
# No CPU limit specifiedThis approach:
- Prevents throttling during usage spikes
- Allows for better utilization of idle cluster resources
- Still maintains fairness during contention through requests
Note: This approach requires careful monitoring and should not be used for untrusted workloads or in multi-tenant environments.
5. Use Horizontal Pod Autoscaling (HPA)
Implement HPA to automatically adjust pod count based on CPU utilization:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70This approach is more efficient than setting high CPU limits to accommodate occasional spikes.
Practical Implementation Examples
Let’s examine real-world scenarios with optimized CPU configurations:
High-Performance Web API
apiVersion: apps/v1
kind: Deployment
metadata:
name: high-perf-api
spec:
replicas: 5
template:
spec:
containers:
- name: api
image: api-server:v1
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
# No CPU limit to avoid throttling during traffic spikes
memory: "1Gi"
readinessProbe:
httpGet:
path: /health
port: 8080
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: high-perf-api
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60CPU-Intensive Data Processing
apiVersion: apps/v1
kind: Deployment
metadata:
name: data-processor
spec:
replicas: 3
template:
spec:
containers:
- name: processor
image: data-processor:v2
resources:
requests:
cpu: "2"
memory: "4Gi"
limits:
cpu: "2"
memory: "4Gi"
nodeSelector:
node-type: compute-optimized
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: data-processorBursty Batch Job
apiVersion: batch/v1
kind: Job
metadata:
name: data-analysis-job
spec:
template:
spec:
containers:
- name: analyzer
image: data-analyzer:v1
resources:
requests:
cpu: "500m"
memory: "2Gi"
limits:
cpu: "4"
memory: "8Gi"
restartPolicy: Never
backoffLimit: 2CPU Metrics to Monitor
To validate and refine your CPU management strategy, monitor these key metrics:
1. Usage vs. Requests Ratio
sum(rate(container_cpu_usage_seconds_total{namespace="production"}[5m])) by (pod) /
sum(kube_pod_container_resource_requests{resource="cpu",namespace="production"}) by (pod)Target: Between 0.7 and 0.9 for efficient resource utilization
2. Throttling Percentage
sum(rate(container_cpu_cfs_throttled_periods_total{namespace="production"}[5m])) by (pod) /
sum(rate(container_cpu_cfs_periods_total{namespace="production"}[5m])) by (pod)Target: Less than 1% for optimal performance
3. Node CPU Utilization
sum(rate(node_cpu_seconds_total{mode!="idle"}[5m])) by (instance) /
count(node_cpu_seconds_total{mode="idle"}) by (instance)Target: 60-80% for cost-efficient cluster utilization
Conclusion: A Balanced Approach to CPU Management
Effective CPU management in Kubernetes requires balancing several competing factors:
- Resource Efficiency: Maximizing cluster utilization
- Performance Predictability: Ensuring consistent application behavior
- Operational Simplicity: Creating maintainable configurations
- Cost Optimization: Minimizing infrastructure expenses
By understanding how the Linux CFS scheduler interacts with Kubernetes CPU requests and limits, you can create optimal configurations for your specific workloads. Remember these key principles:
- Set CPU requests based on actual usage patterns
- Use CPU limits carefully, considering the trade-offs
- Leverage QoS classes intentionally
- Consider the static CPU Manager policy for latency-sensitive workloads
- Monitor throttling metrics to detect and resolve performance issues
With these practices in place, you can achieve both efficient resource utilization and reliable application performance in your Kubernetes clusters.