# Cost Analysis

Source: https://docs.ops0.com/docs/kubernetes/cost-analysis

ops0 integrates with OpenCost to break down Kubernetes spending by namespace, workload, pod, and label. Understand exactly what each team, application, and service costs to run.

- **Namespace Costs**: See the total monthly cost for every namespace at a glance.

- **Workload Breakdown**: Drill into Deployments, StatefulSets, and DaemonSets to find cost outliers.

- **Label-Based Views**: Group costs by any label — team, environment, or application.

---

## How It Works

OpenCost runs inside your cluster and collects real-time resource usage data from the Kubernetes metrics server and cloud pricing APIs. ops0 reads from OpenCost and presents cost breakdowns in the dashboard, letting you navigate costs without leaving the ops0 UI.

---

## Installing OpenCost

ops0 can install OpenCost into your cluster with a single click. No manual Helm commands required.

### Navigate to Cost Analysis
Open your cluster in ops0 and click the **Cost Analysis** tab.

### Click Install OpenCost
ops0 will apply the OpenCost Helm chart to your cluster. This requires deploy permissions on the cluster.

### Watch the Installation Progress
The status indicator updates in real time: **Pending → Installing → Running**.

### Wait for Data
Once the status shows **Running**, cost data begins populating within 15 minutes.

**Resource Requests Required**

OpenCost requires CPU and memory requests to be set on your workloads. Without resource requests, costs cannot be allocated accurately and will appear as $0 for affected workloads.

---

## Cost Views

ops0 provides four cost aggregation views. Switch between them using the view selector at the top of the Cost Analysis tab.

| View | What It Shows |
| --- | --- |
| **Namespace** | Total monthly cost per Kubernetes namespace |
| **Workload** | Cost breakdown per Deployment, StatefulSet, and DaemonSet |
| **Pod** | Individual pod cost broken down by CPU, memory, and storage |
| **Label** | Cost grouped by any label key — useful for team, environment, or app grouping |

---

## Cost Metrics

Each cost view reports the following metrics:

| Metric | Description |
| --- | --- |
| **CPU Cost** | Based on requested CPU multiplied by the node's effective hourly price |
| **Memory Cost** | Based on requested memory multiplied by the node's effective hourly price |
| **Storage Cost** | Derived from PersistentVolumeClaim usage and the backing StorageClass price |
| **Network Cost** | Egress data transfer charges where provider pricing data is available |
| **Total Cost** | Sum of CPU, memory, storage, and network costs |

---

## Time Range

Select the time window for cost analysis using the date picker at the top of the page:

| Range | Description |
| --- | --- |
| **Last 24h** | Cost for the previous 24 hours |
| **Last 7 days** | Rolling 7-day total |
| **Last 30 days** | Rolling 30-day total (closest to monthly billing) |
| **Custom** | Any date range — useful for comparing billing periods |

---

## Cost Optimization Recommendations

ops0 analyzes cost data and flags workloads that are strong candidates for right-sizing.

**Over-provisioned workloads** — Requested resources are consistently above actual usage. ops0 surfaces the recommended request values based on observed p95 usage over the selected time window.

**Burstable QoS candidates** — Pods using the `BestEffort` or `Burstable` QoS class when `Guaranteed` would reduce per-unit cost on reserved or committed-use instances.

**Idle namespaces** — Namespaces with no active pods or zero resource consumption for 7 or more days. These are highlighted as candidates for cleanup.

---

## Exporting Cost Data

Download a CSV of the current cost allocation report by clicking **Export** in the top-right corner of the Cost Analysis tab. The export includes all rows visible in the current view and time range, with one row per namespace, workload, or pod depending on the active aggregation level.

---

## Troubleshooting

**OpenCost shows $0 for all workloads**

Resource requests are not set on your workloads. OpenCost requires `resources.requests.cpu` and `resources.requests.memory` to be defined on pod specs to allocate costs. Update your Deployment manifests and re-deploy.

**Cost data is stale or not updating**

Verify the OpenCost pod is healthy in the `opencost` namespace:

```bash
kubectl get pods -n opencost
```

The pod should be in `Running` state. If it is in `CrashLoopBackOff` or `Pending`, check pod events with `kubectl describe pod -n opencost <pod-name>`.

**New namespaces are not appearing**

OpenCost collects metrics on a scrape interval. New namespaces can take up to one hour to appear in cost reports after their first workload is deployed.

**Cost data is inaccurate for cloud-managed nodes**

Ensure your cloud provider integration is configured in ops0 settings. Without provider pricing data, OpenCost falls back to on-demand list prices which may not reflect your actual negotiated or committed-use pricing.

---

## Related

- [Clusters](https://docs.ops0.com/docs/kubernetes/clusters): Connect and manage your Kubernetes clusters.

- [Nodes](https://docs.ops0.com/docs/kubernetes/nodes): View node-level resource usage and capacity.

- [Workloads](https://docs.ops0.com/docs/kubernetes/workloads): Monitor Deployments, StatefulSets, and DaemonSets.
