From 16eac3deb749047e616be67a594ea77a235a2487 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Mon, 7 Oct 2019 13:40:54 -0700 Subject: [PATCH] cloud-sql: add Cloud Run support to sqlalchemy sample --- cloud-sql/mysql/sqlalchemy/.dockerignore | 4 +++ cloud-sql/mysql/sqlalchemy/.gitignore | 2 ++ cloud-sql/mysql/sqlalchemy/Dockerfile | 37 ++++++++++++++++++++++++ cloud-sql/mysql/sqlalchemy/README.md | 37 ++++++++++++++++++++++++ run/README.md | 2 ++ run/pubsub/Dockerfile | 6 ++-- 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 cloud-sql/mysql/sqlalchemy/.dockerignore create mode 100644 cloud-sql/mysql/sqlalchemy/.gitignore create mode 100644 cloud-sql/mysql/sqlalchemy/Dockerfile diff --git a/cloud-sql/mysql/sqlalchemy/.dockerignore b/cloud-sql/mysql/sqlalchemy/.dockerignore new file mode 100644 index 00000000000..504e1d3bb7b --- /dev/null +++ b/cloud-sql/mysql/sqlalchemy/.dockerignore @@ -0,0 +1,4 @@ +Dockerfile +.dockerignore +__pycache__ +.pytest_cache \ No newline at end of file diff --git a/cloud-sql/mysql/sqlalchemy/.gitignore b/cloud-sql/mysql/sqlalchemy/.gitignore new file mode 100644 index 00000000000..f07124031ac --- /dev/null +++ b/cloud-sql/mysql/sqlalchemy/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +.pytest_cache \ No newline at end of file diff --git a/cloud-sql/mysql/sqlalchemy/Dockerfile b/cloud-sql/mysql/sqlalchemy/Dockerfile new file mode 100644 index 00000000000..bf063994883 --- /dev/null +++ b/cloud-sql/mysql/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/mysql/sqlalchemy/README.md b/cloud-sql/mysql/sqlalchemy/README.md index e743218ef88..4808111a09d 100644 --- a/cloud-sql/mysql/sqlalchemy/README.md +++ b/cloud-sql/mysql/sqlalchemy/README.md @@ -75,3 +75,40 @@ Next, the following command will deploy the application to your Google Cloud pro ```bash gcloud app deploy ``` + +## Deploy to Cloud Run + +See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql) +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 +``` + +2. Deploy the service to Cloud Run: + +```sh +gcloud beta run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql +``` + +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 100421083bd..d1a326905ac 100644 --- a/run/README.md +++ b/run/README.md @@ -13,6 +13,7 @@ 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 | - | For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples). @@ -105,6 +106,7 @@ for more information. [run_deploy]: https://cloud.google.com/run/docs/deploying [helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-python [pubsub]: pubsub/ +[mysql]: ../cloud-sql/mysql/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 diff --git a/run/pubsub/Dockerfile b/run/pubsub/Dockerfile index 7dab58fb0b8..d8928d1bed3 100644 --- a/run/pubsub/Dockerfile +++ b/run/pubsub/Dockerfile @@ -28,10 +28,10 @@ RUN pip install -r requirements.txt # Copy local code to the container image. ENV APP_HOME /app WORKDIR $APP_HOME -COPY . . +COPY . ./ -# Run the web service on container startup. Here we use the gunicorn -# webserver, with one worker process and 8 threads. +# Run the web service on container startup. +# Use 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