DEV Community

Alex Spinov
Alex Spinov

Posted on

OpenCost Has a Free API: Track Your Kubernetes Spending in Real-Time

What is OpenCost?

OpenCost is an open-source CNCF project that provides real-time cost monitoring for Kubernetes clusters. It breaks down your cloud bill by namespace, deployment, pod, and even container — showing exactly where your money goes.

Why OpenCost?

  • 100% free and open-source — CNCF sandbox project
  • Real-time cost allocation — not monthly bills, but live spending
  • Multi-cloud — AWS, GCP, Azure, on-prem pricing
  • Namespace-level — chargeback per team/service
  • Prometheus integration — export cost metrics alongside performance data
  • No vendor lock-in — unlike Kubecost Pro or CloudHealth

Quick Start

# Install via Helm
helm install opencost opencost/opencost \
  --namespace opencost --create-namespace \
  --set opencost.prometheus.internal.enabled=true

# Port forward to UI
kubectl port-forward -n opencost svc/opencost 9090:9090
# Open http://localhost:9090
Enter fullscreen mode Exit fullscreen mode

Query Costs via API

# Get cost allocation by namespace (last 24h)
curl -s 'http://localhost:9090/allocation/compute?window=24h&aggregate=namespace' | jq '.data[0]'

# Get cost by deployment
curl -s 'http://localhost:9090/allocation/compute?window=7d&aggregate=deployment' | jq '.data'

# Get cost by label (e.g., team)
curl -s 'http://localhost:9090/allocation/compute?window=30d&aggregate=label:team' | jq '.data'
Enter fullscreen mode Exit fullscreen mode

API Response Example

{
  "production": {
    "name": "production",
    "cpuCost": 45.23,
    "gpuCost": 0,
    "ramCost": 28.67,
    "pvCost": 12.40,
    "networkCost": 5.30,
    "totalCost": 91.60,
    "cpuEfficiency": 0.34,
    "ramEfficiency": 0.52
  }
}
Enter fullscreen mode Exit fullscreen mode

Prometheus Metrics

# Grafana dashboard queries

# Total cluster cost per day
sum(opencost_cluster_cost_total) by (cluster)

# Cost by namespace
sum(opencost_allocation_cost_total) by (namespace)

# CPU efficiency (actual vs requested)
opencost_allocation_cpu_usage / opencost_allocation_cpu_request

# Idle resources cost (wasted money)
sum(opencost_allocation_cpu_idle_cost + opencost_allocation_ram_idle_cost)
Enter fullscreen mode Exit fullscreen mode

Set Up Alerts for Cost Spikes

# PrometheusRule for cost alerts
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: cost-alerts
spec:
  groups:
    - name: cost-alerts
      rules:
        - alert: NamespaceCostSpike
          expr: |
            sum by (namespace) (rate(opencost_allocation_cost_total[1h])) * 24
            > 100
          for: 30m
          labels:
            severity: warning
          annotations:
            summary: "Namespace {{ $labels.namespace }} spending >$100/day"
        - alert: LowCPUEfficiency
          expr: |
            opencost_allocation_cpu_usage / opencost_allocation_cpu_request < 0.1
          for: 2h
          annotations:
            summary: "{{ $labels.namespace }}/{{ $labels.pod }} using <10% of requested CPU"
Enter fullscreen mode Exit fullscreen mode

OpenCost vs Alternatives

Feature OpenCost Kubecost CloudHealth Spot.io
Cost Free Free/Pro Enterprise Enterprise
Open source Yes (CNCF) Partial No No
Real-time Yes Yes Hourly Hourly
Multi-cloud Yes Yes Yes Yes
API REST REST REST REST
GPU costs Yes Pro only Limited No

Real-World Impact

A SaaS company discovered through OpenCost that their staging namespace cost $2,100/month — almost as much as production. Investigation revealed: 15 forgotten load tests left running, 8 dev deployments with production-sized resource requests. After cleanup: staging costs dropped to $340/month, saving $21K/year.


Overspending on Kubernetes? I help teams implement cost monitoring and right-sizing. Contact spinov001@gmail.com or explore my automation tools on Apify.

Top comments (0)