将Spring应用程序与Google Cloud结合使用的Maven Project与kubernetes一起部署SQL

时间:2018-09-19 15:55:45

标签: google-kubernetes-engine

我已经使用带有Google云的Spring应用程序和kubernetes的Sql部署创建了一个Maven项目。我使用以下步骤。

./mvnw -DskipTests spring-boot:run

./mvnw -DskipTests package

docker build -t gcr.io/ProjectID/app:v1 .

docker run -ti --rm -p 8080:8080 gcr.io/ProjectID/app:v1 

此docker运行的应用程序在Google Cloud Web预览中成功运行后。

gcloud docker -- push gcr.io/ProjectID/app:v1

https://console.cloud.google.com/project/ProjectID/storage/browser/

kubectl run app \
  --image=gcr.io/ProjectID/app:v1 \
  --port=8080

kubectl expose deployment app --type=LoadBalancer

kubectl get services

此后,我得到一个外部IP:http://EXTERNAL_IP:8080/

我正在使用以下代码:

 Class.forName("com.mysql.cj.jdbc.Driver");

   String jdbcUrl = String.format(
        "jdbc:mysql://IPAddress:3306/%s?cloudSqlInstance=%s&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false", databaseName,instanceConnectionName);

  Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

我遇到以下错误:

Start Program
Start To Connect SQL Server
2018-09-19 09:07:33.075  INFO 1 --- [nio-8080-exec-1] c.google.cloud.sql.mysql.SocketFactory   : Connecting to Cloud SQL instance [winter-dynamics-213603:us-central1:visacare].
2018-09-19 09:07:33.086  INFO 1 --- [nio-8080-exec-1] c.g.cloud.sql.mysql.SslSocketFactory     : First Cloud SQL connection, generating RSA key pair.
2018-09-19 09:07:34.344  INFO 1 --- [nio-8080-exec-1] c.g.cloud.sql.mysql.SslSocketFactory     : Obtaining ephemeral certificate for Cloud SQL instance [winter-dynamics-213603:us-central1:visacare].
2018-09-19 09:07:34.906 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server.] with root cause

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
2018-09-19 09:07:33.075  INFO 1 --- [nio-8080-exec-1] c.google.cloud.sql.mysql.SocketFactory   : Connecting to Cloud SQL instance [winter-dynamics-213603:us-centr
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}

1 个答案:

答案 0 :(得分:0)

自您与Google Cloud API进行交互以来,“ insufficientPermissions”与范围相关。 检查您是否为Kubernetes引擎,SQL管理员的API交互提供了正确的作用域,请查看表here。而且它也已经回答了here