Connecting to Google cloud mysql instance from GKE cluster using cloudsqlproxy

11/25/2018

I have two projects in GCP viz. project1 and project2

I have setup mysql instance in project1.

I have also setup cloudsqlproxy (pod) and mypod in a GKE cluster in project2. I want to access mysql instance frommypodthroughcloudsqlproxy`.

I have the following code for cloudmysqlproxy

apiVersion: v1
kind: Service
metadata:
  name: cloudsqlproxy-service-mainnet
  namespace: dev
spec:
  ports:
  - port: 3306
    targetPort: port-mainnet
  selector:
    app: cloudsqlproxy

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudsqlproxy
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloudsqlproxy
  template:
    metadata:
      labels:
        app: cloudsqlproxy
    spec:
      containers:
       # Make sure to specify image tag in production
       # Check out the newest version in release page
       # https://github.com/GoogleCloudPlatform/cloudsql-proxy/releases
      - name: cloudsqlproxy
        image: b.gcr.io/cloudsql-docker/gce-proxy:latest
       # 'Always' if imageTag is 'latest', else set to 'IfNotPresent'
        imagePullPolicy: Always
        name: cloudsqlproxy
        command:
        - /cloud_sql_proxy
        - -dir=/cloudsql
        - -instances=project1:asia-east1:development=tcp:0.0.0.0:3306
        - -credential_file=/credentials/credentials.json
        ports:
        - name: port-mainnet
          containerPort: 3306
        volumeMounts:
        - mountPath: /cloudsql
          name: cloudsql
        - mountPath: /credentials
          name: cloud-sql-client-account-token
      volumes:
      - name: cloudsql
        emptyDir:
      - name: cloud-sql-client-account-token
        secret:
          secretName: cloud-sql-client-account-token

I have setup cloud-sql-client-account-token in the following manner:

kubectl create secret cloud-sql-client-account-token --from-file=credentials.json=$HOME/credentials.json

Where I downloaded the credentials.json file from a service account in project1.

When I try to access the mysql instance from my pod, I get the folløwing error:

mysql --host=cloudsqlproxy-service-mainnet.dev.svc.cluster.local --port=3306
ERROR 1045 (28000): Access denied for user 'root'@'cloudsqlproxy~35.187.201.86' (using password: NO)

And the cloud-proxy logs, I get the following:

2018/11/25 00:35:31 Instance project1:asia-east1:development closed connection

Is it necessary to launch a mysql instance in the same project (project2) as the pod? What am I missing?

EDIT

I can access the proxy on my local machine by setting up like this

/cloud_sql_proxy -instances=project1:asia-east1:development=tcp:3306

and then connecting to the proxy using a mysql client.

-- kosta
cloud-sql-proxy
google-cloud-platform
google-cloud-sql
google-kubernetes-engine

0 Answers