Lesson 3 of 3 Kubernetes Cost Optimization

Optimizing Kubernetes Workloads

Kubernetes offers several levers for reducing cost, and they work best in the right order. This lesson explains request rightsizing, bin packing, pod and node autoscaling, spot node pools and scaling non-production clusters down.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why request rightsizing comes first.
  • Describe bin packing and node shape selection.
  • Distinguish the Horizontal Pod Autoscaler, Vertical Pod Autoscaler, Cluster Autoscaler and Karpenter.
  • Describe how spot node pools are used safely.
  • Describe the recommended order of Kubernetes optimization.

Rightsizing requests first

Because requests drive node count, rightsizing requests is the first and usually the most valuable step. Observed usage over a representative period, including peaks, shows where requests are far above need. The Vertical Pod Autoscaler (VPA) can recommend requests from observed usage or apply them automatically, and cost tools show the cost of the gap between request and usage for each workload.

Memory requests deserve particular care, because a container that exceeds its memory limit is terminated. CPU is more forgiving: a container that needs more CPU than requested can burst if the node has spare capacity, and is throttled rather than killed at its limit.

Bin packing and node shapes

Bin packing means placing pods so that nodes are filled efficiently, leaving few partially used nodes. The scheduler's placement strategy affects this, as does the choice of node shape. If pods need about 4 GiB of memory per vCPU but nodes provide 2 GiB per vCPU, nodes run out of memory while CPU sits unused. Choosing node types whose CPU-to-memory ratio matches the workloads, or letting a node provisioner choose from many types, reduces this stranded capacity.

Autoscaling pods and nodes

Kubernetes autoscaling works at two levels, and both are needed for savings. The following terms describe the main components.

Horizontal Pod Autoscaler HPA
Adjusts the number of pod replicas based on metrics such as CPU utilization or requests per second.
Vertical Pod Autoscaler VPA
Recommends or adjusts the CPU and memory requests of pods based on observed usage.
Cluster Autoscaler
Adds nodes when pods cannot be scheduled and removes underused nodes when their pods can be moved elsewhere.
Karpenter
An open-source node provisioner that launches right-sized nodes from many instance types for pending pods and consolidates underused nodes.

The levers depend on each other. Rightsizing requests frees capacity on nodes, but the money is saved only when the cluster autoscaler or node provisioner removes the freed nodes. Similarly, the HPA reduces replicas at night, and node scaling turns that into fewer nodes.

The following diagram shows where a rightsizing saving stops and what blocks it.

Why a large cut in CPU requests can leave the cluster bill unchanged. Rightsizing frees reserved capacity on the nodes, but the bill falls only when nodes are removed. Node autoscaling may be off or too conservative, or pods with restrictive disruption budgets, local storage or affinity rules may block consolidation.
The bill falls only when nodes are removed.
Troubleshooting scenario

Savings that did not appear

Exam skills 7.3.2

Find why Kubernetes optimizations did not lower the bill.

Log in to try this activity Interactive activities are free with an account.

Spot node pools

Kubernetes is well suited to spot capacity, because the scheduler can restart interrupted pods on other nodes. A spot node pool runs fault-tolerant workloads, such as stateless services with several replicas, batch jobs and CI runners, at a deep discount. Good practice uses several instance types and zones, sets pod disruption budgets so that interruptions do not take down too many replicas at once, handles the termination notice gracefully, and keeps critical or stateful workloads on on-demand or committed nodes through node selectors or taints.

Non-production and idle workloads

Development and test namespaces, preview environments and whole non-production clusters can often be scaled to zero outside working hours, with the node autoscaler then removing the empty nodes. Event-driven autoscalers can scale some workloads to zero when there is no work, such as an empty queue. Cluster consolidation, running fewer, larger shared clusters instead of many small ones, reduces the fixed overhead of control planes and system pods.

Recommended order

The following steps summarize the usual order of Kubernetes optimization.

  1. Make cost visible

    Allocate costs by namespace and show idle cost.

  2. Rightsize requests

    Bring requests close to observed usage plus headroom.

  3. Enable node scaling

    Ensure the cluster autoscaler or node provisioner can remove freed nodes.

  4. Improve packing

    Match node shapes to workloads or allow flexible instance selection.

  5. Use spot

    Move fault-tolerant workloads to spot node pools.

  6. Commit

    Cover the remaining steady node baseline with commitment discounts.

Put in order

Optimizing a cluster

Exam skills 7.3.2

Put Kubernetes optimization steps in the recommended order.

Log in to try this activity Interactive activities are free with an account.
Worked example

Savings that did not appear

A team reduced its pods' CPU requests by 40% after a rightsizing review, but the cluster bill did not change. Why?

Rightsizing freed reserved capacity on the nodes, but the node count did not fall. Either node autoscaling is not enabled, its scale-down settings are too conservative, or pods with restrictive disruption budgets, local storage or affinity rules prevent consolidation. Once the autoscaler can move pods and remove empty nodes, the freed capacity turns into savings.

Worked example

A CI cluster

A CI/CD cluster runs build agents on 20 on-demand nodes all day, although builds happen mostly between 08:00 and 19:00 on weekdays. Builds can be retried if interrupted. What changes would reduce cost?

Run build agents on a spot node pool with several instance types, because builds are interruptible and retryable; scale build agents with the queue of pending jobs, ideally to zero when there are none; and let the node autoscaler remove idle nodes overnight and at weekends. Together, these changes typically reduce the cluster's cost by a large fraction while keeping build capacity available during working hours.

Check yourself

After requests are rightsized, what must happen for the bill to fall?

Show answer

The cluster autoscaler or node provisioner must consolidate pods and remove the freed nodes. Lower requests alone do not change the node count.

Exam Tips

Remember for the exam

  • Rightsize requests first, then make sure node scaling removes the freed nodes.
  • HPA scales replicas; VPA adjusts requests; the cluster autoscaler or Karpenter scales nodes.
  • Match node shapes to workload CPU-to-memory ratios to avoid stranded capacity.
  • Use spot node pools with disruption budgets for fault-tolerant workloads.