From 45c611c3d407e29b6b82da48cb7b7003ba89ec0b Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Tue, 8 Oct 2019 15:54:45 -0700 Subject: [PATCH 1/2] cloud-sql (postgres): add Cloud Run support --- cloud-sql/mysql/sqlalchemy/README.md | 6 ++-- cloud-sql/postgres/sqlalchemy/.dockerignore | 4 +++ cloud-sql/postgres/sqlalchemy/.gitignore | 2 ++ cloud-sql/postgres/sqlalchemy/Dockerfile | 37 +++++++++++++++++++++ cloud-sql/postgres/sqlalchemy/README.md | 36 ++++++++++++++++++++ run/README.md | 4 ++- 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 cloud-sql/postgres/sqlalchemy/.dockerignore create mode 100644 cloud-sql/postgres/sqlalchemy/.gitignore create mode 100644 cloud-sql/postgres/sqlalchemy/Dockerfile diff --git a/cloud-sql/mysql/sqlalchemy/README.md b/cloud-sql/mysql/sqlalchemy/README.md index 4808111a09d..2e04f6a900f 100644 --- a/cloud-sql/mysql/sqlalchemy/README.md +++ b/cloud-sql/mysql/sqlalchemy/README.md @@ -78,19 +78,19 @@ gcloud app deploy ## Deploy to Cloud Run -See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql) +See the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run) for more details on connecting a Cloud Run service to Cloud SQL. 1. Build the container image: ```sh -gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql +gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql ``` 2. Deploy the service to Cloud Run: ```sh -gcloud beta run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql +gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql ``` Take note of the URL output at the end of the deployment process. diff --git a/cloud-sql/postgres/sqlalchemy/.dockerignore b/cloud-sql/postgres/sqlalchemy/.dockerignore new file mode 100644 index 00000000000..504e1d3bb7b --- /dev/null +++ b/cloud-sql/postgres/sqlalchemy/.dockerignore @@ -0,0 +1,4 @@ +Dockerfile +.dockerignore +__pycache__ +.pytest_cache \ No newline at end of file diff --git a/cloud-sql/postgres/sqlalchemy/.gitignore b/cloud-sql/postgres/sqlalchemy/.gitignore new file mode 100644 index 00000000000..f07124031ac --- /dev/null +++ b/cloud-sql/postgres/sqlalchemy/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +.pytest_cache \ No newline at end of file diff --git a/cloud-sql/postgres/sqlalchemy/Dockerfile b/cloud-sql/postgres/sqlalchemy/Dockerfile new file mode 100644 index 00000000000..bf063994883 --- /dev/null +++ b/cloud-sql/postgres/sqlalchemy/Dockerfile @@ -0,0 +1,37 @@ +# Copyright 2019 Google, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Use the official Python image. +# https://hub.docker.com/_/python +FROM python:3.7 + +# Copy application dependency manifests to the container image. +# Copying this separately prevents re-running pip install on every code change. +COPY requirements.txt ./ + +# Install production dependencies. +RUN set -ex; \ + pip install -r requirements.txt; \ + pip install gunicorn + +# Copy local code to the container image. +ENV APP_HOME /app +WORKDIR $APP_HOME +COPY . ./ + +# Run the web service on container startup. Here we use the gunicorn +# webserver, with one worker process and 8 threads. +# For environments with multiple CPU cores, increase the number of workers +# to be equal to the cores available. +CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app diff --git a/cloud-sql/postgres/sqlalchemy/README.md b/cloud-sql/postgres/sqlalchemy/README.md index f43bd634aad..6ff30b2b8af 100644 --- a/cloud-sql/postgres/sqlalchemy/README.md +++ b/cloud-sql/postgres/sqlalchemy/README.md @@ -69,5 +69,41 @@ variables into the runtime. Next, the following command will deploy the application to your Google Cloud project: ```bash gcloud app deploy +``` + +## Deploy to Cloud Run + +See the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run) +for more details on connecting a Cloud Run service to Cloud SQL. + +1. Build the container image: + +```sh +gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql +``` + +2. Deploy the service to Cloud Run: +```sh +gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql ``` + +Take note of the URL output at the end of the deployment process. + +3. Configure the service for use with Cloud Run + +```sh +gcloud beta run services update run-mysql \ + --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \ + --set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\ + DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB] +``` +Replace environment variables with the correct values for your Cloud SQL +instance configuration. + +This step can be done as part of deployment but is separated for clarity. + +4. Navigate your browser to the URL noted in step 2. + +For more details about using Cloud Run see http://cloud.run. +Review other [Python on Cloud Run samples](../../../run/). \ No newline at end of file diff --git a/run/README.md b/run/README.md index d1a326905ac..0e30b1f476e 100644 --- a/run/README.md +++ b/run/README.md @@ -13,7 +13,8 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou | ------------------------------- | ------------------------ | ------------- | |[Hello World][helloworld] ➥ | Quickstart | [Run on Google Cloud][run_button_helloworld] | |[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [Run on Google Cloud][run_button_pubsub] | -|[Cloud SQL (MySQL)[mysql] | Use MySQL with Cloud Run | - | +|[Cloud SQL (MySQL)[mysql] | Use MySQL with Cloud Run | - | +|[Cloud SQL (Postgres)[postgres] | Use Postgres with Cloud Run | - | For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples). @@ -107,6 +108,7 @@ for more information. [helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-python [pubsub]: pubsub/ [mysql]: ../cloud-sql/mysql/sqlalchemy +[postgres]: ../cloud-sql/postgres/sqlalchemy [run_button_helloworld]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/knative/docs&cloudshell_working_dir=docs/serving/samples/hello-world/helloworld-python [run_button_pubsub]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/pubsub [testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services From 6997f6076c2ce0d0d7b7525c16d1aba7d907ee8d Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Tue, 8 Oct 2019 15:55:32 -0700 Subject: [PATCH 2/2] cloud-sql (postgres): link Cloud Run steps to postgres docs --- cloud-sql/postgres/sqlalchemy/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud-sql/postgres/sqlalchemy/README.md b/cloud-sql/postgres/sqlalchemy/README.md index 6ff30b2b8af..92254c9051b 100644 --- a/cloud-sql/postgres/sqlalchemy/README.md +++ b/cloud-sql/postgres/sqlalchemy/README.md @@ -73,7 +73,7 @@ gcloud app deploy ## Deploy to Cloud Run -See the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run) +See the [Cloud Run documentation](https://cloud.google.com/sql/docs/postgres/connect-run) for more details on connecting a Cloud Run service to Cloud SQL. 1. Build the container image: