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

1

9

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.

Pixilated answered 4/2, 2016 at 4:19 Comment(3)
I'm not too familiar with Kubernetes, but could you run the Cloud SQL proxy on your Kubernetes nodes and use that to connect? cloud.google.com/sql/docs/sql-proxySpeechless
I'm going to check and I let you know, thank you @Speechless !Pixilated
Can't you just tell cloud-SQL about your nodes IP addresses? This is far from perfect, I understand, but there's something of a mismatch between the new container-centric world and the existing VM-centric world.Bellay
P
7

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.

Pixilated answered 12/2, 2016 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.