我正在使用 GCP 管理的 Cloud Run 和 MongoDB Atlas 开发应用程序。如果我允许从任何地方连接 Atlas 的 IP 白名单,Cloud Run 就可以完美地与 MongoDB Atlas 配合使用。但是,我只想限制必要 IP 的连接,但我没有找到 Cloud Run 的出站 IP。有什么办法知道出站IP?
4 回答
更新(2020 年 10 月): Cloud Run 现已推出VPC 出口功能,可让您为通过 Cloud NAT 的出站请求配置静态 IP。您可以按照文档中的分步指南将静态 IP 配置为在 MongoDB Atlas 上的白名单。
在 Cloud Run 开始支持 Cloud NAT 或无服务器 VPC 访问之前,很遗憾,这不受支持。
正如@Steren 所提到的,您可以通过运行客户端来创建 SOCKS 代理,该ssh
客户端通过具有静态外部 IP 地址的 GCE VM 实例路由流量。
我在这里写了一篇博客:https ://ahmet.im/blog/cloud-run-static-ip/ ,您可以在以下网址找到带有工作示例的分步说明:https ://github.com/ ahmetb/cloud-run-static-outbound-ip
Cloud Run(与所有可扩展的无服务器产品一样)不会为您提供已知为传出流量来源的专用 IP 地址。另请参阅:可以获得 Google Cloud Functions 的静态 IP 地址吗?
Cloud Run 服务不会获得静态 IP。
一种解决方案是通过具有静态 IP 的代理发送出站请求。
例如在 Python 中:
import requests
import sys
from flask import Flask
import os
app = Flask(__name__)
@app.route("/")
def hello():
proxy = os.environ.get('PROXY')
proxyDict = {
"http": proxy,
"https": proxy
}
r = requests.get('http://ifconfig.me/ip', proxies=proxyDict)
return 'You connected from IP address: ' + r.text
使用PROXY
包含代理的 IP 或 URL 的环境变量(请参阅此处设置环境变量)
对于此代理,您可以:
- 自己创建它,例如使用具有运行squid的静态公共 IP 地址的 Compute Engine 虚拟机,这可能适合 Compute Engine 免费层。
- 使用提供静态 IP 代理的服务,例如https://www.quotaguard.com/static-ip/起价为 $19/m
我个人使用了第二种解决方案。该服务为我提供了一个包含用户名和密码的 URL,然后我使用上面的代码将其用作代理。
此功能现已由 Cloud Run 团队发布Beta 版:
https://cloud.google.com/run/docs/configuring/static-outbound-ip