Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to your Cloud SQL instance with a kubernetes service?

I'm creating a container with a connection to a cloudsql database, when I run the image with kubernetes It does not have an external IP that I can use to allow the new image to connect to the database. But as this is part of the init configuration I can't wait to know what is the public IP to add to the whitelist databases.

I know that are ways to connect a database through services in the same cluster, but I can't figure out how to connect with the cloudsql provided by google.

like image 200
agares Avatar asked Feb 04 '16 04:02

agares


1 Answers

There are two ways to solve that:

The first option is to use a cloudsql proxy using the instructions available in: https://cloud.google.com/sql/docs/sql-proxy

In your docker image you need to ensure that fuse is available in your installation, in wasn't my case (using a ubuntu:trusty-20160119 as base image). If you need to able that, then use the following steps in your Dockerfile:

# install fusermount
# RUN apt-get install build-essential -y
# RUN wget https://github.com/libfuse/libfuse/releases/download/fuse_2_9_5/fuse-2.9.5.tar.gz
# RUN tar -xzvf fuse-2.9.5.tar.gz
# RUN cd fuse-2.9.5 && ./configure && make -j8 && make install

Then at the startup of your container you must create a script that open the socket as described in https://cloud.google.com/sql/docs/sql-proxy#example_proxy_invocations_and_connection_strings.

The second way is just to allow the ips from the nodes that support the kubernetes cluster in the whitelist for the cloudsql.

I prefer the first option, because it works in any machine I deploy the image and I don't need to care about to add or remove ips if I need to deliver more nodes in the kubernetes cluster.

like image 73
agares Avatar answered Nov 04 '22 01:11

agares