Lesson 2 of 3 Kubernetes Cost Optimization

Allocating Kubernetes Costs

Shared clusters hide cost from the provider bill, so allocation must happen inside the cluster. This lesson explains how node cost is distributed to pods, how pods are grouped to teams, and how idle and shared cluster costs are handled.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how node cost is distributed to pods.
  • Compare allocation by requests, usage and the larger of the two.
  • Describe how pod costs are aggregated by namespace and labels.
  • Explain how idle and shared cluster costs are handled.
  • Calculate pod and idle costs for a node.

From nodes to pods

Kubernetes cost allocation distributes each node's cost to the pods that ran on it during each time interval. A node's hourly price is first split between CPU and memory, for example using the provider's relative prices for the two resources, giving a price per CPU-hour and per GiB-hour for that node. Each pod is then charged for the CPU and memory attributed to it, multiplied by those prices.

Tools such as OpenCost, a Cloud Native Computing Foundation (CNCF) project, and commercial Kubernetes cost tools perform this calculation continuously using cluster metrics and provider prices, and can use the organization's actual discounted rates from billing data.

Requests, usage or the larger of the two

The key design choice is which quantity to charge each pod for. The following comparison sets out the three options.

Requests only
What the pod pays forWhat it reserved
Over-requestingCharged to the pod
Bursting above requestNot charged
IncentiveSet requests accurately
Choose when Simplicity and predictable charges
Usage only
What the pod pays forWhat it actually used
Over-requestingHidden; becomes idle cost
Bursting above requestCharged
IncentiveWeak incentive to fix requests
Choose when Rarely recommended on its own
Max(request, usage)
What the pod pays forWhichever is larger
Over-requestingCharged to the pod
Bursting above requestCharged
IncentiveSet requests accurately and avoid excess bursting
Choose when Most allocation tools' default; fairest incentive

Charging the larger of request and usage is common because a pod is responsible for the capacity it blocks (its request) and for any extra capacity it consumes when it bursts above its request.

The following diagram divides a node's hourly cost between two pods and idle capacity.

Allocating an example USD 0.40-an-hour node by the larger of request and usage. Pod A is charged for its 4 vCPU and 8 GiB requests, USD 0.175; Pod B for its 2 vCPU of usage, above its request, and 4 GiB, USD 0.0875. The remaining USD 0.1375, 2 vCPU and 20 GiB, is idle cost. A large unused request, like Pod A's, marks a rightsizing candidate.
The node price and rates are examples.
Fill in the value

Allocating a node

Exam skills 7.3.1

Allocate node cost to pods by the larger of request and usage.

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

From pods to teams

Pod costs are aggregated into the dimensions the organization uses. Namespaces are the most common: if each team or application has its own namespaces, namespace cost is team cost. Labels, such as team, app and env, give finer or cross-cutting views, and a labeling standard enforced through admission policies keeps them consistent. Controllers such as Deployments and StatefulSets group pods by workload.

Idle and shared costs

Some cluster cost belongs to no application. The following terms describe the two main categories and how they are usually handled.

Idle cost
Node capacity not requested or used by any pod. It can be shown as a separate line owned by the platform team, or distributed to tenants in proportion to their allocated cost.
Shared cost
Control plane fees, system and monitoring pods, ingress, and shared tooling. Usually distributed to tenants proportionally or by an agreed driver.

Making idle cost visible is important whichever rule is chosen. If it is silently spread across tenants, nobody sees that the cluster is oversized; if it is shown as the platform team's cost, that team has a clear incentive to tune node sizes and autoscaling.

Scenario questions

Handling idle cluster cost

Exam skills 7.3.1

Decide how idle and shared cluster costs should be handled.

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

Reconciling with the bill

Allocated pod costs, plus idle and shared costs, should add up to the cluster's total cost in the billing data. Checking this reconciliation catches errors such as missing node prices or unaccounted storage. Using discounted rates, so that commitments and spot pricing benefit the workloads that use them, keeps Kubernetes allocation consistent with the rest of the organization's showback.

Worked example

Allocating a node

A node costs USD 0.40 per hour, split as USD 0.30 for its 8 vCPUs and USD 0.10 for its 32 GiB (USD 0.0375 per vCPU-hour and USD 0.003125 per GiB-hour). Pod A requests 4 vCPU and 8 GiB and uses 1 vCPU and 6 GiB. Pod B requests 1 vCPU and 4 GiB and uses 2 vCPU and 3 GiB. Using max(request, usage), what does each pod cost per hour, and what is idle?

Pod A is charged for 4 vCPU and 8 GiB: 4 x 0.0375 + 8 x 0.003125 = USD 0.15 + USD 0.025 = USD 0.175. Pod B is charged for 2 vCPU (its usage exceeds its request) and 4 GiB: 0.075 + 0.0125 = USD 0.0875. Together they account for USD 0.2625; the remaining USD 0.1375 is idle cost (2 vCPU and 20 GiB not attributed to any pod). Pod A's large unused request makes it the obvious rightsizing candidate.

Worked example

The shared cluster dispute

Three teams share a cluster. The platform team divides the whole cluster bill equally, so each pays a third. One team runs half of all pods. The other two complain. What allocation would be fairer?

Allocate each team's pod costs by max(request, usage), aggregated by namespace; show idle cost as the platform team's line or distribute it in proportion to allocated cost; and distribute shared costs such as the control plane and monitoring proportionally. The team running half the pods would then carry most of the cost, and each team would have an incentive to rightsize its own requests.

Check yourself

Why does allocating Kubernetes cost by actual usage alone weaken the incentive to fix requests?

Show answer

Over-requested capacity is not charged to the pod that blocks it; it becomes idle cost spread elsewhere, so the team that over-requests does not see the cost.

Exam Tips

Remember for the exam

  • Node cost is split into CPU and memory prices and distributed to pods.
  • Charging max(request, usage) makes teams responsible for capacity they block or burst into.
  • Aggregate pod costs by namespace and enforced labels.
  • Make idle and shared costs visible and allocate them by an agreed rule.