Dataproc Docker on YARN 功能可讓您建立及使用 Docker 映像檔,自訂 Spark 工作執行階段環境。映像檔可包含 Java、Python 和 R 依附元件的自訂項目,以及工作 JAR。
限制
下列服務不支援這項功能:
- 2.0.49 之前的 Dataproc 映像檔版本 (不適用於 1.5 映像檔)
- MapReduce 工作 (僅適用於 Spark 工作)
- Spark 用戶端模式 (僅支援 Spark 叢集模式)
- Kerberos 叢集:如果建立啟用 Kerberos 的叢集,並在 YARN 上使用 Docker,叢集建立作業會失敗。
- JDK、Hadoop 和 Spark 的自訂項目:系統會使用主機 JDK、Hadoop 和 Spark,而非您的自訂項目。
建立 Docker 映像檔
如要自訂 Spark 環境,第一步是建構 Docker 映像檔。
Dockerfile
您可以將下列 Dockerfile 做為範例,並視需要進行變更和新增內容。
FROM debian:10-slim
# Suppress interactive prompts.
ENV DEBIAN_FRONTEND=noninteractive
# Required: Install utilities required by Spark scripts.
RUN apt update && apt install -y procps tini
# Optional: Add extra jars.
ENV SPARK_EXTRA_JARS_DIR=/opt/spark/jars/
ENV SPARK_EXTRA_CLASSPATH='/opt/spark/jars/*'
RUN mkdir -p "${SPARK_EXTRA_JARS_DIR}"
COPY *.jar "${SPARK_EXTRA_JARS_DIR}"
# Optional: Install and configure Miniconda3.
ENV CONDA_HOME=/opt/miniconda3
ENV PYSPARK_PYTHON=${CONDA_HOME}/bin/python
ENV PYSPARK_DRIVER_PYTHON=${CONDA_HOME}/bin/python
ENV PATH=${CONDA_HOME}/bin:${PATH}
COPY Miniconda3-py39_4.10.3-Linux-x86_64.sh .
RUN bash Miniconda3-py39_4.10.3-Linux-x86_64.sh -b -p /opt/miniconda3 \
&& ${CONDA_HOME}/bin/conda config --system --set always_yes True \
&& ${CONDA_HOME}/bin/conda config --system --set auto_update_conda False \
&& ${CONDA_HOME}/bin/conda config --system --prepend channels conda-forge \
&& ${CONDA_HOME}/bin/conda config --system --set channel_priority strict
# Optional: Install Conda packages.
#
# The following packages are installed in the default image. It is strongly
# recommended to include all of them.
#
# Use mamba to install packages quickly.
RUN ${CONDA_HOME}/bin/conda install mamba -n base -c conda-forge \
&& ${CONDA_HOME}/bin/mamba install \
conda \
cython \
fastavro \
fastparquet \
gcsfs \
google-cloud-bigquery-storage \
google-cloud-bigquery[pandas] \
google-cloud-bigtable \
google-cloud-container \
google-cloud-datacatalog \
google-cloud-dataproc \
google-cloud-datastore \
google-cloud-language \
google-cloud-logging \
google-cloud-monitoring \
google-cloud-pubsub \
google-cloud-redis \
google-cloud-spanner \
google-cloud-speech \
google-cloud-storage \
google-cloud-texttospeech \
google-cloud-translate \
google-cloud-vision \
koalas \
matplotlib \
nltk \
numba \
numpy \
openblas \
orc \
pandas \
pyarrow \
pysal \
pytables \
python \
regex \
requests \
rtree \
scikit-image \
scikit-learn \
scipy \
seaborn \
sqlalchemy \
sympy \
virtualenv
# Optional: Add extra Python modules.
ENV PYTHONPATH=/opt/python/packages
RUN mkdir -p "${PYTHONPATH}"
COPY test_util.py "${PYTHONPATH}"
# Required: Create the 'yarn_docker_user' group/user.
# The GID and UID must be 1099. Home directory is required.
RUN groupadd -g 1099 yarn_docker_user
RUN useradd -u 1099 -g 1099 -d /home/yarn_docker_user -m yarn_docker_user
USER yarn_docker_user
建構及推送映像檔
以下是建構及推送範例 Docker 映像檔的指令,您可以根據自訂項目進行變更。
# Increase the version number when there is a change to avoid referencing
# a cached older image. Avoid reusing the version number, including the default
# `latest` version.
IMAGE=gcr.io/my-project/my-image:1.0.1
# Download the BigQuery connector.
gcloud storage cp \
gs://spark-lib/bigquery/spark-bigquery-with-dependencies_2.12-0.22.2.jar .
# Download the Miniconda3 installer.
wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.10.3-Linux-x86_64.sh
# Python module example:
cat >test_util.py <<EOF
def hello(name):
print("hello {}".format(name))
def read_lines(path):
with open(path) as f:
return f.readlines()
EOF
# Build and push the image.
docker build -t "${IMAGE}" .
docker push "${IMAGE}"
建立 Dataproc 叢集
建立 Docker 映像檔來自訂 Spark 環境後,請建立 Dataproc 叢集,在執行 Spark 工作時使用 Docker 映像檔。
gcloud
gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --image-version=DP_IMAGE \ --optional-components=DOCKER \ --properties=dataproc:yarn.docker.enable=true,dataproc:yarn.docker.image=DOCKER_IMAGE \ other flags
取代下列項目:
- CLUSTER_NAME:叢集名稱。
- REGION:叢集區域。
- DP_IMAGE:Dataproc 映像檔版本必須為
2.0.49
以上版本 (--image-version=2.0
會使用2.0.49
之後的合格次要版本)。 --optional-components=DOCKER
:在叢集上啟用 Docker 元件。--properties
flag:dataproc:yarn.docker.enable=true
:啟用 Dataproc Docker on YARN 功能時的必要屬性。dataproc:yarn.docker.image
:選用屬性,可新增此屬性來指定 DOCKER_IMAGE,格式如下:{hostname}/{project-id}/{image}:{tag}
。範例:
dataproc:yarn.docker.image=gcr.io/project-id/image:1.0.1
必要條件:您必須透過 Container Registry 或 Artifact Registry 託管 Docker 映像檔。(Dataproc 無法從其他登錄檔擷取容器)。
建議:建立叢集時新增這項屬性,即可快取 Docker 映像檔,避免日後提交使用該映像檔的作業時發生 YARN 超時問題。
將 dataproc:yarn.docker.enable
設為 true
時,Dataproc 會更新 Hadoop 和 Spark 設定,在叢集中啟用 Docker on YARN 功能。舉例來說,spark.submit.deployMode
設為 cluster
,而 spark.yarn.appMasterEnv.YARN_CONTAINER_RUNTIME_DOCKER_MOUNTS
和 spark.executorEnv.YARN_CONTAINER_RUNTIME_DOCKER_MOUNTS
則設為將主機的目錄掛接至容器。
將 Spark 工作提交至叢集
建立 Dataproc 叢集後,請將使用 Docker 映像檔的 Spark 工作提交至叢集。本節的範例會將 PySpark 工作提交至叢集。
設定工作屬性:
# Set the Docker image URI.
IMAGE=(e.g., gcr.io/my-project/my-image:1.0.1)
# Required: Use `#` as the delimiter for properties to avoid conflicts.
JOB_PROPERTIES='^#^'
# Required: Set Spark properties with the Docker image.
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.yarn.appMasterEnv.YARN_CONTAINER_RUNTIME_DOCKER_IMAGE=${IMAGE}"
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.executorEnv.YARN_CONTAINER_RUNTIME_DOCKER_IMAGE=${IMAGE}"
# Optional: Add custom jars to Spark classpath. Don't set these properties if
# there are no customizations.
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.driver.extraClassPath=/opt/spark/jars/*"
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.executor.extraClassPath=/opt/spark/jars/*"
# Optional: Set custom PySpark Python path only if there are customizations.
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.pyspark.python=/opt/miniconda3/bin/python"
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.pyspark.driver.python=/opt/miniconda3/bin/python"
# Optional: Set custom Python module path only if there are customizations.
# Since the `PYTHONPATH` environment variable defined in the Dockerfile is
# overridden by Spark, it must be set as a job property.
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.yarn.appMasterEnv.PYTHONPATH=/opt/python/packages"
JOB_PROPERTIES="${JOB_PROPERTIES}#spark.executorEnv.PYTHONPATH=/opt/python/packages"
注意:
- 如要瞭解相關屬性,請參閱「使用 Docker 容器啟動應用程式」。
gcloud
將工作提交至叢集。
gcloud dataproc jobs submit pyspark PYFILE \ --cluster=CLUSTER_NAME \ --region=REGION \ --properties=${JOB_PROPERTIES}
取代下列項目:
- PYFILE:PySpark 工作檔案的路徑。可以是本機檔案路徑,也可以是 Cloud Storage 中檔案的 URI (
gs://BUCKET_NAME/PySpark filename
)。 - CLUSTER_NAME:叢集名稱。
- REGION:叢集區域。