OpenCost on Google Cloud
Use this guide to deploy OpenCost components for Kubernetes Billing exports to Google Cloud Storage.
Prerequisites
Before you begin, make sure you have:
- Infrastructure
- A GKE cluster in Standard mode (not Autopilot) with Workload Identity enabled
- Required cloud permissions (GCP IAM roles)
roles/container.adminfor GKE administrationroles/iam.serviceAccountAdminfor managing service accountsroles/storage.adminfor managing the GCS bucketroles/apikeys.adminfor managing API keys
- Required tools
- Google Cloud CLI (
gcloud) kubectl- Helm (v3.x or later)
- Google Cloud CLI (
Autopilot is not supported due to the managed Prometheus configuration.
Set Up Access
Create a Service Account
- In the Google Cloud console, navigate to IAM & Admin → Service Accounts.
- Click + Create service account.
- Set:
- Name:
opencost-sa(service account name used in this guide). - Description: Service account for OpenCost.
- Name:
- Assign the role:
roles/compute.viewer.
- Click Done to create the account.
Configure Workload Identity
- Open the newly created service account.
- Select the Principals with access tab and click Grant access.
- Add the following principal:
{YOUR_GCP_PROJECT_ID}.svc.id.goog[opencost/opencost-sa](replace{YOUR_GCP_PROJECT_ID}andopencost-saas needed). - Assign the
Workload Identity Userrole and save.
Create a Service Account Key
- Navigate back to the Service Accounts list.
- For
opencost-sa, open the Actions (three‑dot) menu → Manage keys. - Click Add key → Create new key.
- Select JSON and click Create.
- Download and store the key securely; it will be used later when creating the Kubernetes secret.
Create an API Key
- Navigate to APIs & Services → Credentials.
- Click + Create credentials and select API key.
- Copy the generated key.
- From the Actions (three‑dot) menu of the new key, select Edit API key.
- Under API restrictions, select Restrict key.
- Check Cloud Billing API (
cloudbilling.googleapis.com) → OK, then Save.
Set Up the Storage Bucket
- Navigate to Cloud Storage → Buckets.
- Click + Create and configure:
- Name: for example,
opencost-bucket(must be globally unique). - Location type: Region.
- Storage class: Standard.
- Name: for example,
- Leave remaining settings as is and click Create.
Grant bucket access:
- Open the bucket and go to Permissions → + Grant access.
- Select:
- Principal:
opencost-sa@{YOUR_GCP_PROJECT_ID}.iam.gserviceaccount.com(replace{YOUR_GCP_PROJECT_ID}as needed). - Role:
Storage Object User.
- Principal:
- Click Save.
Use the storage bucket name consistently in the later steps.
Create the Cluster Connection
- In the Google Cloud console, go to Kubernetes Engine → Clusters.
- For the target cluster, open the Actions (three‑dot) menu and select Connect.
- Copy the
gcloudcommand and run it in your terminal to generate/update yourkubeconfigfor the cluster.
Deploy Prometheus
-
Add the Prometheus Helm repository:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts -
Create the Prometheus namespace:
kubectl create namespace prometheus-system -
Install Prometheus:
helm install prometheus prometheus-community/prometheus \--namespace prometheus-system \--set prometheus-pushgateway.enabled=false \--set alertmanager.enabled=false
If you use an external Prometheus instance, skip this step and review the Configuration notes (1) for external Prometheus below.
Deploy OpenCost
-
Add the OpenCost Helm repository:
helm repo add opencost https://opencost.github.io/opencost-helm-chart -
Create the OpenCost namespace:
kubectl create namespace opencost -
Create a Kubernetes secret with the service account key. Replace
/path/to/your/service-account-key.jsonwith the path to the JSON key created earlier:kubectl create secret generic google-application-credentials \--from-file=config.json=/path/to/your/service-account-key.json \--namespace opencost -
Install OpenCost. Replace
{YOUR_PROJECT_ID}and{YOUR_API_KEY}:helm --namespace opencost upgrade --install opencost opencost/opencost -f - <<EOFpodSecurityContexts:runAsUser: 1001runAsGroup: 1001fsGroup: 1001extraVolumes:- name: configsemptyDir: {}serviceAccount:create: truename: opencost-saannotations:iam.gke.io/gcp-service-account: "opencost-sa@{YOUR_PROJECT_ID}.iam.gserviceaccount.com"opencost:prometheus:namespaceName: prometheus-systemexporter:cloudProviderApiKey: {YOUR_API_KEY}extraVolumeMounts:- mountPath: /var/configsname: configsreadOnly: falsepodAnnotations:prometheus.io/path: /metricsprometheus.io/port: "9003"prometheus.io/scrape: "true"EOFtipThe
podAnnotationsensure that Prometheus scrapes metrics from the OpenCost pods, enabling label‑based cost attribution in reports.
Configuration Notes
Configure the following Helm values depending on your setup:
-
If your cluster already has an external Prometheus, configure OpenCost to use the existing Prometheus instance. Replace
{YOUR_PROMETHEUS_URL}with the correct endpoint:opencost:prometheus:external:enabled: trueurl: {YOUR_PROMETHEUS_URL} -
If your cluster uses
prometheus-operator, enableServiceMonitor; otherwise, OpenCost metrics will not be scraped automatically:opencost:metrics:serviceMonitor:enabled: true
Deploy the Parquet Exporter
Install the OpenCost Parquet exporter.
-
To store reports in subfolders or use a naming prefix, use the command below, replacing
{YOUR_CLUSTER_PREFIX},{YOUR_GKE_CLUSTER_ID}and{YOUR_OPENCOST_PARQUET_GCP_BUCKET_NAME}as needed:helm install parquet-exporter opencost/opencost-parquet-exporter \--namespace opencost \--set schedule="0 */12 * * *" \--set existingServiceAccount=opencost-sa \--values - <<EOFresources:limits:cpu: "1"memory: 1Girequests:cpu: 100mmemory: 100Mienv:- name: OPENCOST_PARQUET_SVC_HOSTNAMEvalue: "opencost.opencost.svc.cluster.local"- name: OPENCOST_PARQUET_STORAGE_BACKENDvalue: "gcp"- name: OPENCOST_PARQUET_FILE_KEY_PREFIXvalue: "{YOUR_CLUSTER_PREFIX}/{YOUR_GKE_CLUSTER_ID}"- name: OPENCOST_PARQUET_JSON_SEPARATORvalue: "_"- name: OPENCOST_PARQUET_GCP_BUCKET_NAMEvalue: "{YOUR_OPENCOST_PARQUET_GCP_BUCKET_NAME}"- name: OPENCOST_PARQUET_GCP_CREDENTIALS_JSONvalueFrom:secretKeyRef:name: google-application-credentialskey: config.jsonEOF -
To store reports in the root folder, use this variant instead, replacing
{YOUR_GKE_CLUSTER_ID}and{YOUR_OPENCOST_PARQUET_GCP_BUCKET_NAME}as needed:helm install parquet-exporter opencost/opencost-parquet-exporter \--namespace opencost \--set schedule="0 */12 * * *" \--set existingServiceAccount=opencost-sa \--values - <<EOFresources:limits:cpu: "1"memory: 1Girequests:cpu: 100mmemory: 100Mienv:- name: OPENCOST_PARQUET_SVC_HOSTNAMEvalue: "opencost.opencost.svc.cluster.local"- name: OPENCOST_PARQUET_STORAGE_BACKENDvalue: "gcp"- name: OPENCOST_PARQUET_FILE_KEY_PREFIXvalue: "{YOUR_GKE_CLUSTER_ID}/"- name: OPENCOST_PARQUET_JSON_SEPARATORvalue: "_"- name: OPENCOST_PARQUET_GCP_BUCKET_NAMEvalue: "{YOUR_OPENCOST_PARQUET_GCP_BUCKET_NAME}"- name: OPENCOST_PARQUET_GCP_CREDENTIALS_JSONvalueFrom:secretKeyRef:name: google-application-credentialskey: config.jsonEOF
For {YOUR_GKE_CLUSTER_ID}, Cloudaware expects a value in the format:
projects/{YOUR_PROJECT_ID}/zones/{YOUR_ZONE}/clusters/{YOUR_CLUSTER_NAME}
To retrieve this value, run:
gcloud container clusters describe {YOUR_CLUSTER_NAME} \
--project {YOUR_GCP_PROJECT_ID} \
--region {YOUR_GCP_REGION} \
--format="value(selfLink)" | sed 's|.*/projects/|projects/|'
For multi‑cluster environments, deploy the Parquet exporter separately on each cluster.
Verify the Deployment
-
Check that all pods are running:
kubectl get pods -n prometheus-systemkubectl get pods -n opencost -
Access the OpenCost UI:
kubectl port-forward -n opencost service/opencost 9003:9003
Then open http://localhost:9003 in your browser.
The first Parquet export may take up to 24 hours. To verify the export, check your storage bucket for newly created files.
Next Steps
Return to the parent guide Kubernetes Billing (OpenCost) to proceed with the Kubernetes Billing setup in Cloudaware.