从具有私有和公共 IP 的虚拟机连接到私有 IP 上的 Google Cloud SQL 实例失败 [英] Connecting to Google Cloud SQL instance on private IP from a VM with both private and public IPs fails

查看:18
本文介绍了从具有私有和公共 IP 的虚拟机连接到私有 IP 上的 Google Cloud SQL 实例失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 具有私有 IP 的 Cloud SQL 实例、Postgresql 数据库
  • 一台拥有一个公网 IP 和一个私网 IP 的虚拟机,与 SQL 实例在同一个 VPC 网络上(虚拟机、SQL 实例和 VPC 都在同一个区域)
  • VM 有一个服务帐号,该帐号具有足够的 Cloud SQL 客户端/查看者权限
  • VM 上的 SQL 代理连接到 SQL 实例.我使用在某些文档中找到的 -ip_address_types=PRIVATE 参数运行它.
  • Cloud SQL instance with private IP, Postgresql database
  • A VM with one public IP and one private IP on same VPC network as the SQL instance is on (VM, SQL instance and VPC are all in the same region)
  • VM has a service account with sufficient Cloud SQL client/viewer permissions
  • SQL proxy on VM connecting to SQL instance. I run it with the -ip_address_types=PRIVATE argument I've found in some of the documentation.

用于重现令我困惑的状态的稍微简化的 Terraform 代码如下:https://github.com/hallvors/gcp-network-issue-demo要对此进行测试,请执行以下操作:

Slightly simplified Terraform code for reproducing the state that confuses me is here: https://github.com/hallvors/gcp-network-issue-demo To test this, do the following:

  1. 创建一个新的一次性 Google Cloud 项目.
  2. 为方便起见,您可以运行 bootstrap.sh 来启用正确的服务(它会询问 Google 项目的 ID,并假设您有一个已登录并有权访问的 gcloud 客户端).
  3. 在项目中创建一个服务帐号,方便起见,将其设为所有者,并在 ./local-secrets/google-project-credentials.json 中保存一个密钥文件
  4. 使用项目 ID 和服务帐户的电子邮件更新 terraform.tfvars
  5. terraform 工作区新登台
  6. terraform 初始化
  7. terraform 应用

Terraform 完成后,您应该在项目中设置一个数据库和一个虚拟机.

When Terraform is done, you should have a database and a VM set up in the project.

  1. SSH 进入虚拟机并运行 sudo apt install postgresql-client-common postgresql-client
  2. 查找数据库实例的 IP 地址
  3. 运行这个(根据需要修改细节)psql --host 10.167.0.3 -U gcp-network-issue-demo-staging-db-user gcp-network-issue-demo-staging-database

会发生什么?

  • 任何实际使用连接的尝试(例如来自 psql 客户端或 db-migrate)都会超时
  • 如果我从设置中删除虚拟机的公共 IP 地址,它可以正常连接.但是,我需要一个可公开访问的 VM 以供其他服务连接到它.
  • 我错过了什么?

    推荐答案

    这个问题的原因是我没有理解网络接口可以有公共和私有 IP/网络.所以我的代码为 google_compute_instance 设置了一个公共接口和一个专用网络接口:

    The cause of this problem was me failing to understand that a network interface can have both public and private IPs/networks. So my code set up one interface for the public and one for the private network for the google_compute_instance:

      # Update VM needs a public IP
      network_interface {
        network = "default"
        access_config {
        }
      }
    
      network_interface {
        network    = var.network
        subnetwork = var.subnetwork
      }
    

    现在,我仍然不完全了解网络,但您似乎无法(很容易?)指定数据库连接尝试应使用的接口,并且它不会自动选择正确的接口.修复在此提交中,同时配置了对专用网络和公共网络接口的访问:

    Now, I still don't fully understand networking but it appears you can not (easily?) specify what interface the database connection attempts should use and it does not automatically pick the right one. The fix is in this commit, configuring both access to the private network and the public one in one network interface:

    https://github.com/hallvors/gcp-network-issue-demo/commit/ea14174c1087c89b92310b5b4913e12a4e17130d

    这篇关于从具有私有和公共 IP 的虚拟机连接到私有 IP 上的 Google Cloud SQL 实例失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆