OpenCost Deployment for Google Cloud
This guide explains how to deploy OpenCost for a GKE cluster.
Prerequisites
Before starting the deployment, ensure you have the following:
Infrastructure:
GKE cluster (Standard mode, not Autopilot*) with Workload Identity enabled
Cloud permissions (GCP IAM roles):
roles/container.admin– required for GKE managementroles/iam.serviceAccountAdmin– required for Service Account managementroles/storage.admin– required for GCS bucket managementroles/apikeys.admin– required for API key management
Tools:
gcloudCLI toolkubectlCLI toolhelmCLI tool (v3.x or later)
*Autopilot is not supported due to the managed Prometheus configuration.
STEP 1. Service account setup
Create a service account
In the Google Cloud console, navigate to IAM & Admin -> Service Accounts.
Click +Create service account.
Set the following:
Name:
opencost-saDescription: Service account for OpenCost
Assign the required roles:
roles/compute.viewerroles/bigquery.admin(only if using BigQuery integration)roles/storage.admin(required for Parquet export)
Click Done to create the account.
Configure workload identity
Return to the Service Accounts list. Open the newly created service account.
Select the tab ‘Principals with access’. Click Grant access.
Add the following principal:
YOUR_GCP_PROJECT_ID.svc.id.goog[opencost/opencost-sa]whereYOUR_GCP_PROJECT_IDis a placeholder for your GCP Project IDAssign the Workload Identity User role and save.
Create a service account key
Navigate back to the Service Accounts list. Click on Actions (three-dot) menu → Manage keys.
Click Add Key → Create new key.
Select JSON and click Create.
Download and store the key securely. The key will be required 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 the box ‘Cloud Billing API’ (
cloudbilling.googleapis.com) → OK. Click Save.
STEP 2. Storage bucket setup
Navigate to Cloud Storage -> Buckets.
Click +Create.
Configure the bucket:
Name:
opencost-bucketLocation type: Region
Storage class: Standard
Leave the remaining settings as is.
Click Create.
STEP 3. Create a cluster connection file
Navigate to Kubernetes Engine → Clusters.
Click the Actions (three-dot) menu next to the cluster and select Connect.
Copy the
gcloudcommand and run it in your terminal. This generates the kubeconfig file needed to access the cluster.
STEP 4. Prometheus deployment
Add the Prometheus Helm repository:
CODEhelm repo add prometheus-community <https://prometheus-community.github.io/helm-charts>Create the Prometheus namespace:
CODEkubectl create namespace prometheusInstall Prometheus:
CODEhelm install prometheus prometheus-community/prometheus \\ --namespace prometheus \\ --set prometheus-pushgateway.enabled=false \\ --set alertmanager.enabled=false
STEP 5. OpenCost deployment
Add the OpenCost Helm repository:
CODEhelm repo add opencost <https://opencost.github.io/opencost-helm-chart>Create the OpenCost namespace:
CODEkubectl create namespace opencostCreate a Kubernetes secret with the service account key. Replace
/path/to/your/service-account-key.jsonwith your file path.CODEkubectl create secret generic google-application-credentials \\ --from-file=config.json=/path/to/your/service-account-key.json \\ --namespace opencostInstall OpenCost. Replace
<YOUR_PROJECT_ID>and<YOUR_API_KEY>in the command below:CODEhelm --namespace opencost upgrade --install opencost opencost/opencost -f - <<EOF serviceAccount: create: true name: opencost-sa annotations: iam.gke.io/gcp-service-account: "opencost-sa@<YOUR_PROJECT_ID>.iam.gserviceaccount.com" opencost: prometheus: namespaceName: prometheus exporter: cloudProviderApiKey: <YOUR_API_KEY> podAnnotations: prometheus.io/path: /metrics prometheus.io/port: "9003" prometheus.io/scrape: "true" EOF
These podAnnotations ensure that Prometheus scrapes metrics from the OpenCost pods, enabling label-based cost attribution in reports.
STEP 6. Parquet Exporter setup
Install the OpenCost Parquet Exporter. Replace
<YOUR_GKE_CLUSTER_ID>in the command below:CODEhelm install parquet-exporter opencost/opencost-parquet-exporter \\ --namespace opencost \\ --set schedule="0 */12 * * *" \\ --set existingServiceAccount=opencost-sa \\ --values - <<EOF resources: limits: cpu: "1" memory: 1Gi requests: cpu: 100m memory: 100Mi env: - name: OPENCOST_PARQUET_SVC_HOSTNAME value: opencost.opencost.svc.cluster.local - name: OPENCOST_PARQUET_STORAGE_BACKEND value: gcp - name: OPENCOST_PARQUET_FILE_KEY_PREFIX value: "kubernetes_clusters/<YOUR_GKE_CLUSTER_ID>" - name: OPENCOST_PARQUET_JSON_SEPARATOR value: "_" - name: OPENCOST_PARQUET_GCP_BUCKET_NAME value: opencost-bucket - name: OPENCOST_PARQUET_GCP_CREDENTIALS_JSON valueFrom: secretKeyRef: name: google-application-credentials key: config.json EOF
*For multi-cluster environments, deploy the Parquet Explorer separately on each cluster.
STEP 7. Verification
Check that all pods are running:
CODEkubectl get pods -n prometheus kubectl get pods -n opencostAccess the OpenCost UI:
CODEkubectl port-forward -n opencost service/opencost 9003:9003Then open
http://localhost:9003in your browser.
The first Parquet export may take up to 24 hours. To verify the export, check your storage bucket for newly created files.