Cloud SQL for Postgres
Runway supports connecting to unmanaged Cloud SQL for Postgres instances from your GKE workloads. This allows you to connect workloads to an existing database in a different project.
Prerequisites
Section titled “Prerequisites”Before configuring your GKE workload, complete the shared setup steps in the Cloud Run CloudSQL documentation:
- Enable PSC on your Cloud SQL instance
- Obtain PSC details (DNS name, service attachment link, connection name)
- Update the provisioner - use
config/runtimes/gke/cloud-sql/unmanaged.ymlinstead of the cloud-run path
Grant role to SA
Section titled “Grant role to SA”In the project where your Cloud SQL instance lives, grant the Cloud SQL Client role to your GKE workload’s service account.
Your workload’s service account will have the following format:
gke-<Workload ID>@<Runway Project ID>.iam.gserviceaccount.comFor example:
gke-my-service-gke@gitlab-runway-staging.iam.gserviceaccount.comUsing the gcloud CLI:
gcloud projects add-iam-policy-binding <DB Project ID> \ --member=serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com \ --role=roles/cloudsql.client \ --condition="expression=resource.name == 'projects/<DB Project ID>/instances/<DB Instance ID>',title=access_specific_db"Terraform example:
resource "google_project_iam_member" "runway-workload-cloudsql-client" { project = var.project role = "roles/cloudsql.client" member = "serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com"
condition { title = "Cloud SQL client access for Runway workload" expression = "resource.name == \"projects/${var.project}/instances/<DB instance ID>\"" }}Update your deployment file
Section titled “Update your deployment file”Declare one named binding per database in your Fairway manifest, and one entry in the spec.infrastructure.cloudsqlInstances map of your deployment.yaml, keyed by the binding’s name.
This works the same as for managed instances, including the deploy-time checks and the deprecated cloudsqlInstance key.
Only instanceConnectionName differs:
spec: infrastructure: cloudsqlInstances: users: instanceConnectionName: project-a:us-east1:db-users-{% .Environment %} databaseName: users orders: instanceConnectionName: project-b:us-east1:db-orders-{% .Environment %} databaseName: ordersFor an unmanaged instance, instanceConnectionName is <project>:<region>:<instance>.
The <instance> part must contain the {% .Environment %} template, so that staging and production connect to different instances.
Runway replaces it with staging or production at deploy time.
Add cloudsql_instances to your gke-service.yaml:
apiVersion: runway/v1kind: RunwayKubernetesDeployment
your-service-gke: serviceMixins: - "runway-gke.service-mixin"
cloudsql_instances: - instance_connection_name: <project>:<region>:<instance>For multiple instances:
cloudsql_instances: - instance_connection_name: project-a:us-east1:db-users - instance_connection_name: project-b:us-east1:db-ordersConnecting to Cloud SQL
Section titled “Connecting to Cloud SQL”Runway runs one Cloud SQL Auth Proxy sidecar per instance and writes the connection settings into your Helm values, as described for managed instances.
The proxy authenticates with IAM database authentication as your workload’s service account, so there is no password.
Neither the provisioner nor Runway sets this up for an unmanaged instance.
In addition to the Cloud SQL Client role granted above, do the following in the project where your Cloud SQL instance lives.
Grant the Cloud SQL Instance User role to your workload’s service account, which lets it log in to the database with IAM:
gcloud projects add-iam-policy-binding <DB Project ID> \ --member=serviceAccount:gke-<Runway workload ID>@<Runway Project ID>.iam.gserviceaccount.com \ --role=roles/cloudsql.instanceUser \ --condition="expression=resource.name == 'projects/<DB Project ID>/instances/<DB Instance ID>',title=access_specific_db_iam_login"In Terraform, add a second google_project_iam_member like the one above, with role = "roles/cloudsql.instanceUser".
Enable IAM authentication on the instance by setting the cloudsql.iam_authentication database flag to on:
resource "google_sql_database_instance" "db" { # ... settings { database_flags { name = "cloudsql.iam_authentication" value = "on" } }}Create the IAM database user for your workload’s service account:
gcloud sql users create gke-<Runway workload ID>@<Runway Project ID>.iam \ --instance=<DB Instance ID> \ --type=cloud_iam_service_account \ --project=<DB Project ID>The user name is the service account’s email address without the .gserviceaccount.com suffix, and it is the username Runway configures for your application.
Then, connected as a database administrator, grant the user the privileges your application needs on your database’s schemas and tables.
In SQL, quote the user name, because it contains @ and .: "gke-<Runway workload ID>@<Runway Project ID>.iam".
The Cloud SQL Auth Proxy exposes databases on localhost with sequential ports starting at 5432:
| Instance | Port |
|---|---|
| First | 5432 |
| Second | 5433 |
| Third | 5434 |
Connect using:
- Host:
127.0.0.1 - Port:
5432(first instance),5433(second), etc. - Username and password via environment variables (see secrets management)
Troubleshooting
Section titled “Troubleshooting”Connection refused
Section titled “Connection refused”- Verify
instanceConnectionName(v2) orinstance_connection_name(v1) is correct - Check proxy sidecar logs from grafana (see logging):
On Runway v2, the sidecar of a named instance is calledSELECTTimestampTime as "timestamp",Body as "body",SeverityText as "level",Attributes as "labels"FROM"observability"."otel_logs"WHERE(timestamp >= $__fromTimeAND timestamp <= $__toTime)AND (labels['service.name'] = '{runway-service-id}-{staging|production}')AND (labels['k8s.container.name'] = 'cloud-sql-proxy')ORDER BYtimestamp DESCLIMIT1000
cloud-sql-proxy-<name>, so match it withlabels['k8s.container.name'] LIKE 'cloud-sql-proxy%'instead. - Ensure PSC endpoint is configured in provisioner