通过VPC连接器连接Google Cloud Platform的计算引擎和应用引擎

时间:2020-09-26 13:20:42

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

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

我已经在Google计算引擎上设置了虚拟机实例,并且我的Postgres服务器在该计算机上运行,​​具体操作如下:https://cloud.google.com/community/tutorials/setting-up-postgres

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

我搜索了如何将计算引擎和应用程序引擎连接在一起,似乎可以通过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       

在我的flask应用上,我试图像这样连接到远程数据库:

    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

2 个答案:

答案 0 :(得分:1)

事实证明,防火墙/ postgres配置都可以,但是由于此VPC连接器方法处于beta状态,因此我需要运行:

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)?

启用此功能后,一切正常。

答案 1 :(得分:0)

根据提供的信息,似乎VPC防火墙规则和连接器均已正确配置。

但是,根据消息

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

好像使用10.154.0.2的VM或服务器在端口5432上不接受请求或未打开端口,可以使用this站点进行端口扫描。

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