Search code examples
google-cloud-platformgoogle-cloud-sqlgoogle-vpc

Access Cloud SQL instance in separate project


I'm looking for a solution (maybe this isn't the best way) to get an app running on one of our GKE clusters in Project-A, to access a Cloud SQL instance in Project-B, over it's an internal IP and ideally via cloud SQL proxy. Some more info:

  • We have VPC peering in between project-A and Project-B, traffic from both VPC's definitely flows fine
  • We have Cloud SQL proxy running in GKE cluster in project-A with the SQL instance in Project-B defined
  • The cloud SQL instance only has an internal Private IP
  • Pods from GKE cluster in Project-B can access Cloud SQL in the same project (Project-B) so I know the internal connectivity is definitely there
  • Only when we briefly add a public IP to the cloud SQL instance in project B, does the connection work from project A via Cloud SQL Proxy

When I try from Project-A to project-B, we get connection time outs.

I understand that when creating a cloud sql instance with an internal IP, that there is another separate VPC peering connection created called servicenetworking-googleapis.com from the VPC in that same project.

My thoughts here, being from a networking background, is that there is no IP route in project-A, to tell pod traffic to go over the VPC peering connection between the 2 projects if it wants to get to the private IP of the cloud SQL instance.

But I wondered if anyone else has tried to same thing.


Solution

  • I've found in documentation, that transitive peering is not supported. Haven't tried it myself, but it seems that recommended way is to use shared VPC for accessing CloudSQL from multiple projects. In this section: https://cloud.google.com/sql/docs/mysql/private-ip#quick-reference

    Transitive peering
    Only directly peered networks can communicate. Transitive peering is not supported. In other words, if VPC network N1 is peered with N2 and N3, but N2 and N3 are not directly connected, VPC network N2 cannot communicate with VPC network N3 over VPC Network Peering. Clients in one project can connect to Cloud SQL instances in multiple projects using Shared VPC networks.

    You can use the following guide to set up a Shared VPC between your projects. In summary, it involves the following steps:

    • Set the project that hosts your Cloud SQL instance as a host project, since it is the one sharing the resources which in this case includes your Cloud SQL instance.
    • Select the subnets you would like to share to other projects
    • Set the project where your GKE cluster is hosted as the service project. This service project can then be attached to the host project set up previously.
    • Attach the service project to the host project and set up the appropriate VPC administrator roles so that users from the service project are allowed access to the shared resources.