Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Unable to connect containerized GCE VM instance to Cloud SQL for PostgreSQL

I'm trying to get my nodejs app deployed to a Compute Engine VM containerized via Docker image to connect properly with my PostgreSQL db on Cloud SQL.

1. I have a VPC network set up
2. I have a static IP set up for the VM instance
3. I tried to add the VM instance static IP to the list of public IP addresses in the DB configs, but it errors out with the message "Invalid request: You entered 'X.XX.XX.X', which is already automatically included in networks authorized by Cloud SQL, and can't be added again." so I assume the VM instance is already allowed access to the SQL instance
4. The VM instance is deploying properly via Cloud Build
5. I see the app booting up in the VM instance logs
6. I see a connection timeout error with the DB's IP address

What could be the issue?

0 1 172
1 REPLY 1

To troubleshoot your connectivity issue between your containerized Compute Engine (GCE) VM instance and Cloud SQL for PostgreSQL, here is a a checklist and some potential issues you might be facing:

Network Configuration

  1. Private IP vs. Public IP:

    • Private IP: If your VM and Cloud SQL instances are in the same VPC network, it’s recommended to use private IP addresses for enhanced security and performance.

    • Public IP: If you’re using a public IP, make sure your connection string points to the correct public IP address of the Cloud SQL instance. Ensure that the Cloud SQL instance is configured to accept connections from your VM's public IP.

    • Action: Double-check that your connection string uses the correct IP address (private or public) based on your setup.

  2. VPC Peering:

    • Requirement: If your VM and Cloud SQL instances are in different VPC networks, ensure proper VPC peering is configured.

    • Firewall Rules: Verify that appropriate firewall rules are in place to allow traffic between the networks.

    • Action: Review your VPC peering configuration and confirm that the necessary firewall rules allow traffic on port 5432 (default PostgreSQL port).

  3. Firewall Rules:

    • Configuration: Ensure your VPC firewall rules allow ingress traffic on port 5432 from your VM instance to the Cloud SQL instance.

    • Action: Verify and adjust firewall rules to allow necessary traffic between your VM and Cloud SQL instance.

Cloud SQL Configuration

  1. Authorized Networks:

    • Check: Confirm that your VM instance’s IP address (or the VPC network range) is included in the authorized networks for your Cloud SQL instance.

    • Error Handling: If you receive an error indicating the VM's IP is already included, it might be automatically covered by a broader range.

    • Action: Ensure the subnet containing your VM's IP is included in the authorized networks.

  2. Cloud SQL Auth Proxy (Recommended):

    • Purpose: The Cloud SQL Auth Proxy establishes a secure connection between your VM and Cloud SQL, managing authentication and connectivity.

    • Action: Consider using the Cloud SQL Auth Proxy for secure and simplified connectivity, which removes the need to whitelist IP addresses.

Container Configuration

  1. Docker Networking:

    • Setup: Ensure that your Docker container is correctly configured to access the network where your Cloud SQL instance resides.

    • Action: Verify the network configuration in your docker-compose.yml file or other Docker setup to ensure it allows access to Cloud SQL.

  2. Environment Variables:

    • Configuration: If you're using environment variables for your Cloud SQL connection details, make sure they are correctly set and accessible within your container.

    • Action: Double-check that all environment variables are correctly configured and passed to your application.

Troubleshooting Steps

  1. Check Connectivity:

    • Test: From your VM instance, try pinging the Cloud SQL instance’s private or public IP address to verify basic network connectivity.

    • Action: Use tools like ping or telnet to test connectivity to the Cloud SQL instance.

  2. Examine Logs:

    • Review: Check the logs of your VM instance, Docker container, and Cloud SQL instance for any error messages that might provide insights into the connection problem.

    • Action: Look for specific error messages that could indicate what might be going wrong.

  3. Connection String:

    • Verification: Triple-check your connection string in your Node.js application. It should include the correct IP address, port, database name, username, and password.

    • Example for Private IP:

       

      postgresql://<username>:<password>@/<database-name>?host=/cloudsql/<project-id>:<region>:<instance-name>

       

    • Example for Public IP:

       

      postgresql://<username>:<password>@<public-ip-address>:<port>/<database-name>

       

Example Cloud SQL Auth Proxy Configuration in Docker

If you're using Docker, here’s a simplified example setup using the Cloud SQL Auth Proxy (in docker-compose.yml) :

version: '3.7'
services:
  cloudsql-proxy:
    image: gcr.io/cloudsql-docker/gce-proxy:1.23.0
    command: /cloud_sql_proxy -instances=<PROJECT_ID>:<REGION>:<INSTANCE_ID>=tcp:5432
    volumes:
      - /path/to/your/credentials.json:/config
    environment:
      - GOOGLE_APPLICATION_CREDENTIALS=/config/credentials.json

  app:
    image: your-app-image
    environment:
      - DB_HOST=127.0.0.1
      - DB_USER=your-db-user
      - DB_PASS=your-db-password
      - DB_NAME=your-db-name
    depends_on:
      - cloudsql-proxy

In this setup, your Node.js app connects to 127.0.0.1:5432, and the Cloud SQL Auth Proxy handles the secure connection to the Cloud SQL instance.