How to connect to Google Cloud SQL with python's pymysql running as part of a dockerized container running in a Kubernetes cluster?

5/13/2020

Currently I am accessing my Google Cloud SQL locally through a proxy, as in the following.

conn = pymysql.connect(unix_socket='cloudsql/'+project_name, user=user, password=password, db=db)

Now I have set up my cluster and whitelisted its External IP. I need to change the line above to connect to Cloud SQL from Kubernetes. (I don't think I can keep using the proxy, as it cannot be dockerized) I am unsure how to change my pymysql.connect() call. Any advice will be greatly appreciated.

-- Steve Zakharov
google-cloud-platform
google-cloud-sql
kubernetes
pymysql
python

1 Answer

5/13/2020

There's an official Cloud SQL proxy docker image that you can use as a sidecar container next to your app container, in the same pod. This is actually the recommended way to connect to Cloud SQL from GKE.

You'll find a detailed deployment configuration here (this example is for wordpress but the idea is the same). Your app will use 127.0.0.1:3306 as the host address to access your database

-- LundinCast
Source: StackOverflow