Container Cost Optimization
A vendor-neutral methodology for measuring and allocating infrastructure and container costs in Kubernetes environments, based on the OpenCost Specification. Kubernetes runs transient, containerized workloads that consume variable amounts of shared cluster resources. This tab specifies how to accurately measure and allocate the cost of a cluster to the tenants that consume it.
a) Total Cluster Costs — Calculation Details
Total Cluster Costs represent all costs required to operate a Kubernetes cluster. They break down into Cluster Asset Costs (directly observable entities: nodes, persistent volumes, attached disks, load balancers, and network ingress/egress) and Cluster Overhead Costs (the overhead to operate all assets, e.g. cluster management fees). From a financial-accounting view, Asset Costs are equivalent to Cost of Goods Sold, while Overhead Costs are equivalent to SG&A (indirect costs).
Total Cluster Costs = Cluster Asset Costs + Cluster Overhead Costs
Total Cluster Costs = Resource Allocation Costs (for all assets)
+ Resource Usage Costs (for all assets)
+ Cluster Overhead Costs (for cluster)
Total Cluster Costs = Workload Costs + Cluster Idle Costs + Cluster Overhead Costs
Once calculated, Asset Costs are distributed to the tenants that consume them, where Workload Costs + Idle Costs = Asset Costs. Workload costs are directly attributable to a set of Kubernetes workloads (container, pod, deployment, etc.); Cluster Idle Costs are the portion of Resource Allocation Costs not allocated to any workload.
Total Cluster Cost Composition (Assets + Overhead)
Cost Distribution to Tenants (Workload + Idle + Overhead)
Cost Waterfall — From Total Cluster Cost to Allocated Workload Cost
b) Cluster Asset Costs — In Detail
Cluster Assets are observable entities within a cluster that directly incur costs. Asset Costs consist of Resource Allocation Costs (accumulate based on time provisioned, irrespective of usage — e.g. CPU hourly rate) and Resource Usage Costs (accumulate per unit consumed — e.g. cost per byte egressed). An individual asset's cost is the sum of its components, e.g. a Node's cost = CPU cost + GPU cost + RAM cost + Node Network cost. Every conforming asset MUST include at least one cost component with Amount, Unit, and Rate attributes and a TotalCost value.
Amount × Duration × HourlyRate
Attributes: Amount (e.g. 2 CPU cores), Duration (hrs), Unit (CPU cores), HourlyRate ($/unit-hr), TotalCost.
Amount × UnitRate
Attributes: Amount (e.g. 1GB egress), Unit (GB), UnitRate ($/unit), TotalCost.
Asset Cost Inputs by Type (example billing models over a 24-hour window)
| Asset | Cost Type | Formula / Inputs |
|---|---|---|
| Node — CPU | Allocation | total = cores × duration × price cores = avg_over_time(cpu) by (node); duration = end−start [hrs]; price [$/core-hr] |
| Node — RAM | Allocation | total = ram_bytes × duration × price ram = avg_over_time(GB) by (node); price [$/GB-hr] |
| Persistent Volumes | Allocation | DiskSize = avg_over_time(GB) by (pv); price [$/GB-hr] — function of disk class, IOPS, backup size |
| Attached Disks | Allocation | DiskSize = avg_over_time(GB) by (pv); price [$/GB-hr] — ephemeral storage used per pod on node |
| Load Balancer — Usage | Usage | amount = bytes ingressed; price = $ per byte ingressed |
| Load Balancer — Allocation | Allocation | rules = # of forwarding rules defined; price = average $ per forwarding rule |
Resource Measurement Reference
| Resource Type | Measurement |
|---|---|
| CPU | The greater of requested and used CPU resources measured in cores or millicores. |
| Memory | The greater of requested resources and memory in use measured in bytes or gigabytes. |
| GPU | The greater of requested resources and used resources measured in cores. |
| Storage Volume | The storage capacity of Persistent Volume Claim (PVC) requests measured in bytes or gigabytes. Attached at the Kubernetes pod level. |
| Network | Amount of ingress or egress across zones, regions, or the wide internet measured in bytes or gigabytes (can vary by provider). |
| Load Balancer | The number of load balancers used plus the volume of connections and bytes (can vary by provider). |
Overhead Costs
Provider fees typically charged on an hourly basis.
Potential DevOps team costs allocated to cluster operations.
c) Workload Costs — In Detail
Workloads are entities to which Asset Costs are committed. Some resources have only Usage Costs; others carry Allocation Costs independent of actual usage. When assets have Resource Allocation Costs (e.g. CPU or GPU), Workload Costs are understood as max(request, usage) — this assigns costs directly reserved or allocated by kube-scheduler. Workload Costs should be calculated at the lowest level possible (the container level) and then aggregated by any dimension.
Workload Cost = max(request, usage) [computed at container level, then aggregated]
Supported Workload Cost Aggregations
d) Shared Costs — In Detail
Shared Workload Costs, Cluster Idle Costs, and Overhead Costs are common costs that organizations can optionally distribute amongst tenants. A common example is system workload costs (e.g. kube-system pods) that benefit all tenants. A full implementation should support various methods of distributing shared costs.
Common Distribution Methods
| Method | Description |
|---|---|
| Uniformly | Distributed evenly across all other tenants. |
| Proportional | Proportionate to a tenant's consumption of Cluster Asset costs. |
| Custom Metric | Based on a custom metric, e.g. bytes of network egress. |
e) Idle Costs — In Detail
Idle Costs can be calculated at both the Asset/Resource level and the Workload level. Asset Idle Costs represent the cost-weighted difference between Cluster Asset Costs and the costs of resources actually allocated or consumed. Workload Idle Costs are a cost-weighted measurement of requested resources that go unused, and can be computed on any grouping of workloads (containers, pods, labels, annotations, namespaces, etc.).
Cluster Idle Cost = ( Cluster Asset Costs - Workload Costs )
Cluster Idle % = Idle Cost / Resource Allocation Costs
Asset Idle Cost can be calculated by individual assets, groups of assets, cluster(s), and by individual resources (e.g. CPU). Resources strictly billed on usage can be viewed as 100% efficient and should not be included when measuring the idle percentage of a cluster (a usage-billed resource never "sits idle").
Asset Cost: Allocated vs Idle
Idle % by Resource (illustrative)
f) Pod States
The state of a pod affects the ability to assign costs and whether a resource is considered allocated. The OpenCost model does not account for resources allocated to pods in ImagePullBackOff.
| State | Cost Allocation | Status |
|---|---|---|
| Running | Max(Usage, request) | Implemented |
| ImagePullBackOff | Request | Currently no charge |