Spring Boot + google kubernetes + Google SQL Cloud not working

8/18/2017

I am trying to push spring boot application in google kubernetes(Google Container Engine).

I have performed all the step which given in below link.

https://codelabs.developers.google.com/codelabs/cloud-springboot-kubernetes/index.html?index=..%2F..%2Findex#0

When i am trying to perform step 9 http://:8080 in browser that is not reachable.

Yes i got external ip address.

I am able to ping that ip address

let me know if any other information is require.

In Logging that does not able to connect database

Error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

-- Dudhat Dhaval
google-cloud-platform
google-kubernetes-engine
kubernetes
spring-boot

1 Answer

8/22/2017

I hope you have created cluster in google container engine

Follow the first 5 step given in this link

https://cloud.google.com/sql/docs/mysql/connect-container-engine

change database configuration in your application

hostname: 127.0.0.1 port: 3306 or your mysql port username: proxyuser

should be same as link step - 3

  1. mvn package -Dmaven.test.skip=true
  2. Create File with name "Dockerfile" and below content

    FROM openjdk:8 COPY target/SpringBootWithDB-0.0.1-SNAPSHOT.jar /app.jar EXPOSE 8080/tcp ENTRYPOINT ["java", "-jar", "/app.jar"]

  3. docker build -t gcr.io//springbootdb-java:v1 .

  4. docker run -ti --rm -p 8080:8080 gcr.io//springbootdb-java:v1

  5. gcloud docker -- push gcr.io//springbootdb-java:v1

    Follow the 6th step given in link and create yaml file

  6. kubectl create -f cloudsql_deployment.yaml

    run kubectl get deployment and copy name of deployment

  7. kubectl expose deployment --type=LoadBalancer


My Yaml File

apiVersion: extensions/v1beta1 kind: Deployment metadata: name: conversationally spec: replicas: 1 template: metadata: labels: app: conversationally spec: containers: - image: gcr.io/<project ID>/springbootdb-java:v1 name: web env: - name: DB_HOST # Connect to the SQL proxy over the local network on a fixed port. # Change the [PORT] to the port number used by your database # (e.g. 3306). value: 127.0.0.1:3306 # These secrets are required to start the pod. # [START cloudsql_secrets] - name: DB_PASSWORD valueFrom: secretKeyRef: name: cloudsql-db-credentials key: password - name: DB_USER valueFrom: secretKeyRef: name: cloudsql-db-credentials key: username # [END cloudsql_secrets] ports: - containerPort: 8080 name: conv-cluster # Change [INSTANCE_CONNECTION_NAME] here to include your GCP # project, the region of your Cloud SQL instance and the name # of your Cloud SQL instance. The format is # $PROJECT:$REGION:$INSTANCE # Insert the port number used by your database. # [START proxy_container] - image: gcr.io/cloudsql-docker/gce-proxy:1.09 name: cloudsql-proxy command: ["/cloud_sql_proxy", "--dir=/cloudsql", "-instances=<instance name>=tcp:3306", "-credential_file=/secrets/cloudsql/credentials.json"] volumeMounts: - name: cloudsql-instance-credentials mountPath: /secrets/cloudsql readOnly: true - name: ssl-certs mountPath: /etc/ssl/certs - name: cloudsql mountPath: /cloudsql # [END proxy_container] # [START volumes] volumes: - name: cloudsql-instance-credentials secret: secretName: cloudsql-instance-credentials - name: ssl-certs hostPath: path: /etc/ssl/certs - name: cloudsql emptyDir: # [END volumes]

\===========

-- Dudhat Dhaval
Source: StackOverflow