2

For a sample project I am working on (https://gitlab.com/connorbutch/reading-comprehension-ws), I am having issues connecting to a google cloud mysql database from google cloud run. However, when I run locally with the same args (in both docker and kubernetes) the application looks to succeed.

The steps I followed in setting up my google cloud run application are listed here (https://cloud.google.com/sql/docs/mysql/connect-run). I included the mysql db in the cloud database info. Things I have tried

connecting using ip address in jdbc connection string (which works locally, but this statement on the page suggests it might not on google cloud run, "Cloud Run (fully managed) does not support connecting to the Cloud SQL instance using TCP. Your code should not try to access the instance using an IP address such as 127.0.0.1 or 172.17.0.1.") connecting using unix socket as suggested, server does not even start

When I start the application with the ip address in the jdbc url, on google cloud, it looks like the app starts successfully:

2020-02-12T02:51:01.733606Z 2020-02-12 02:51:01.733  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '' 
2020-02-12T02:51:01.740162Z 2020-02-12 02:51:01.739  INFO 1 --- [           main] com.connor.Application                   : Started Application in 15.717 seconds (JVM running for 17.715) 

However, when I make the first request, I see the following:

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
Caused by: java.net.SocketTimeoutException: connect timed out
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

Is there any suggestions you may have? I am wondering if it may be related to having to configure our DataSource as listed here: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/cloud-sql/mysql/servlet/src/main/java/com/example/cloudsql/ConnectionPoolContextListener.java

Connor Butch
  • 648
  • 1
  • 10
  • 28

2 Answers2

2

Following the documentation steps:

  1. Build and deploy container
  2. Connect from Cloud Run

On the last step: “Connecting to CloudSQL” of the 2nd link instead of using the code snippet, I used the following command from the GitHub instructions:

 gcloud run services update helloworld --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]

where "helloworld" is the name of my service.

Please be meticulous with the spacing on this command, as it can easily throw errors.

Having said that after performing a curl command I did not receive any errors, so my application runs successfully.

Additionally, during my investigation on the error you received, I came across this link, which contains a list of possible causes for this error.

Finally, since the error indicates a timeout, you could also try modifying the request timeout of Cloud Run by following this.

Some further links that could come in handy for you are:

  1. Diagnosing issues with Cloud SQL instances
  2. Troubleshooting Cloud Run (fully managed)

I hope this information helps.

0

Sadly the https://cloud.google.com/sql/docs/mysql/connect-run document is currently not documenting the Knative instructions. Any time you see "Cloud Run (fully managed)" that's not to Kubernetes implementation.

If you are using Cloud Run on a Kubernetes/GKE cluster, this is probably more applicable. https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine That said this approach uses "Cloud SQL Proxy" sidecar container, which is not yet a notion supported by Knative today.

However, using those instructions, you can connect to Cloud SQL instance over a private IP, as GKE clusters can be in a VPC (though "Cloud Run fully-managed" applications currently cannot).

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214