Cloud Bill Out of Control: Building a Reliable Budget Alert with OpenCost and Prometheus
You get the email on the 3rd of the month: last month's cloud invoice is 40% above forecast. You open the billing console, and it tells you exactly one useful thing — the account spent too much. Which team? Which workload? Which namespace started a nightly job that requests 64 GiB of RAM and never finishes? The billing console has no idea, because it has never heard of your namespaces. This…
Cloud expenses can easily exceed initial estimates if caught only after billing arrives. This article explains how to create a budget alert system using OpenCost and Prometheus that warns of overspending in near real-time. Native cloud budget alerts often arrive too late, operating on account or project level budgets that are evaluated on delayed billing data.
Kubernetes clusters appear as a few EC2 instances and EBS volumes to the provider, so each namespace shares the same bill line. Billing data arrives in batches with 6-24 hour delays, making it impossible to detect runaway deployments that consume budget over several days before triggering.
OpenCost solves this by watching cluster allocation, multiplying it by node and volume costs, and exporting the result as Prometheus metrics. This provides a local, resolution-dependent estimate of allocation costs in seconds. While not as precise as actual invoices, a 5-minute estimate pinpointing a specific namespace beats delayed account-level numbers.
OpenCost exposes important metrics like container CPU and memory allocation, persistent volume consumption, and hourly costs per node and volume. Default pricing uses public on-demand rates, but custom pricing files can improve accuracy.
To deploy OpenCost on OpenShift, first enable user workload monitoring by modifying ConfigMaps, then install OpenCost using Helm. Disable the bundled Prometheus and UI for security. Verify the exporter produces prices, and if needed, supply a custom pricing file. Finally, query the hourly node and volume costs through Thanos to get the cluster's hourly compute burn rate, which can be multiplied by 730 for a monthly run rate.
This budget alert system provides early warning for excessive cloud spending at a per-namespace level.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.