Automatically start gcloud sql proxy when google compute engine VM starts
Asked Answered
S

2

7

I'm using google compute engine and have an auto scaling instance group that spins up new VMs as needed all sitting behind a load balancer. I'm also using google's cloud SQL in the same project. The VMs need to connect to the cloud SQL instance.

Since the IPs of the VMs are dynamic I can't just plug in the IPs to the SQL access config so I followed the cloud sql proxy setup along with the notes from this very similar question: How to connect from a pool of Google Compute Engine instances to Cloud SQL DB in the same project?

I can now log into a single test VM and run:

./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306

and everything works great and that VM connects to the cloud SQL instance.

The next step is where I'm having issues. How can I setup the VM so it automatically starts up the proxy when it's either built from an instance template or just restarted. The obvious answer seem to be to shove the above in the VM's start-up script but that doesn't seem to be working. So with my single test VM I can SSH into the VM and manually run the cloud_sql_proxy command and all works. If I then include the below in my start-up script and restart the VM it doesn't connect:

#! /bin/bash
./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306

Any suggestions? I seriously can't believe it's this hard to connect to the SQL cloud from a VM in the same project...

Salutatory answered 26/9, 2016 at 18:35 Comment(0)
R
4

The startup script you have shown doesn’t show the download step of the cloud_sql_proxy.

You need to first download and then launch the proxy. So, your startup script should look like:

sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
sudo mv cloud_sql_proxy.linux.amd64 cloud_sql_proxy
sudo chmod +x cloud_sql_proxy
sudo ./cloud_sql_proxy -instances=PROJ_NAME:TIMEZONE:SQL_NAME=tcp:3306 &
Royroyal answered 26/9, 2016 at 19:27 Comment(2)
That did the trick. I assumed that since I had already manually downloaded and installed the cloud_sql_proxy that I could skip that step in my start-up script and instead just run the last command to fire it up again.Salutatory
Your manual download was on one specific instance in your auto-scaling group. Those instances come and go. You need to work with the assumption that the next minute, it could be a fresh machine, which won't have any of the changes you have manually done. One more thing is that u should keep an eye on the instance's logs when it's starting up (by going to instance details page and clicking "View Serial Port" near the bottom. When your instances were starting without the startup script downloading the proxy first, it must have shown "cloud_sql_proxy: command not found") in the logs.Royroyal
C
-1

I choose crontab to run cloud_sql_proxy automatically when vm start up.

$crontab -e

and add @reboot cloud_sql_proxy blah blah.

Crookback answered 2/6, 2017 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.