Connect to Cloud SQL for SQL Server from Google Kubernetes Engine
This page shows you how to deploy a sample app on Google Kubernetes Engine (GKE) connected to a SQL Server instance using the Google Cloud console and a client application. The resources created in this quickstart typically cost less than one dollar (USD), assuming you complete the steps, including the clean up, in a timely manner.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Google Cloud APIs necessary to run a Cloud SQL sample app on GKE.
Console
Click Enable APIs to enable the APIs required for this quickstart.
This enables the following APIs:
- Compute Engine API
- Cloud SQL Admin API
- Google Kubernetes Engine API
- Artifact Registry API
- Cloud Build API
gcloud
Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the
gcloud
commands presented throughout this quickstart.Run the
gcloud services enable
command as follows using Cloud Shell to enable the APIs required for this quickstart.:gcloud services enable compute.googleapis.com sqladmin.googleapis.com \ container.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com
This command enables the following APIs:
- Compute Engine API
- Cloud SQL Admin API
- GKE API
- Artifact Registry API
- Cloud Build API
Set up Cloud SQL
Create a Cloud SQL instance
Create a database
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select
quickstart-instance
. - From the SQL navigation menu, select Databases.
- Click Create database.
- In the Database name field of the New database dialog
box, enter
quickstart-db
. - Click Create.
gcloud
Run the gcloud
sql databases create
command to create a database.
gcloud sql databases create quickstart-db --instance=quickstart-instance
Create a GKE cluster
Console
-
In the Google Cloud console, go to the Google Kubernetes Engine page.
- Click Create.
- Click Configure for GKE Autopilot.
- For Name, specify the cluster name as
gke-cloud-sql-quickstart
. - Click Create.
gcloud
Run the gcloud container clusters create-auto
command to create the cluster.
gcloud container clusters create-auto gke-cloud-sql-quickstart \ --region us-central1
Clone a Cloud SQL sample app into Cloud Shell Editor
With a Cloud SQL instance, a database, and a GKE cluster,
you can now clone and configure a sample application to connect to your
Cloud SQL instance. The remaining steps in this quickstart require using the gcloud
and kubectl
command-line tools. Both tools are pre-installed in Cloud Shell.
Go
-
In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editor -
In the
Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
Java
-
In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editor -
In the
Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
Node.js
-
In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editor -
In the
Open in Cloud Shell
dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
Python
-
In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editor -
In the
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
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.
gcloud container clusters get-credentials gke-cloud-sql-quickstart \ --region us-central1
Set up a service account
-
Run the
gcloud iam service-accounts create
command as follows to create a new service account:gcloud iam service-accounts create gke-quickstart-service-account \ --display-name="GKE Quickstart Service Account"
- Run the
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.gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/cloudsql.client"
- The sample app uses logging, so run the
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.gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/logging.logWriter"
- The service account must be able to pull images from the artifactory repository, so run the
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.gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/artifactregistry.reader"
- Create a Kubernetes Service Account.
- Update the
service-account.yaml
file in Cloud Shell Editor. Replace<YOUR-KSA-NAME>
withksa-cloud-sql
. - Run the
kubectl apply
command as follows in Cloud Shell:kubectl apply -f service-account.yaml
- Update the
- Run the
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:- YOUR_PROJECT_ID with the project ID.
- YOUR_K8S_NAMESPACE with
default
, which is the default namespace for clusters created in GKE. - YOUR_KSA_NAME with
ksa-cloud-sql
.
gcloud iam service-accounts add-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
- Run the
kubectl annotate
command as follows to annotate the Kubernetes Service Account with IAM binding. Make the following replacements:- YOUR_KSA_NAME with
ksa-cloud-sql
. - YOUR_PROJECT_ID with the project ID.
kubectl annotate serviceaccount \ YOUR_KSA_NAME \ iam.gke.io/gcp-service-account=gke-quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com
- YOUR_KSA_NAME with
Configure secrets
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 sqlserver
root user that was created in the Create a Cloud SQL instance step.
kubectl create secret generic gke-cloud-sql-secrets \ --from-literal=database=quickstart-db \ --from-literal=username=sqlserver \ --from-literal=password=DB_PASS
Build the sample app
Go
-
Run the following
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.gcloud artifacts repositories create gke-cloud-sql-repo \ --project=YOUR_PROJECT_ID \ --repository-format=docker \ --location=us-central1 \ --description="GKE Quickstart sample app"
-
Run the
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.gcloud builds submit \ --tag us-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql .
Java
-
Run the following
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.gcloud artifacts repositories create gke-cloud-sql-repo \ --project=YOUR_PROJECT_ID \ --repository-format=docker \ --location=us-central1 \ --description="GKE Quickstart sample app"
-
Run the
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.mvn clean package com.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
Node.js
-
Run the following
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.gcloud artifacts repositories create gke-cloud-sql-repo \ --project=YOUR_PROJECT_ID \ --repository-format=docker \ --location=us-central1 \ --description="GKE Quickstart sample app"
-
Run the
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.gcloud builds submit \ --tag us-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql .
Python
-
Run the following
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.gcloud artifacts repositories create gke-cloud-sql-repo \ --project=YOUR_PROJECT_ID \ --repository-format=docker \ --location=us-central1 \ --description="GKE Quickstart sample app"
-
Run the
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.gcloud builds submit \ --tag us-central1-docker.pkg.dev/YOUR_PROJECT_ID/gke-cloud-sql-repo/gke-sql .
Deploy the sample app
Clean up
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.
- Select the
quickstart-instance
instance to open the Instance details page. - In the icon bar at the top of the page, click Delete.
- In the Delete instance dialog box, type
quickstart-instance
, and then click Delete to delete the instance. -
In the Google Cloud console, go to the Google Kubernetes Engine page.
- Click the checkbox next to the
gke-cloud-sql-quickstart
service name. - Click the Delete button at the top of the Google Kubernetes Engine page.
Optional cleanup steps
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.
- Select the checkbox for the IAM account named
gke-quickstart-service-account
. - Click Remove and confirm the removal.
If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
- APIs that were enabled within this quickstart:
- Compute Engine API
- Cloud SQL Admin API
- Google Kubernetes Engine API
- Artifact Registry API
- Cloud Build API
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.
What's next
Based on your needs, you can learn more about creating Cloud SQL instances.You also can learn about creating SQL Server users and databases for your Cloud SQL instance.
Also see the Cloud SQL pricing information.
Learn more about:
- All of the connectivity options in Cloud SQL.
- Configuring your Cloud SQL instance with a public IP address.
- Configuring your Cloud SQL instance with a private IP address.
Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications: