In this lesson, you will learn how to define CPU and memory requirements to provide the necessary data for the autoscaling engine to monitor pod performance and manage cluster capacity.
Setting the Foundation for Scaling
When you deploy applications to Kubernetes, the cluster scheduler needs to know exactly how much "fuel" your containers require to function. If you don't specify these, the scheduler might place too many pods on a single node, leading to resource contention.
We manage this via requests and limits:
- Requests: This is the minimum amount of CPU and memory the container is guaranteed. The scheduler uses this value to decide which node has enough "room" to host your pod.
- Limits: This acts as a hard ceiling. It prevents a single container from consuming all the resources on a node, which could starve other services.
Defining Resources in Your Manifest
To provide the telemetry necessary for the Horizontal Pod Autoscaler (HPA) to function, you must define these values within your Pod or Deployment specification:
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
Why This Matters for Autoscaling
The Kubernetes autoscaling engine—such as the Horizontal Pod Autoscaler—operates on the ratio of actual usage versus the requested resources. If your pod is consuming 400m CPU, but your request is set to 250m, the HPA sees that your service is operating above its requested threshold.
If you omit these definitions, the metrics server cannot accurately calculate utilization percentages. Without these markers, the autoscaling engine has no reference point to trigger a "scale-out" event when your application experiences a spike in traffic. By setting precise requests, you ensure that the metrics collected represent the true health and demand of your workloads, allowing the cluster to proactively scale replicas up or down as traffic fluctuates.
Key Takeaway: Resource requests and limits are the primary data points for Kubernetes autoscalers; without them, the engine cannot accurately measure performance or trigger automated scaling decisions.