Skip to content

Commit bd939ef

Browse files
authored
Cloud SQL (Postgres): Add Cloud Run support (GoogleCloudPlatform#2460)
* cloud-sql (postgres): add Cloud Run support * cloud-sql (postgres): link Cloud Run steps to postgres docs
1 parent cbb910e commit bd939ef

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

cloud-sql/mysql/sqlalchemy/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ gcloud app deploy
7878

7979
## Deploy to Cloud Run
8080

81-
See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql)
81+
See the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run)
8282
for more details on connecting a Cloud Run service to Cloud SQL.
8383

8484
1. Build the container image:
8585

8686
```sh
87-
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql
87+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
8888
```
8989

9090
2. Deploy the service to Cloud Run:
9191

9292
```sh
93-
gcloud beta run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
93+
gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql
9494
```
9595

9696
Take note of the URL output at the end of the deployment process.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Dockerfile
2+
.dockerignore
3+
__pycache__
4+
.pytest_cache
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__
2+
.pytest_cache
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2019 Google, LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use the official Python image.
16+
# https://hub.docker.com/_/python
17+
FROM python:3.7
18+
19+
# Copy application dependency manifests to the container image.
20+
# Copying this separately prevents re-running pip install on every code change.
21+
COPY requirements.txt ./
22+
23+
# Install production dependencies.
24+
RUN set -ex; \
25+
pip install -r requirements.txt; \
26+
pip install gunicorn
27+
28+
# Copy local code to the container image.
29+
ENV APP_HOME /app
30+
WORKDIR $APP_HOME
31+
COPY . ./
32+
33+
# Run the web service on container startup. Here we use the gunicorn
34+
# webserver, with one worker process and 8 threads.
35+
# For environments with multiple CPU cores, increase the number of workers
36+
# to be equal to the cores available.
37+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app

cloud-sql/postgres/sqlalchemy/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,41 @@ variables into the runtime.
6969
Next, the following command will deploy the application to your Google Cloud project:
7070
```bash
7171
gcloud app deploy
72+
```
73+
74+
## Deploy to Cloud Run
75+
76+
See the [Cloud Run documentation](https://cloud.google.com/sql/docs/postgres/connect-run)
77+
for more details on connecting a Cloud Run service to Cloud SQL.
78+
79+
1. Build the container image:
80+
81+
```sh
82+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-sql
83+
```
84+
85+
2. Deploy the service to Cloud Run:
7286

87+
```sh
88+
gcloud beta run deploy run-sql --image gcr.io/[YOUR_PROJECT_ID]/run-sql
7389
```
90+
91+
Take note of the URL output at the end of the deployment process.
92+
93+
3. Configure the service for use with Cloud Run
94+
95+
```sh
96+
gcloud beta run services update run-mysql \
97+
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
98+
--set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
99+
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
100+
```
101+
Replace environment variables with the correct values for your Cloud SQL
102+
instance configuration.
103+
104+
This step can be done as part of deployment but is separated for clarity.
105+
106+
4. Navigate your browser to the URL noted in step 2.
107+
108+
For more details about using Cloud Run see http://cloud.run.
109+
Review other [Python on Cloud Run samples](../../../run/).

run/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1313
| ------------------------------- | ------------------------ | ------------- |
1414
|[Hello World][helloworld]&nbsp;&#10149; | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] |
1515
|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
16-
|[Cloud SQL (MySQL)[mysql] | Use MySQL with Cloud Run | - |
16+
|[Cloud SQL (MySQL)[mysql] | Use MySQL with Cloud Run | - |
17+
|[Cloud SQL (Postgres)[postgres] | Use Postgres with Cloud Run | - |
1718

1819
For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
1920

@@ -107,6 +108,7 @@ for more information.
107108
[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-python
108109
[pubsub]: pubsub/
109110
[mysql]: ../cloud-sql/mysql/sqlalchemy
111+
[postgres]: ../cloud-sql/postgres/sqlalchemy
110112
[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
111113
[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
112114
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

0 commit comments

Comments
 (0)