GCP 托管云 运行 的出站 IP 范围是多少?

What are the outbound IP ranges for GCP managed Cloud Run?

我正在使用 GCP 管理的云 运行 和 MongoDB Atlas 开发应用程序。如果我允许从任何地方连接到 Atlas 的 IP 白名单,Cloud 运行 与 MongoDB Atlas 完美配合。但是,我只想限制必要 IP 的连接,但我找不到 Cloud 运行 的出站 IP。有什么方法可以知道出站 IP?

Cloud 运行(与所有可扩展的无服务器产品一样)不会为您提供已知为传出流量来源的专用 IP 地址。另见:

云 运行 服务不获取静态 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

使用包含 IP 或 URL 代理的 PROXY environemnt 变量(参见此处 set an environment variable

对于此代理,您可以:

  • 自己创建,例如使用具有静态 public IP 地址 运行 squid 的 Compute Engine VM,这可能适合 Compute Engine 免费层。
  • 使用提供静态 IP 代理的服务,例如 https://www.quotaguard.com/static-ip/ 起价为 $19/m

我个人使用了第二种解决方案。该服务为我提供了一个包含用户名和密码的 URL,然后我使用上面的代码将其用作代理。

更新(2020 年 10 月): Cloud 运行 现已启动 VPC egress feature that lets you configure a static IP for outbound requests through Cloud NAT. You can follow this step by step guide in the documentation 以在 MongoDB Atlas 上配置静态 IP 以列入白名单。


直到 Cloud 运行 开始支持 Cloud NAT 或无服务器 VPC 访问,很遗憾,这不受支持。

正如@Steren 所提到的,您可以通过 运行 一个 ssh 客户端创建一个 SOCKS 代理,该客户端通过具有静态外部 IP 地址的 GCE VM 实例路由流量。

我在这里写了博客:https://ahmet.im/blog/cloud-run-static-ip/, and you can find step-by-step instructions with a working example at: https://github.com/ahmetb/cloud-run-static-outbound-ip

此功能现在由 Cloud 运行 团队在 beta 中发布:

https://cloud.google.com/run/docs/configuring/static-outbound-ip