Learn how to deploy a sample app on Cloud Run connected to a SQL Server instance by using the Google Cloud console and a client application.
Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).
Before you begin Note: The name you use for your project must be between 4 and 30 characters. When you type the name, the form suggests a project ID, which you can edit. The project ID must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash.In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify 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.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Click Enable APIs to enable the APIs required for this quickstart.
This enables the following APIs:
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 following gcloud
command using Cloud Shell:
gcloud services enable compute.googleapis.com sqladmin.googleapis.com run.googleapis.com \ containerregistry.googleapis.com cloudbuild.googleapis.com servicenetworking.googleapis.com
This command enables the following APIs:
Cloud Run does not support connecting to Cloud SQL for SQL Server over public IP. Use private IP instead.
Private IP Console Allocate an IP address range and create a private connection to configure private services access for Cloud SQLIn the Google Cloud console, go to the VPC networks page.
default
VPC network.google-managed-services-default
.16
.default
VPC network.google-managed-services-default
.In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.Run the gcloud compute addresses create
command to allocate an IP address range.
gcloud compute addresses create google-managed-services-default \ --global --purpose=VPC_PEERING --prefix-length=16 \ --description="peering range for Google" --network=default
Run the gcloud services vpc-peerings connect
command to create a private connection to the allocated IP address range. Replace YOUR_PROJECT_ID with your project's project ID.
gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \ --ranges=google-managed-services-default --network=default \ --project=YOUR_PROJECT_ID
Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.
Optionally, modify the values for the following parameters:
us-central1
is used. See the full list of regions.Run the gcloud sql instances create
command to create a Cloud SQL instance with a Private IP address.
gcloud sql instances create quickstart-instance \ --database-version=SQLSERVER_2017_STANDARD \ --cpu=1 \ --memory=4GB \ --region=us-central \ --root-password=DB_ROOT_PASSWORD \ --no-assign-ip \ --network=default
Run the gcloud sql instances patch
command to enable only allow SSL connections for the instance.
gcloud sql instances patch quickstart-instance --require-ssl
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
.quickstart-db
.Run the gcloud sql databases create
command to create a database.
gcloud sql databases create quickstart-db --instance=quickstart-instanceDeploy sample app to Cloud Run Configure a Cloud Run service account
Configure the service account used by Cloud Run so that it has the
Cloud SQL Clientrole with permissions to connect to Cloud SQL.
ConsoleIn the Google Cloud console, go to the IAM page.
gcloud
command to get a list of your project's service accounts:
gcloud iam service-accounts list
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \ --role="roles/cloudsql.client"
With a Cloud SQL instance, database, and service account with client permissions, you can now configure a sample application to connect to your Cloud SQL instance.
Public IP Cloud Run does not support connecting to Cloud SQL for SQL Server over public IP. Use private IP instead. Private IP For private IP paths, your application connects directly to your instance through Serverless VPC Access. This method uses a TCP socket to connect directly to the Cloud SQL instance without using the Cloud SQL Auth Proxy. Go Create and download SSL server certificateIn the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
to see its Overview page.In the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input box.In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editorserver-ca.pem
.gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
For Java users, the connector already provides a secure connection so that creating and downloading the SSL server certificate is unnecessary.
Create a serverless VPC connection for connections to the instance using private IPIn the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input box.In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editormvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build \ -Dimage=gcr.io/YOUR_PROJECT_ID/run-sql -DskipTests
In the Google Cloud console, go to the Cloud SQL Instances page.
quickstart-instance
to see its Overview page.In the Google Cloud console, go to the Serverless VPC access - Create connector page.
quickstart-connector
for the Name.10.8.0.0
in the IP range input box.In Cloud Shell Editor, open the sample app's source code.
Open Cloud Shell Editorserver-ca.pem
.gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
The steps to deploy the sample to Cloud Run depend on the type of IP address you assigned to your Cloud SQL instance.
The image connection method varies based on which environment variables are set.
INSTANCE_HOST
environment variable. This connection method honors the certificates and ports configured as environment variables.INSTANCE_UNIX_SOCKET
environment variable.
Don't set the value for the INSTANCE_HOST
environment variable.
Unix sockets aren't natively supported in Java. If your application is written using Java, you must use the Cloud SQL Java Connector.
INSTANCE_CONNECTION_NAME
environment variable.
Don't set the values for the following environment variables:
INSTANCE_HOST
INSTANCE_UNIX_SOCKET
DB_PORT
DB_ROOT_CERT
DB_CERT
DB_KEY
3307
.In the Google Cloud console, go to the Cloud Run page.
Click Create container and select Service to display the Create service form.
gcr.io/YOUR_PROJECT_ID/run-sql
container image you created in the previous step.quickstart-service
for the Service name.Allow unauthenticated invocations
option. If you don't have permissions (Cloud Run Admin role) to select this, the service will deploy and require authentication.DB_NAME
: Set to quickstart-db
.DB_USER
: Set to sqlserver
.DB_PASS
: Set to the password of the sqlserver
user that you created in Create a Cloud SQL instance.INSTANCE_CONNECTION_NAME
: Set to your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.DB_PORT
: Set to 1433
.DB_ROOT_CERT
: Set to certs/server-ca.pem
.PRIVATE_IP
: Set to TRUE
.Enable connecting to Cloud SQL:
quickstart-instance
Cloud SQL instance that you previously created.default: Serverless VPC Access Connector "quickstart-connector"
from the VPC Network drop-down menu.Click Create to finish creating the Cloud Run service.
gcloud run deploy
, and have the required roles granted to you.
Before running the following command, make the following replacements:
sqlserver
user that you created in Create a Cloud SQL instance.Run the gcloud run deploy
command as follows to create the Cloud Run service. Environment variables vary depending on the connection method you want to use:
gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \ --add-cloudsql-instances INSTANCE_CONNECTION_NAME \ --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \ --set-env-vars DB_NAME="quickstart-db" \ --set-env-vars DB_USER="sqlserver" \ --set-env-vars DB_PASS="DB_PASS" \ --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \ --add-cloudsql-instances INSTANCE_CONNECTION_NAME \ --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \ --set-env-vars DB_NAME="quickstart-db" \ --set-env-vars DB_USER="sqlserver" \ --set-env-vars DB_PASS="DB_PASS" \ --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
Enter the numeric choice provided for us-central1
when prompted to specify a region.
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 Cloud Run page.
quickstart-service
service name.If you're not using the Cloud SQL client role that you assigned to the Compute Engine service account, you can remove it.
In the Google Cloud console, go to the IAM page.
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 SQL Server users and databases for your Cloud SQL instance.
For more information about pricing, see Cloud SQL for SQL Server pricing.
Learn more about:
Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4