התאמה אישית של סביבת זמן הריצה של משימת Spark באמצעות Docker ב-YARN

התכונה Docker on YARN ב-Managed Service for Apache Spark מאפשרת ליצור קובץ אימג' של Docker ולהשתמש בו כדי להתאים אישית את סביבת זמן הריצה של משימת Spark. התמונה יכולה לכלול התאמות אישיות של יחסי תלות של Java,‏ Python ו-R, ושל קובץ ה-JAR של המשימה.

מגבלות

התכונה או התמיכה לא זמינות:

  • גרסאות קודמות ל-2.0.49 של Managed Service for Apache Spark (לא זמין בגרסאות 1.5)
  • משימות MapReduce (נתמכות רק במשימות Spark)
  • מצב לקוח של Spark (נתמך רק במצב אשכול של Spark)
  • אשכולות Kerberos: יצירת האשכול נכשלת אם יוצרים אשכול עם Docker ב-YARN ו-Kerberos מופעל.
  • התאמות אישיות של JDK,‏ Hadoop ו-Spark: המערכת משתמשת ב-JDK,‏ Hadoop ו-Spark של המארח, ולא בהתאמות האישיות שלכם.

יצירת קובץ אימג' של Docker

השלב הראשון בהתאמה אישית של סביבת Spark הוא ליצור קובץ אימג' של Docker.

קובץ Docker

אפשר להשתמש בקובץ ה-Docker הבא כדוגמה, ולבצע שינויים ותוספות לפי הצורך.

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}"

יצירת אשכול ב-Managed Service for Apache Spark

אחרי שיוצרים קובץ אימג' של Docker שמתאים אישית את סביבת Spark, יוצרים אשכול Managed Service for Apache Spark שישתמש בקובץ האימג' של Docker כשמריצים משימות Spark.

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: גרסת האימג' של Managed Service for Apache Spark צריכה להיות 2.0.49 או גרסה חדשה יותר (--image-version=2.0 ישתמש בגרסה משנית מתאימה שחדשה יותר מ-2.0.49).
  • ‫--optional-components=DOCKER: הפעלת רכיב Docker באשכול.
  • הדגל --properties:
    • ‫dataproc:yarn.docker.enable=true: מאפיין חובה להפעלת התכונה Managed Service for Apache Spark Docker on YARN.
    • ‫dataproc:yarn.docker.image: מאפיין אופציונלי שאפשר להוסיף כדי לציין את DOCKER_IMAGE באמצעות הפורמט הבא למתן שמות לתמונות ב-Container Registry: ‏{hostname}/{project-id}/{image}:{tag}.

      דוגמה:

      dataproc:yarn.docker.image=gcr.io/project-id/image:1.0.1
      

      דרישה: אתם צריכים לארח את קובץ האימג' של Docker ב-Container Registry או ב-Artifact Registry. ‫(Managed Service for Apache Spark לא יכול לאחזר קונטיינרים ממאגרים אחרים).

      המלצה: כדאי להוסיף את המאפיין הזה כשיוצרים את האשכול כדי לשמור במטמון את קובץ האימג' של 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 לאשכול

אחרי שיוצרים אשכול של Managed Service for Apache Spark, שולחים לאשכול משימת Spark שמשתמשת בקובץ האימג' של Docker. בדוגמה שבקטע הזה נשלחת משימת 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"

הערות:

gcloud

שולחים את המשימה לאשכול.

gcloud dataproc jobs submit pyspark PYFILE \
    --cluster=CLUSTER_NAME \
    --region=REGION \
    --properties=${JOB_PROPERTIES}

מחליפים את הערכים בשדות הבאים:

  • ‫PYFILE: הנתיב לקובץ של משימת PySpark. הוא יכול להיות נתיב של קובץ מקומי או ה-URI של הקובץ ב-Cloud Storage‏ (gs://BUCKET_NAME/PySpark filename).
  • ‫CLUSTER_NAME: שם האשכול.
  • ‫REGION: האזור של האשכול.