Having troubles connecting my kubernetes engine application to google cloud sql instance using sidecar method

9/12/2020

I'm pretty new to Kubernetes and google cloud and am trying to deploy my dropwizard microservice to the cloud. I have a mysql instance set up on the cloud with all the secrets and service account created. Whenever I create the deployment and the load balancer and then get the logs, it throws the communication links failure error. The config file in the image in my container registry is set to use localhost as the host. Here's the yml file for my deployment

kind: Deployment
metadata:
  labels:
    app: users-service
  name: users-service
  annotations:
    deployment.kubernetes.io/revision: "1"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: users-service
  template:
    metadata:
      labels:
        app: users-service
    spec:
      containers:
        - image: gcr.io/users-service-289022/users-service:v1
          imagePullPolicy: Always
          name: users-service-container
          ports:
            - containerPort: 8080
              name: app-port
              protocol: TCP
            - containerPort: 8081
              name: admin-port
              protocol: TCP
          env:
          - name: DB_USER
            valueFrom:
              secretKeyRef:
                name: cloudsql-db-credentials
                key: username
          - name: DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: cloudsql-db-credentials
                key: password
          resources: {}
        - name: cloudsql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:1.17
          command: ["/cloud_sql_proxy",
                    "-instances=lusers-service-289022:us-east1:mysql-db=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
      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
        - name: cloudsql
          emptyDir:
        - name: ssl-certs
          hostPath:
            path: /etc/ssl/certs
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler 

This is the output from the logs:

Causing: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure
!
! The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
! at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
! at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
! at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
! at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
! at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
! at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
! at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
! at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
! at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
! ... 26 common frames omitted
! Causing: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
!
! The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
! at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:166)
! at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:58)
! at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
! at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
! at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
! at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
! at io.dropwizard.db.ManagedPooledDataSource.start(ManagedPooledDataSource.java:37)
! at io.dropwizard.lifecycle.JettyManaged.doStart(JettyManaged.java:27)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
! at org.eclipse.jetty.server.Server.start(Server.java:449)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:113)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
! at org.eclipse.jetty.server.Server.doStart(Server.java:416)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)
! at io.dropwizard.Application.run(Application.java:93)
! at com.cb.CBUsersService.main(CBUsersService.java:30)
INFO  [2020-09-12 15:57:23,835] org.eclipse.jetty.server.AbstractConnector: Started application@3069a360{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
WARN  [2020-09-12 15:57:23,846] org.eclipse.jetty.util.thread.QueuedThreadPool: org.eclipse.jetty.util.thread.QueuedThreadPool@dw-admin{STOPPED,1<=0<=64,i=0,q=0} rejected org.eclipse.jetty.io.ManagedSelector$$Lambda$167/0x0000000800e08040@7f6329cb
ERROR [2020-09-12 15:57:23,848] io.dropwizard.cli.ServerCommand: Unable to start server, shutting down
! java.net.ConnectException: Connection refused
! at java.base/sun.nio.ch.Net.connect0(Native Method)
! at java.base/sun.nio.ch.Net.connect(Net.java:503)
! at java.base/sun.nio.ch.Net.connect(Net.java:492)
! at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
! at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
! at java.base/java.net.Socket.connect(Socket.java:648)
! at com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:202)
! at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
! ... 29 common frames omitted
! Causing: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure
!
! The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
! at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
! at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
! at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
! at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
! at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
! at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
! at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
! at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:204)
! at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:975)
! at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
! ... 26 common frames omitted
! Causing: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
!
! The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
! at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:166)
! at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:58)
! at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
! at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
! at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
! at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
! at io.dropwizard.db.ManagedPooledDataSource.start(ManagedPooledDataSource.java:37)
! at io.dropwizard.lifecycle.JettyManaged.doStart(JettyManaged.java:27)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
! at org.eclipse.jetty.server.Server.start(Server.java:449)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:113)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
! at org.eclipse.jetty.server.Server.doStart(Server.java:416)
! ... 7 common frames omitted
! Causing: org.eclipse.jetty.util.MultiException: Multiple exceptions
! at org.eclipse.jetty.server.Server.doStart(Server.java:413)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)
! at io.dropwizard.Application.run(Application.java:93)
! at com.cb.CBUsersService.main(CBUsersService.java:30)
! Suppressed: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.io.ManagedSelector$$Lambda$167/0x0000000800e08040@7f6329cb
!       at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:385)
!       at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:167)
!       at org.eclipse.jetty.io.ManagedSelector.doStart(ManagedSelector.java:99)
!       at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
!       at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
!       at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:113)
!       at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:305)
!       at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
!       at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
!       at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:105)
!       at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:276)
!       at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81)
!       at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:238)
!       at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
!       at org.eclipse.jetty.server.Server.doStart(Server.java:428)
!       ... 7 common frames omitted
-- Ishaan Khurana
cloud-sql-proxy
google-cloud-platform
google-cloud-sql
kubernetes
mysql

0 Answers