Google Cloud SQL proxy with Autoscaling

Nidhi Singh
Google Cloud - Community
2 min readDec 27, 2017

One of the best feature of GCP is Autoscaling in Managed instance group.

Nowadays all applications need auto scaling feature as one can predict the traffic all the time and we do need our application ready for any type of traffic and also not pay extra when there is no traffic.

A managed instance group uses an template to create a group of identical instances. You control a managed instance group as a single entity. If you wanted to make changes to instances that are part of a managed instance group, you would make the change to the whole instance group.

In case of high traffic autoscaling will use this instance template and launch a new server to handle the requests in the instance group and also scale down when needed.

Till now all seems good and easy.

Now your application needs a database.

Google Cloud SQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your relational databases on Google Cloud Platform. You can use Cloud SQL with either MySQL or PostgreSQL.

My application is a wordpress application on GCE and database on Google Cloud SQL. As we know Google Cloud SQL allows connection from authorized IP only. But all the new instances created in managed instance group have new external IP which are not static. So in order to allow all my new instances connect to my Google Cloud SQL instance what i can use is Cloud SQL proxy.

The Cloud SQL Proxy provides secure access to your Cloud SQL Second Generation instances without having to whitelist IP addresses or configure SSL.

Cloud SQL Proxy Installation on GCE

https://cloud.google.com/sql/docs/mysql/connect-compute-engine#gce-connect-proxy

These steps are performed on instances but as our instances are created on demand the solution to this would be to add these steps in startup scripts.

Eg:

#!/bin/sh
sudo apt-get update
sudo apt-get install mysql-client
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
chmod +x cloud_sql_proxy
sudo mkdir /cloudsql; sudo chmod 777 /cloudsql
./cloud_sql_proxy -dir=/cloudsql &

This will allow every new instance created in that instance group able to connect to google cloud SQL.

You can also use service accounts to authenticate the instance . The service account for that instance can be grabbed from default instance metadata values.

--

--

Nidhi Singh
Google Cloud - Community

I am a Cloud infrastructure technologist with 8+ years of experience.