Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor. Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor. Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor. Enable the GKE cluster you just created as the default cluster to be used for the remaining commands in this quickstart.
gcloud container clusters get-credentials
command as follows to enable the GKE cluster. gcloudcontainerclustersget-credentialsgke-cloud-sql-quickstart\--regionus-central1
gcloud iam service-accounts create
command as follows to create a new service account: gcloudiamservice-accountscreategke-quickstart-service-account\--display-name="GKE Quickstart Service Account"
gcloud projects add-iam-policy-binding
command as follows to add the Cloud SQL Client role to the Google Cloud service account you just created. Replace YOUR_PROJECT_ID with the project ID. gcloudprojectsadd-iam-policy-bindingYOUR_PROJECT_ID\--member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com"\--role="roles/cloudsql.client"
gcloud projects add-iam-policy-binding
command as follows to add the Log Writer role to the Google Cloud service account you just created. Replace YOUR_PROJECT_ID with the project ID. gcloudprojectsadd-iam-policy-bindingYOUR_PROJECT_ID\--member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com"\--role="roles/logging.logWriter"
gcloud projects add-iam-policy-binding
command as follows to add the Artifact Registry Reader role to the service account. Replace YOUR_PROJECT_ID with the project ID. gcloudprojectsadd-iam-policy-bindingYOUR_PROJECT_ID\--member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com"\--role="roles/artifactregistry.reader"
service-account.yaml
file in Cloud Shell Editor. Replace <YOUR-KSA-NAME>
with ksa-cloud-sql
.kubectl apply
command as follows in Cloud Shell: kubectlapply-fservice-account.yaml
gcloud iam service-accounts add-iam-policy-binding
command as follows to enable IAM binding of the Google Cloud Service Account and the Kubernetes Service Account. Make the following replacements: default
, which is the default namespace for clusters created in GKE.ksa-cloud-sql
.gcloudiamservice-accountsadd-iam-policy-binding\--role="roles/iam.workloadIdentityUser"\--member="serviceAccount:YOUR_PROJECT_ID.svc.id.goog[YOUR_K8S_NAMESPACE/YOUR_KSA_NAME]"\gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com
kubectl annotate
command as follows to annotate the Kubernetes Service Account with IAM binding. Make the following replacements: ksa-cloud-sql
.kubectlannotateserviceaccount\YOUR_KSA_NAME\iam.gke.io/gcp-service-account=gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com
Run the kubectl create secret generic
command as follows to create Kubernetes secrets for the database, user, and user password to be used by the sample app. The values of each secret are based on the values specified in the previous steps of this quickstart. Replace DB_PASS with the password of the quickstart-user
that you created in the previous Create a user quickstart step.
kubectlcreatesecretgenericgke-cloud-sql-secrets\--from-literal=database=quickstart-db\--from-literal=username=quickstart-user\--from-literal=password=DB_PASS
gcloud artifacts repositories create
command in Cloud Shell to create a repository in the Artifact Registry named gke-cloud-sql-repo in the same region as your cluster. Replace YOUR_PROJECT_ID with the project ID. gcloudartifactsrepositoriescreategke-cloud-sql-repo\--project=YOUR_PROJECT_ID\--repository-format=docker\--location=us-central1\--description="GKE Quickstart sample app"
gcloud builds submit
command as follows in Cloud Shell to build a Docker container and publish it to Artifact Registry. Replace YOUR_PROJECT_ID with the project ID. gcloudbuildssubmit\--tagus-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql.
gcloud artifacts repositories create
command in Cloud Shell to create a repository in the Artifact Registry named gke-cloud-sql-repo in the same region as your cluster. Replace YOUR_PROJECT_ID with the project ID. gcloudartifactsrepositoriescreategke-cloud-sql-repo\--project=YOUR_PROJECT_ID\--repository-format=docker\--location=us-central1\--description="GKE Quickstart sample app"
mvn
command as follows in Cloud Shell to build a Docker container and publish it to Artifact Registry. Replace YOUR_PROJECT_ID with the project ID. mvncleanpackagecom.google.cloud.tools:jib-maven-plugin:2.8.0:build\-Dimage=us-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql\-DskipTests-Djib.to.credHelper=gcloud
gcloud artifacts repositories create
command in Cloud Shell to create a repository in the Artifact Registry named gke-cloud-sql-repo in the same region as your cluster. Replace YOUR_PROJECT_ID with the project ID. gcloudartifactsrepositoriescreategke-cloud-sql-repo\--project=YOUR_PROJECT_ID\--repository-format=docker\--location=us-central1\--description="GKE Quickstart sample app"
gcloud builds submit
command as follows in Cloud Shell to build a Docker container and publish it to Artifact Registry. Replace YOUR_PROJECT_ID with the project ID. gcloudbuildssubmit\--tagus-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql.
gcloud artifacts repositories create
command in Cloud Shell to create a repository in the Artifact Registry named gke-cloud-sql-repo in the same region as your cluster. Replace YOUR_PROJECT_ID with the project ID. gcloudartifactsrepositoriescreategke-cloud-sql-repo\--project=YOUR_PROJECT_ID\--repository-format=docker\--location=us-central1\--description="GKE Quickstart sample app"
gcloud builds submit
command as follows in Cloud Shell to build a Docker container and publish it to Artifact Registry. Replace YOUR_PROJECT_ID with the project ID. gcloudbuildssubmit\--tagus-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql.
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
instance to open the Instance details page.quickstart-instance
, and then click Delete to delete the instance.In the Google Cloud console, go to the Google Kubernetes Engine page.
gke-cloud-sql-quickstart
service name.If you're not using the Google Cloud service account you created for this quickstart, you can remove it.
In the Google Cloud console, go to the IAM page.
gke-quickstart-service-account
.If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
In the Google Cloud console, go to the APIs page.
Select any API that you would like to disable and then click the Disable API button.
You also can learn about creating MySQL users and databases for your Cloud SQL instance.
Also see the Cloud SQL pricing information.
Learn more about:
Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-17 UTC.