|
| 1 | +--- |
| 2 | +title: OpenCost |
| 3 | +description: "OpenCost Integration for Tenants" |
| 4 | +logo: https://landscape.cncf.io/logos/ba27e92ddd22f942dde50f20b6d6d3f2ac7a0b78baf2471bdd89eb5d105c708f.svg |
| 5 | +type: single |
| 6 | +display: true |
| 7 | +integration: true |
| 8 | +--- |
| 9 | +{{% pageinfo %}} |
| 10 | +This guide explains how to integrate OpenCost with Capsule to provide cost visibility and chargeback/showback per tenant. |
| 11 | +You can group workloads into tenants by annotating namespaces (for example, `opencost.projectcapsule.dev/tenant: {{ tenant.name }}`). |
| 12 | +OpenCost can use this annotation to aggregate costs, enabling accurate cost allocation across clusters, nodes, namespaces, controller kinds, controllers, services, pods, and containers for each tenant. |
| 13 | +{{% /pageinfo %}} |
| 14 | + |
| 15 | +# Prerequisites |
| 16 | +- [Capsule](/docs/operating/setup/installation/) v0.10.8 or later |
| 17 | +- [Prometheus Operator](https://prometheus-operator.dev/docs/getting-started/installation/) |
| 18 | +- [Prometheus](https://opencost.io/docs/installation/prometheus) |
| 19 | +- [OpenCost](https://opencost.io/docs/installation/helm) |
| 20 | + |
| 21 | +# Installation |
| 22 | + |
| 23 | +## Capsule |
| 24 | +1. Create a tenant with spec.namespaceOptions.additionalMetadataList: |
| 25 | + ```bash |
| 26 | + kubectl create -f - << EOF |
| 27 | + apiVersion: capsule.clastix.io/v1beta2 |
| 28 | + kind: Tenant |
| 29 | + metadata: |
| 30 | + name: solar |
| 31 | + spec: |
| 32 | + namespaceOptions: |
| 33 | + additionalMetadataList: |
| 34 | + - annotations: |
| 35 | + opencost.projectcapsule.dev/tenant: "{{ tenant.name }}" |
| 36 | + owners: |
| 37 | + - name: alice |
| 38 | + kind: User |
| 39 | + EOF |
| 40 | + ``` |
| 41 | +
|
| 42 | +## OpenCost |
| 43 | +1. Create a basic OpenCost values file. Set emitNamespaceAnnotations: true because aggregation is based on the Capsule annotation. |
| 44 | + ```yaml |
| 45 | + opencost: |
| 46 | + prometheus: |
| 47 | + internal: |
| 48 | + namespaceName: prometheus-system |
| 49 | + serviceName: prometheus-server |
| 50 | + port: 80 |
| 51 | +
|
| 52 | + dataRetention: |
| 53 | + dailyResolutionDays: 30 # default: 15 |
| 54 | +
|
| 55 | + exporter: |
| 56 | + defaultClusterId: kind-opencost-capsule |
| 57 | + replicas: 1 |
| 58 | + resources: |
| 59 | + requests: |
| 60 | + cpu: "10m" |
| 61 | + memory: "55Mi" |
| 62 | + limits: |
| 63 | + memory: "1Gi" |
| 64 | + persistence: |
| 65 | + enabled: false |
| 66 | +
|
| 67 | + metrics: |
| 68 | + kubeStateMetrics: |
| 69 | + emitNamespaceAnnotations: true |
| 70 | + emitPodAnnotations: true |
| 71 | + emitKsmV1Metrics: false |
| 72 | + emitKsmV1MetricsOnly: false |
| 73 | + serviceMonitor: |
| 74 | + enabled: true |
| 75 | + additionalLabels: |
| 76 | + release: prometheus |
| 77 | + ``` |
| 78 | +2. Install OpenCost with the values above: |
| 79 | + ```bash |
| 80 | + helm install opencost opencost-charts/opencost --namespace opencost --create-namespace -f values.yaml |
| 81 | + ``` |
| 82 | +
|
| 83 | +# Fetch data from OpenCost |
| 84 | +{{% alert title="Note!" color=warning %}} |
| 85 | +Aggregation is only possible via the API. There is no option to aggregate using the UI. |
| 86 | +{{% /alert %}} |
| 87 | +
|
| 88 | +1. Port-forward: |
| 89 | + ```bash |
| 90 | + kubectl -n opencost port-forward deployment/opencost 9003:9003 9090:9090 |
| 91 | + ``` |
| 92 | +2. Query the API: |
| 93 | + - Aggregate by namespace: |
| 94 | + ```bash |
| 95 | + curl -G http://localhost:9003/allocation \ |
| 96 | + -d window=1h \ |
| 97 | + -d aggregate=namespace,annotation:opencost_projectcapsule_dev_tenant \ |
| 98 | + -d resolution=1h |
| 99 | + ``` |
| 100 | + - Aggregate by pod: |
| 101 | + ```bash |
| 102 | + curl -G http://localhost:9003/allocation \ |
| 103 | + -d window=1h \ |
| 104 | + -d aggregate=pod,annotation:opencost_projectcapsule_dev_tenant \ |
| 105 | + -d resolution=1h |
| 106 | + ``` |
| 107 | + - Aggregate by deployment: |
| 108 | + ```bash |
| 109 | + curl -G http://localhost:9003/allocation \ |
| 110 | + -d window=1h \ |
| 111 | + -d aggregate=deployment,annotation:opencost_projectcapsule_dev_tenant \ |
| 112 | + -d resolution=1h |
| 113 | + ``` |
0 commit comments