简体   繁体   English

将静态 IP 云函数连接到 Cloud SQL

[英]Connecting Static IP Cloud Functions to Cloud SQL

There are several good answers available for both of these scenarios - but not combined.这两种情况都有几个很好的答案 - 但不能结合起来。

1. I need to make a call to an external API via whitelisted static IP. 1. 我需要通过列入白名单的静态 IP 调用外部 API。
See: Associating Cloud Function egress with a static IP address请参阅:将Cloud Function 出口与静态 IP 地址相关联
a) call the external API from the Cloud Function a)从 Cloud Function 调用外部 API
b) route all egress from this Cloud Function through a VPC Connector on vpcnetwork-1 (IP address range = 10.8.10.0/28) b)通过 vpcnetwork-1(IP 地址范围 = 10.8.10.0/28)上的 VPC 连接器路由从此云功能的所有出口
c) use a Cloud NAT that routes all traffic on vpcnetwork-1 through [STATIC IP] (whitelisted by external API) c)使用 Cloud NAT 通过 [STATIC IP](由外部 API 列入白名单)路由 vpcnetwork-1 上的所有流量

2. Next, I need to take that API data and send it to a Cloud SQL instance (MySQL in this case). 2. 接下来,我需要获取该 API 数据并将其发送到 Cloud SQL 实例(在本例中为 MySQL)。
See: Connecting to Cloud SQL from Cloud Functions请参阅:从 Cloud Functions 连接到 Cloud SQL
a) create a UNIX socketpath connection to [Cloud SQL Instance] a)创建到 [Cloud SQL 实例] 的 UNIX socketpath 连接

When I run with the VPC Connector (as shown above), I get:当我使用 VPC 连接器运行时(如上所示),我得到:
1) SUCCESS! 1)成功! I've received the API data using my whitelisted IP address我已使用列入白名单的 IP 地址收到 API 数据
2) CONNECTION REFUSED by [Cloud SQL Instance] - because I'm using a static external IP? 2) [Cloud SQL 实例] 拒绝连接 - 因为我使用的是静态外部 IP? Does socketpath use external, or connect within my Google Cloud Project? socketpath 是使用外部的,还是在我的 Google Cloud 项目中连接?

If I remove the VPC Connector from my Cloud Function, then I get:如果我从 Cloud Function 中删除 VPC 连接器,则会得到:
1) CONNECTION REFUSED - this IP is not whitelisted (because I'm no longer using the static IP) 1) CONNECTION REFUSED - 此 IP 未列入白名单(因为我不再使用静态 IP)
2) SUCCESS! 2)成功! I'm now able to connect to [Cloud SQL Instance] (using UNIX socketpath, userid, password)我现在可以连接到 [Cloud SQL 实例](使用 UNIX 套接字路径、用户 ID、密码)

How can I get both of these to work from the same Cloud Function?我怎样才能让这两个功能在同一个 Cloud Function 中工作?

I see that I can "Route only requests to private IPs through the VPC connector" but I really want the opposite of that.我看到我可以“仅通过 VPC 连接器将请求路由到私有 IP”,但我真的想要相反的。 I want to only route external requests to the VPC connector, to use my static IP, and then keep my private routing for connections within my GCP.我只想将外部请求路由到 VPC 连接器,以使用我的静态 IP,然后在我的 GCP 中保留我的私有路由以进行连接。

ADDED: I am using Javascript mysql to connect to Cloud SQL.添加:我正在使用Javascript mysql连接到 Cloud SQL。

var pool = mysql.createPool({ socketPath: '/cloudsql/[instance_connection_name]',
                              user: uid,
                              password: pwd,
                              database: 'mysql_db' });
var result = pool.query(sql, {}, (err,result)=> {});

This works ok without using a VPC Connector.这在不使用 VPC 连接器的情况下可以正常工作。 When I use a VPC Connector with a static external IP address, this connection is refused.当我使用带有静态外部 IP 地址的 VPC 连接器时,此连接被拒绝。 Is it because the VPC Connector and Cloud SQL instance are not on the same VPC?是不是因为 VPC Connector 和 Cloud SQL 实例不在同一个 VPC 上? I don't think Cloud SQL is linked to a VPC, is it?我不认为 Cloud SQL 与 VPC 相关联,是吗?

The Cloud SQL Instance Overview dashboard lists both [Public IP Address] and [Instance Connection Name] Cloud SQL 实例概览仪表板列出了 [公共 IP 地址] 和 [实例连接名称]

For a standard Cloud Functions connection, I use:对于标准的 Cloud Functions 连接,我使用:

socketpath:[Instance Connection Name]
user: uid
password: pwd
database: 'mysql_db'

When using a VPC Connector, I use使用 VPC 连接器时,我使用

host:[Private (or Public) IP Address]
user: uid
password: pwd
database: 'mysql_db'

Summary:概括:

[Cloud Function] -> socketpath:[Instance Connection Name] => **SUCCESS**
[Cloud Function] -> host:[Public IP Address] => **FAIL** (Timeout - IP Not Allowed)

Private VPC Connections:
[Cloud Function] -> [VPC Connector] -> socketpath:[Instance Connection Name] => **FAIL** (Connection Refused)
[Cloud Function] -> [VPC Connector] -> host:[Private IP Address] => **SUCCESS** (Set up Private IP in GCP->SQL->Connections)

Public VPC Connection:
[Cloud Function] -> [VPC Connector] -> host:[Public IP Address] => **SUCCESS** (Only after allowing/whitelisting IP of the VPC Connector routed through Cloud NAT)

Google Cloud CLI instructions for Private IP setup:私有 IP 设置的 Google Cloud CLI 说明:
CONNECTING FROM GOOGLE CLOUD FUNCTIONS TO CLOUD SQL USING TCP AND UNIX DOMAIN SOCKETS 2020 使用 TCP 和 UNIX 域套接字 2020 从 GOOGLE 云功能连接到云 SQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM