如何在私有子网上运行Dataflow python?

时间:2017-09-08 18:33:29

标签: python google-cloud-dataflow apache-beam

Apache Beam 2.1.0增加了对在私有子网上的Dataflow运行器上提交作业以及没有公共IP的支持,我们需要这些IP来满足防火墙规则。我计划使用squid代理访问apt-getpip等来安装python依赖项;代理实例已在运行,我们在setup.py脚本中设置了代理。

python $DIR/submit.py \
       --runner DataflowRunner \
       --no_use_public_ips \
       --subnetwork regions/us-central1/subnetworks/$PRIVATESUBNET \
       --staging_location $BUCKET/staging \
       --temp_location $BUCKET/temp \
       --project $PROJECT \
       --setup_file $DIR/setup.py \
       --job_name $JOB_NAME

当我尝试通过python API运行时,我在工作启动期间出错,然后才有机会启用代理。在我看来,每个工作人员首先尝试安装数据流sdk:

install_dataflow_sdk

并且在此期间它尝试更新requests并且无法连接到pip

enter image description here

此时我的代码都没有被执行,所以在设置代理之前我无法找到避免此错误的方法。有没有办法在私有子网上启动dataflow python worker?

2 个答案:

答案 0 :(得分:3)

我设法用NAT网关而不是代理来解决这个问题。继special configurations下的说明 - 我必须编辑其中一个步骤,通过网关自动路由Dataflow工作者实例:

gcloud compute routes create no-ip-internet-route --network my-network \
    --destination-range 0.0.0.0/0 \
    --next-hop-instance nat-gateway \
    --next-hop-instance-zone us-central1-a \
    --tags dataflow --priority 800

我使用了标记dataflow而不是no-ip,这是所有数据流工作者的网络标记。

在这种情况下,NAT网关似乎比代理更容易解决,因为它将在不必配置工作人员的情况下路由流量。

答案 1 :(得分:0)

现在仍可以使用 Cloud NAT 完成此操作,如下所示:

$REGION_ID 是任何 GCP 区域,例如 us-central1

gcloud compute routers create nat-router \
       --network=$NETWORK_NAME \
       --region=$REGION_ID

gcloud compute routers nats create nat-config \
   --router=nat-router \
   --nat-custom-subnet-ip-ranges=$SUBNET \
   --auto-allocate-nat-external-ips \
   --region=$REGION_ID

如果您需要为 Cloud NAT 分配一个静态 IP 地址(也许是为了将 NAT IP 地址列入防火墙规则中的白名单),您也可以这样做:

gcloud compute addresses create nat-ip-address --network=$NETWORK_NAME

gcloud compute routers nats create nat-config \
   --router=nat-router \
   --nat-custom-subnet-ip-ranges=$SUBNET \
   --nat-external-ip-pool=nat-ip-address # from above
   --region=$REGION_ID

资源: Creating Cloud NAT instance