Multi-tenant SaaS Platforms — Cells, Rings & Progressive Delivery›10 · Cost per tenant

Lesson 10 of 16 · Edge, isolation and cost

Cost per tenant

Show what each tenant and team costs: label everything, install OpenCost through GitOps, make kube-state-metrics export the labels it ignores by default, query allocation by tenant, and decide fairly how shared and idle costs are split. Tested on a lab cell, including the two problems found.

Advanced
Key wordsFinOpscost allocationOpenCostKubecostshowbackchargebackcost per tenantlabelskube-state-metricsmetricLabelsAllowlistshared costsidle costunit economics

Why cost per tenant

In a multi-tenant platform, finance asks "what does this customer cost us?" and engineering asks "which team or tenant is driving spend?". Without allocation, the answer is a single cloud bill. With it you get unit economics (cost per tenant, per transaction) and a way to spot waste.

The school pays one electricity bill. By putting a small meter in each classroom, it can see which rooms leave the heater on overnight, and the shared corridors are split fairly by an agreed rule everyone can read.

Label everything

Cost allocation is only as good as the labels:

  • Namespaces carry team, cost-center and, for tenant-dedicated namespaces, tenant.example.io/id.
  • Workloads inherit them through the golden path (templates and onboarding policy add them; a validation policy rejects workloads without them).
  • Cloud resources created by Crossplane or Terraform get the same tags, so database and storage costs join the picture.

Install OpenCost through GitOps

OpenCost multiplies resource usage by prices (cloud price APIs or a custom price list) and allocates cost per container, then aggregates by namespace, label or controller. It needs Prometheus. Deliver both as fleet add-ons:

# Prometheus chart values (excerpt)
server:
  fullnameOverride: prometheus-server       # a stable Service name OpenCost can find
kube-state-metrics:
  metricLabelsAllowlist:                    # exports NO labels by default
  - pods=[*]
  - namespaces=[tenant.example.io/id,team,cost-center]
---
# OpenCost chart values (excerpt)
podLabels: { app.kubernetes.io/part-of: opencost }   # satisfy the platform's own label policy
opencost:
  prometheus:
    internal: { enabled: true, serviceName: prometheus-server, namespaceName: monitoring, port: 80 }

Two problems found while testing

  • Everything showed as __unallocated__ when aggregating by tenant: kube-state-metrics exports no Kubernetes labels unless they are allow-listed, so the tenant label never reached Prometheus.
  • The platform's own admission policy blocked OpenCost, because its pods lacked the required app.kubernetes.io/part-of label. The fix was to set the label through the chart's values, not to weaken the policy. Platform components follow the same guard-rails as tenants.

Query and report

$ kubectl -n opencost port-forward svc/opencost 9003:9003
$ curl -s 'localhost:9003/allocation/compute?window=7d&aggregate=label:tenant.example.io/id&accumulate=true' | jq '.data[0] | to_entries[] | {tenant: .key, cost: .value.totalCost}'

Allocation needs some minutes of data before numbers appear. Feed results into a weekly report or dashboard per team and per tenant.

Shared and idle costs

Not everything belongs to one tenant: control plane, monitoring, the edge, and idle capacity (nodes paid for but not requested).

  • Choose a documented allocation key, for example proportional to each tenant's requested CPU and memory.
  • Show idle separately: it is the platform team's optimisation target (bin-packing, autoscaling, right-sizing), not something to hide in tenants' bills.
  • Start with showback (reports) before chargeback (internal billing); people need to trust the numbers first.

Act on it

  • Right-size requests using actual usage (VPA recommendations).
  • Scale non-production to zero outside working hours.
  • Move steady load to committed-use pricing and stateless batch work to spot capacity.
  • Find tenants whose cost exceeds their contract value: a product and pricing conversation, backed by data.

Try it

On one lab cluster, deploy Prometheus and OpenCost with the values above through an ApplicationSet. Create two namespaces labelled with different tenant IDs and run a small workload in each (one twice as large). After ten minutes, query allocation by namespace and by tenant label, and explain the __idle__ and __unallocated__ entries.

Recap

  • Label namespaces, workloads and cloud resources consistently through the golden path.
  • OpenCost + Prometheus allocate cost by namespace or label; allow-list labels in kube-state-metrics.
  • Platform add-ons comply with the platform's own policies.
  • Split shared costs by a documented key, show idle separately, and start with showback.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.