Configure Cloud SQL Proxy for DBO

Configure DBO on Google Kubernetes Engine (GKE) to use Cloud SQL Proxy for the upstream connection to Cloud SQL. Your application connects to DBO with its existing database username and password and no longer needs a Cloud SQL Proxy sidecar.

In GKE, the app pod connects to the db-proxy pod. The db-proxy container connects through the cloud-sql-proxy container in the castai-db-optimizer namespace to Cloud SQL.

Application traffic passes through DBO and Cloud SQL Proxy before reaching Cloud SQL.

The application continues to authenticate with its existing database username and password. Cloud SQL Proxy secures the upstream connection from DBO to Cloud SQL.

Prerequisites

  • An existing Cloud SQL instance connected to DBO in the Cast AI console. If you have not connected an instance, complete Cloud SQL quick start first.
  • A GKE cluster with network access to the Cloud SQL instance with Workload Identity enabled.
  • kubectl configured to access the cluster.
  • gcloud and helm CLIs installed.
  • Permissions to create Google service accounts, grant IAM roles, and deploy DBO.
  • An existing database username and password for the application.

Create the DBO identity

Cloud SQL Proxy needs a Google service account (GSA) with the Cloud SQL Client role. Workload Identity lets the DBO pod use this service account without a credential file.

Set the Google Cloud and DBO identity variables

export PROJECT_ID="your-project-id"
export DBO_SA_NAME="db-proxy"
export DBO_SA_EMAIL="${DBO_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
export DBO_NAMESPACE="castai-db-optimizer"
export DBO_KSA_NAME="db-proxy"

Replace your-project-id with your Google Cloud project ID. Service account IDs must contain 6 to 30 characters and must be unique within the project.

Create the DBO service account

gcloud iam service-accounts create "${DBO_SA_NAME}" \
  --project="${PROJECT_ID}" \
  --display-name="DBO Cloud SQL Proxy" \
  --description="Establishes the connection from DBO to Cloud SQL through Cloud SQL Proxy"

Grant the Cloud SQL Client role to the DBO service account

gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
  --member="serviceAccount:${DBO_SA_EMAIL}" \
  --role="roles/cloudsql.client"

This role allows Cloud SQL Proxy to establish a secure connection to Cloud SQL. This authentication path does not require the Cloud SQL Instance User role or an IAM database user: the application continues to authenticate with its existing database username and password.

Allow the DBO Kubernetes service account to impersonate the DBO GSA

gcloud iam service-accounts add-iam-policy-binding "${DBO_SA_EMAIL}" \
  --project="${PROJECT_ID}" \
  --role="roles/iam.workloadIdentityUser" \
  --member="serviceAccount:${PROJECT_ID}.svc.id.goog[${DBO_NAMESPACE}/${DBO_KSA_NAME}]"

The Workload Identity pool is ${PROJECT_ID}.svc.id.goog. If your GKE cluster belongs to a different project, use that cluster's Workload Identity pool in the member string instead.

Create and annotate the DBO Kubernetes service account

If you used Terraform, make sure you have the environment variables from step 1 set before continuing.

kubectl create namespace "${DBO_NAMESPACE}" \
  --dry-run=client -o yaml | kubectl apply -f -

kubectl create serviceaccount "${DBO_KSA_NAME}" \
  --namespace="${DBO_NAMESPACE}" \
  --dry-run=client -o yaml | kubectl apply -f -

kubectl annotate serviceaccount "${DBO_KSA_NAME}" \
  --namespace="${DBO_NAMESPACE}" \
  "iam.gke.io/gcp-service-account=${DBO_SA_EMAIL}" \
  --overwrite

Redeploy DBO

Configure DBO to run Cloud SQL Proxy and connect to your Cloud SQL instance.

Add the Cloud SQL Proxy Helm values

Add the following configurations to your existing db-proxy Helm values:

serviceAccountName: db-proxy
cloudSqlProxy:
  enabled: true
  privateIp: true
  autoIamAuthn: false
  • serviceAccountName specifies the annotated DBO KSA.
  • enabled: true adds Cloud SQL Proxy to the DBO deployment.
  • privateIp: true connects to Cloud SQL over private IP (set to false if using public IP).
  • autoIamAuthn: false disables automatic IAM database authentication. Your application continues using its existing database credentials.

Redeploy DBO

Run the deployment command from the Cast AI console with your customized Helm values file.

Verify the deployment

Check that the DBO pods are running and all containers are ready:

kubectl get pods -n castai-db-optimizer

Connect your application

Open the endpoint settings

In the Cast AI console, open the DBO instance and go to Settings → Endpoints.

Copy the connection details

In Caching configuration, copy the connection string or the DBO HOST and DBO PORT values.

Read/Write endpoint configuration. Caching configuration lists DBO HOST db-proxy-gcp-d3238.castai-db-optimizer, DBO PORT 5432, and connection string postgres://db-proxy-gcp-d3238.castai-db-optimizer:5432.

Copy the DBO host and port from the Read/Write endpoint's Caching configuration.

Update the application connection settings

Update the application's database connection settings to use the copied DBO values. Keep the existing database name, username, and password unchanged.

See Connecting client applications for connection-string details.

Deploy and verify the application

Deploy the updated application and run its normal database reads and writes through the DBO host and port. Confirm that these operations succeed with its existing database username and password. You can also check the relevant cloud-sql-proxy container logs for accepted local connections to confirm that traffic traverses the new tunnel. If the application has a Cloud SQL Proxy sidecar, keep it until this application-level verification succeeds.

Remove an existing application sidecar

After verification, remove the application's Cloud SQL Proxy sidecar if it has one. Remove any volumes, settings, or credentials that only that sidecar used.

Your application no longer needs a Cloud SQL Proxy sidecar. DBO manages the upstream Cloud SQL connection.

Next steps

For a multi-application setup with separate identities and database permissions, continue to Isolate application identities for Cloud SQL.


Did this page help you?