Connecting to a Cloud SQL instance from a VM with a public IP and private IP - how do I ensure the correct network interface is selected for the connection?


dreams

What I want to set:

  • Cloud SQL instance with private IP, Postgresql database
  • A VM with a public IP and one private IP on the same VPC network as the SQL instance (VM, SQL instance and VPC are all in the same region)
  • The VM's service account has sufficient Cloud SQL client/viewer permissions
  • Possibility to connect to SQL instance from VM.

What's wrong?

  • Any attempt to actually use the connection (e.g. from the psql client or db-migrate) will hang - e.g. psql --host 10.78.0.3 -U gcp-network-issue-demo-staging-db-user gcp-network-issue-demo-staging-databasenot prompt for a password, just sit there.
  • If I remove the VM's public IP address from the settings, the connection works fine. However, I need a publicly accessible VM that other services can connect to.

I'm assuming the psql connection attempt is going through the wrong network interface or something (it's probably just my ignorance of network stuff) - how do I get this to work? What am I missing?

PS: This is basically the same issue as failing to connect to a Google Cloud SQL instance on a private IP from a VM with a private IP and a public IP , but the commenters there seem to want a Terraform related question as well as a connection to problem related issues.

Some screenshots:

VM IP:VM IP address

Database IP:Database IP address

VM's network configuration:virtual machine network

Private IP configuration for the DB instance:Database instance connection configuration

Here are the settings for the private network:VPC configuration screenshot

I don't understand why the private IP of the DB instance (10.78.0.3) is not an IP from the range of the private network (10.2.0.0-10.2.0.24, right?)..? Is that my problem?

Khalid K

To answer your question:

I don't understand why the private IP of the DB instance (10.78.0.3) is not an IP from the range of the private network (10.2.0.0-10.2.0.24, right?)..?

The Cloud SQL instance is assigned an IP address from the allocated range. When you setup a private services access a VPC peering is created between your VPC gcp-network-issue-demo-staging-network and the service producer VPC network that uses the allocated range 10.78.0.0/16 enter image description here

Also, looking at your VM network configuration, I see that the VM has two Nics in two different VPCs ( default and gcp-network-issue-demo-staging-network ) . In your case you can only use one Nic.

Next, make sure your VMs only use the VPC network you used to create the private connection . After doing this, you should be able to connect to the Cloud SQL instance IP with:

telnet 10.78.0.3 3306

Related


Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

Convert CloudSQL instance from public IP to private IP

Zama Ques We cannot cast an CloudSQLinstance Public IPof Private IP. The allocation Private IPis in use .SharedVPCPrivate service Connection For me it fails with the following error $ gcloud --project=<project id> beta sql instances patch test1-instance --n

How do I deny IP addresses from connecting to my system?

Nux Is there a way to deny IP addresses from connecting to my PC? Is there a way to block an IP from connecting to a specific port? Thomas Ward Before proceeding, please note a few things. Most of the Internet does not have static IP addresses. So proxies, Tor

How do I deny IP addresses from connecting to my system?

Nux Is there a way to deny IP addresses from connecting to my PC? Is there a way to block an IP from connecting to a specific port? Thomas Ward Before proceeding, please note a few things. Most of the Internet does not have static IP addresses. So proxies, Tor

How do I deny IP addresses from connecting to my system?

Nux Is there a way to deny IP addresses from connecting to my PC? Is there a way to block an IP from connecting to a specific port? Thomas Ward Before proceeding, please note a few things. Most of the Internet does not have static IP addresses. So proxies, Tor

How to attach public and private reserved IP to GCE instance?

red 888 https://www.terraform.io/docs/providers/google/r/compute_instance.html I want to keep an external and internal IP, but how do I attach both? I don't see an example in the tf documentation. It only has network_ip - (Optional) The *private* IP address ..

How to attach public and private reserved IP to GCE instance?

red 888 https://www.terraform.io/docs/providers/google/r/compute_instance.html I want to keep an external and internal IP, but how do I attach both? I don't see an example in the tf documentation. It only has network_ip - (Optional) The *private* IP address ..

Unable to enable private IP for my Postgres Cloud SQL instance

McGinn When I try to enable private IP on my Cloud SQL instance (PostgreSQL 9.6), I get the following error message: Network association failed due to the following error: set Service Networking service account as servicenetworking.serviceAgent role on consume