Version v0.10.0 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

OpenCost

OpenCost Integration for Tenants

This guide explains how to integrate OpenCost with Capsule to provide cost visibility and chargeback/showback per tenant. You can group workloads into tenants by annotating namespaces (for example, opencost.projectcapsule.dev/tenant: {{ tenant.name }}). 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.

Prerequisites

Installation

Capsule

  1. Create a tenant with spec.namespaceOptions.additionalMetadataList:
    kubectl create -f - << EOF
    apiVersion: capsule.clastix.io/v1beta2
    kind: Tenant
    metadata:
      name: solar
    spec:
      namespaceOptions:
        additionalMetadataList:
        - annotations:
            opencost.projectcapsule.dev/tenant: "{{ tenant.name }}"
      owners:
      - name: alice
        kind: User
    EOF
    

OpenCost

  1. Create a basic OpenCost values file. Set emitNamespaceAnnotations: true because aggregation is based on the Capsule annotation.
    opencost:
      prometheus:
        internal:
          namespaceName: prometheus-system
          serviceName: prometheus-server
          port: 80
    
      dataRetention:
        dailyResolutionDays: 30  # default: 15
    
      exporter:
        defaultClusterId: kind-opencost-capsule
        replicas: 1
        resources:
          requests:
            cpu: "10m"
            memory: "55Mi"
          limits:
            memory: "1Gi"
        persistence:
          enabled: false
    
      metrics:
        kubeStateMetrics:
          emitNamespaceAnnotations: true
          emitPodAnnotations: true
          emitKsmV1Metrics: false
          emitKsmV1MetricsOnly: false
        serviceMonitor:
          enabled: true
          additionalLabels:
            release: prometheus
    
  2. Install OpenCost with the values above:
    helm install opencost opencost-charts/opencost --namespace opencost --create-namespace -f values.yaml
    

Fetch data from OpenCost

  1. Port-forward:
    kubectl -n opencost port-forward deployment/opencost 9003:9003 9090:9090
    
  2. Query the API:
    • Aggregate by namespace:
      curl -G http://localhost:9003/allocation \
        -d window=1h \
        -d aggregate=namespace,annotation:opencost_projectcapsule_dev_tenant \
        -d resolution=1h
      
    • Aggregate by pod:
      curl -G http://localhost:9003/allocation \
        -d window=1h \
        -d aggregate=pod,annotation:opencost_projectcapsule_dev_tenant \
        -d resolution=1h
      
    • Aggregate by deployment:
      curl -G http://localhost:9003/allocation \
        -d window=1h \
        -d aggregate=deployment,annotation:opencost_projectcapsule_dev_tenant \
        -d resolution=1h