Connecting node.js running on Kubernetes to Cloud Sql

5/21/2019

I'm trying to connect my node.js backend to a Cloud SQL on the same project, following many tutorials and documentation, but without success.

Here my service and endpoint config:

kind: Service
apiVersion: v1
metadata:
  name: mysql-service
spec:
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
---
kind: Endpoints
apiVersion: v1
metadata:
  name: mysql-service
subsets:
  - addresses:
    - ip: 10.78.176.3
    ports:
    - port: 3306

I got on my node.js side:

process.DB_HOST 10.78.176.3
process.DB_DATABASE marketplace_test
MySQL Connection pool error
connect ETIMEDOUT

I already tried to connect over the public and the private IP, over the 'mysql-service' name, but nothing worked.

Could it be some relation I'm missing, like a matchLabels? Should I match the service+endpoint+deployment? How? Must I use a proxy for that?

What I'm missing?

Thanks for all help!

-- Tiago GouvĂȘa
google-cloud-platform
google-cloud-sql
kubernetes

1 Answer

5/22/2019

If you are connecting via Private IP, you need to make sure you meet the environment requirements on your GKE cluster. This means that it needs to be a VPC-native cluster. If it's not, you'll need to use public IP.

If you are using a Public IP, you need to authenticate your connection. There are 3 main ways to do this:

-- kurtisvg
Source: StackOverflow