0

I'm deploying my Java8 SpringBoot App to a Google Compute Engine instance and trying to connect it to a Debian9 CloudSQL instance. I'm trying to get the instance to run with my startup-script.sh, but when it tries to boot up the SpringBoot Application, according to the daemon.log, when the .war is ran by "java -jar order-routing-0.0.1-SNAPSHOT.war" the startup fails with a "Unable to obtain connection from database: Communications link failure", with SQL State:08S01 and error code 0.

I mapped the GCE instance to a static external IP, as well as whitelisting that IP on the CloudSQL instance's connections configs. I also verify that the war file runs locally with "java -jar order-routing.war".

Here is my startup script.sh:

#!/usr/bin/env bash
# This script is passed to the GCE instance by the setup script. It is run on the instance when it is spun up.
# Derived from GCE Tutorial at https://cloud.google.com/java/docs/tutorials/bookshelf-on-compute-engine

# [START script]
set -e
set -v

# Talk to the metadata server to get the project id
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
BUCKET=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/BUCKET" -H "Metadata-Flavor: Google")

echo "Project ID: ${PROJECTID}"

# get our file(s)
gsutil cp "gs://order-routing-install/gce/"** .

# Install dependencies from apt
apt-get update
apt-get install mysql-client -y

wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
mv cloud_sql_proxy.linux.amd64 cloud_sql_proxy
chmod +x cloud_sql_proxy
./cloud_sql_proxy -instances=instance-qa1:us-central1:instance-qa1-cloudsql-0=tcp:3307 &

apt-get install -yq default-jre
apt-get install -yq default-jdk

java -jar order-routing-0.0.1-SNAPSHOT.war

# [END script]

Here is the failing error log from the daemon.log:

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException:
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: Unable to obtain connection from database: Communications link failure
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: 2019-07-29 03:39:30.435  INFO 9051 --- [           main] ConditionEvaluationReportLoggingListener :
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script:n from database: Communications link failure
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: SQL State  : 08S01
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: Error Code : 0
Jul 29 03:39:30 order-routing-group-hk66 startup-script: INFO startup-script: Message    : Communications link failure

Expected Result: The GCE instance starts up the war file successfully and I can access my app using the external IP.

Actual Result: Getting CommunicationsLinkFailures upon starting up Spring Boot Java app.

P_equals_NP_2021
  • 627
  • 1
  • 9
  • 27
  • What is the setting in the Google Cloud Console -> Compute Engine -> Identity and API Access? You need to enable Cloud SQL. The default is disabled. – John Hanley Jul 29 '19 at 05:54
  • @JohnHanley I don't see that particular option but in CE -> VM Instances I do see "Cloud API access scopes: Allow full access to all Cloud APIs" – P_equals_NP_2021 Jul 29 '19 at 06:05
  • Yes that is the one. Are you using the default service account or did you create a new service account? – John Hanley Jul 29 '19 at 06:18
  • Also, did you whitelist your Compute Engine public IP address in Cloud SQL or are you using Cloud SQL Proxy? – John Hanley Jul 29 '19 at 06:20
  • @JohnHanley I mapped the instance to a static external IP and whitelisted it in the CloudSQL instance and I am also using a proxy on top of that as my script above using ./cloud_sql_proxy. – P_equals_NP_2021 Jul 29 '19 at 06:22
  • @JohnHanley The service accounts in the CloudSQL instance and the CE instance don't match per se, but one has sql in the name and one has compute in the name. – P_equals_NP_2021 Jul 29 '19 at 06:24
  • If you are using the Cloud SQL Proxy, then remove the IP address from Cloud SQL Whitelist. However, the error message indicates that Cloud SQL is ignoring your connection attempt. Since you are using Cloud SQL Proxy you probably have the instance connection name wrong. – John Hanley Jul 29 '19 at 06:24
  • For the service account you must have enabled one of the Cloud SQL Roles otherwise you will be blocked. – John Hanley Jul 29 '19 at 06:25
  • @JohnHanley By CloudSQL roles do you mean the MySQL user accounts list of the CloudSQL instance? – P_equals_NP_2021 Jul 29 '19 at 06:31
  • No, Google Cloud IAM Roles such as `Cloud SQL Admin` – John Hanley Jul 29 '19 at 06:48
  • @JohnHanley Wow that seems promising but I lack the permissions to edit them. The instance name for the proxy seems correct, so the IAM roles sounds like it might be it. Will check with data guy tomorrow. Thanks – P_equals_NP_2021 Jul 29 '19 at 06:51

0 Answers0