我正在尝试在可以访问GCP资源但不能访问Internet的GCP VM上部署数据流作业。当我尝试运行作业时,出现连接超时错误,如果我尝试连接到Internet,这将是有意义的。该代码中断,因为正在尝试代表apache-beam建立http连接。
Python设置: 在切断虚拟机之前,我使用pip和requirements.txt安装了所有必需的软件包。这似乎行得通,因为代码的其他部分工作正常。
以下是运行代码时收到的错误消息。
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))
after connection broken by 'ConnectTimeoutError(
<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at foo>,
'Connection to pypi.org timed out. (connect timeout=15)')': /simple/apache-beam/
Could not find a version that satisfies the requirement apache-beam==2.9.0 (from versions: )
No matching distribution found for apache-beam==2.9.0
我是否正在运行python作业,是否需要连接到pypi?周围有骇客吗?
答案 0 :(得分:0)
当我们使用启用了私有IP的Google Cloud Composer时,我们将无法访问互联网。
要解决此问题:
答案 1 :(得分:0)
如果您在私有Cloud Composer中运行DataflowPythonOperator,则该作业需要访问Internet才能从映像projects/dataflow-service-producer-prod
下载一组软件包。但是在私有群集中,VM和GKE无法访问Internet。
要解决此问题,您需要创建一个Cloud NAT和一个路由器:https://cloud.google.com/nat/docs/gke-example#step_6_create_a_nat_configuration_using
这将允许您的实例将数据包发送到Internet并接收入站流量。
答案 2 :(得分:0)
TL;DR:将 Apache Beam SDK 存档复制到可访问的路径中,并在 Dataflow 管道中将该路径作为 SetupOption sdk_location 变量提供。
我也为此设置挣扎了很长时间。最后我找到了一个在执行时不需要互联网访问的解决方案。
可能有多种方法可以做到这一点,但以下两种方法相当简单。
作为先决条件,您需要按如下方式创建 apache-beam-sdk 源存档:
切换到所需的标签,例如。 v2.28.0
cd 到 beam/sdks/python
创建所需的 beam_sdk 版本的 tar.gz 源存档,如下所示:
python setup.py sdist
现在您应该在路径 apache-beam-2.28.0.tar.gz
beam/sdks/python/dist/
选项 1 - 使用 Flex 模板并在 Dockerfile 中复制 Apache_Beam_SDK
文档:Google Dataflow Documentation
COPY utils/apache-beam-2.28.0.tar.gz /tmp
,因为这将是您可以在 SetupOptions 中设置的路径。FROM gcr.io/dataflow-templates-base/python3-template-launcher-base
ARG WORKDIR=/dataflow/template
RUN mkdir -p ${WORKDIR}
WORKDIR ${WORKDIR}
# Due to a change in the Apache Beam base image in version 2.24, you must to install
# libffi-dev manually as a dependency. For more information:
# https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4891
# update used packages
RUN apt-get update && apt-get install -y \
libffi-dev \
&& rm -rf /var/lib/apt/lists/*
COPY setup.py .
COPY main.py .
COPY path_to_beam_archive/apache-beam-2.28.0.tar.gz /tmp
ENV FLEX_TEMPLATE_PYTHON_SETUP_FILE="${WORKDIR}/setup.py"
ENV FLEX_TEMPLATE_PYTHON_PY_FILE="${WORKDIR}/main.py"
RUN python -m pip install --user --upgrade pip setuptools wheel
options.view_as(SetupOptions).sdk_location = '/tmp/apache-beam-2.28.0.tar.gz'
gcloud builds submit --tag $TEMPLATE_IMAGE .
gcloud dataflow flex-template build "gs://define-path-to-your-templates/your-flex-template-name.json" \
--image=gcr.io/your-project-id/image-name:tag \
--sdk-language=PYTHON \
--metadata-file=metadata.json
gcloud dataflow flex-template run "your-dataflow-job-name" \
--template-file-gcs-location="gs://define-path-to-your-templates/your-flex-template-name.json" \
--parameters staging_location="gs://your-bucket-path/staging/" \
--parameters temp_location="gs://your-bucket-path/temp/" \
--service-account-email="your-restricted-sa-dataflow@your-project-id.iam.gserviceaccount.com" \
--region="yourRegion" \
--max-workers=6 \
--subnetwork="https://www.googleapis.com/compute/v1/projects/your-project-id/regions/your-region/subnetworks/your-subnetwork" \
--disable-public-ips
选项 2 - 从 GCS 复制 sdk_location
根据 Beam 文档,您甚至应该能够直接为选项 sdk_location
提供 GCS / gs:// 路径,但它对我不起作用。但以下应该有效:
gs://yourbucketname/beam_sdks/apache-beam-2.28.0.tar.gz
/tmp/apache-beam-2.28.0.tar.gz
# see: https://cloud.google.com/storage/docs/samples/storage-download-file
from google.cloud import storage
def download_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
# bucket_name = "your-bucket-name"
# source_blob_name = "storage-object-name"
# destination_file_name = "local/path/to/file"
storage_client = storage.Client()
bucket = storage_client.bucket("gs://your-bucket-name")
# Construct a client side representation of a blob.
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
# any content from Google Cloud Storage. As we don't need additional data,
# using `Bucket.blob` is preferred here.
blob = bucket.blob("gs://your-bucket-name/path/apache-beam-2.28.0.tar.gz")
blob.download_to_filename("/tmp/apache-beam-2.28.0.tar.gz")
options.view_as(SetupOptions).sdk_location = '/tmp/apache-beam-2.28.0.tar.gz'