【发布时间】: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
【问题讨论】:
-
你们使用同一个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