【问题标题】:Connecting Google Cloud Platform's compute engine and app engine via VPC connector通过 VPC 连接器连接 Google Cloud Platform 的计算引擎和应用引擎
【发布时间】:2021-01-12 14:49:53
【问题描述】:

我想详细了解如何连接google计算引擎虚拟机实例和应用引擎。

我在 Google 计算引擎上设置了一个虚拟机实例,我的 Postgres 服务器正在那里运行,遵循本教程:https://cloud.google.com/community/tutorials/setting-up-postgres

我已经在 Google Cloud Platform 上的同一个项目下部署了我的烧瓶应用,创建了一个应用引擎实例。

我搜索了如何将计算引擎和应用引擎连接在一起,似乎可以通过VPC连接器:connect Google App Engine and Google Compute Engine

这是我的 VPC 连接器的样子:

无服务器 VPC 访问

Name            Network Region          IP address range Min. throughput Max. throughput
connector-name  default europe-west2    10.8.0.0/28      200             300    

在我的计算引擎上,我的虚拟机实例如下:

Name        Zone            Internal IP         External IP 
some-name   europe-west2-c  10.154.0.2 (nic0)   34.89.113.193       

在我的烧瓶应用程序上,我正在尝试像这样连接到我的远程数据库:

    db = PostgresqlExtDatabase(
        "some-name", # databse name
        user="postgres",
        password="some-password",
        host="10.154.0.2", # remote host internal ip
        port=5432,
    )
    db.connect()

这是我的 vpc 访问部分的 app.yaml,我遵循了这个参考:https://cloud.google.com/appengine/docs/standard/python/connecting-vpc#configuring

vpc_access_connector:
  name: projects/some-name/locations/europe-west2/connectors/connector-name

如果我理解正确,如果存在 VPC 连接器,我应该只能使用我的 VM 实例的内部 IP 地址(本例为 10.154.0.2)进行连接?

问题是,当应用部署到生产环境时,它仍然抱怨无法连接:

2020-09-26 12:54:51 default[20200926t134815]    Is the server running on host "10.154.0.2" and accepting
2020-09-26 12:54:51 default[20200926t134815]    TCP/IP connections on port 5432?

如果它是内部连接的,我假设我不必将该内部 IP 添加到防火墙规则中,尽管我也尝试过。至于防火墙规则,我已经允许我的本地机器的 IP 地址,所以我可以通过 PgAdmin 连接到远程 Postgres 服务器。

我实际上也尝试过外部 IP(34.89.113.193),尽管这对我来说没有意义。

总的来说,我在网络和后端方面有点菜鸟,任何帮助都将不胜感激。

更新 1 这是我的防火墙规则:

Direction
Ingress, Egress

Action on match
Allow

Source filters
IP ranges
92.40.176.9/32
78.146.103.141/32
10.154.0.2

Protocols and ports
tcp:5432

图片供参考:Screenshot for the list of firewall rules

【问题讨论】:

  • 你们使用同一个VPC(名称:默认)吗?如果是这样,你能分享你的防火墙规则吗?
  • 我想是的,在“VPC 网络”菜单下,在“VPC 网络”选项卡上,我有一个包含很多区域的“默认”,然后在“无服务器 VPC 访问”选项卡下,我有连接器那是使用相同的默认网络 - 我已经更新了防火墙规则。谢谢!
  • 以这种方式连接到远程数据库时,我应该使用VM的内部IP地址(10.154.0.2),而不是连接器的IP地址(10.8.0.0),对吧?
  • 是的,您使用了正确的内部 IP。您共享防火墙规则以从外部访问数据库(您可以共享目标吗?它是标签吗?全是 wm?)。另外,可以分享一下其他防火墙规则吗?

标签: postgresql google-app-engine google-cloud-platform google-compute-engine vpc


【解决方案1】:

根据提供的信息,VPC 防火墙规则和连接器似乎都配置良好。

但是,根据消息

2020-09-26 12:54:51 default[20200926t134815] 服务器是否在主机“10.154.0.2”上运行并接受 2020-09-26 12:54:51 默认[20200926t134815] 端口 5432 上的 TCP/IP 连接?

好像使用 10.154.0.2 的虚拟机或服务器不接受 5432 端口上的请求或者该端口尚未打开,您可以使用this 站点进行端口扫描。

根据您创建 PostgreSQL 所遵循的指南,您使用的是 Ubuntu 作为操作系统,因此我建议您在 ubuntu 中打开端口并查看问题是否仍然存在。

【讨论】:

    【解决方案2】:

    事实证明防火墙/postgres 配置一切正常,但由于此 VPC 连接器方法处于测试阶段,我需要运行:

    gcloud beta app deploy

    而不是通常的

    gcloud app deploy.

    此命令随后更新了 gcloud Beta 命令并提示我启用 API:

    API [appengine.googleapis.com] not enabled on project [742932836941]. Would you like to enable and retry (this will take a few minutes)? (y/N)?

    启用后一切正常。

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2020-06-30
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多