【发布时间】:2021-08-16 11:34:09
【问题描述】:
我正在关注this 链接以创建火花集群。我能够运行火花集群。但是,我必须给出启动spark-shell 的绝对路径。我正在尝试设置环境变量,即PATH 和start-shell.sh 中的其他一些变量。但是,它没有在容器内设置。我尝试在容器内使用printenv 打印它。但这些变量永远不会反映。
我是否试图错误地设置环境变量? Spark 集群运行成功。
我正在使用 docker-compose.yml 来构建和重新创建图像和容器。
docker-compose up --build
Dockerfile
# builder step used to download and configure spark environment
FROM openjdk:11.0.11-jre-slim-buster as builder
# Add Dependencies for PySpark
RUN apt-get update && apt-get install -y curl vim wget software-properties-common ssh net-tools ca-certificates python3 python3-pip python3-numpy python3-matplotlib python3-scipy python3-pandas python3-simpy
# JDBC driver download and install
ADD https://go.microsoft.com/fwlink/?linkid=2168494 /usr/share/java
RUN update-alternatives --install "/usr/bin/python" "python" "$(which python3)" 1
# Fix the value of PYTHONHASHSEED
# Note: this is needed when you use Python 3.3 or greater
ENV SPARK_VERSION=3.1.2 \
HADOOP_VERSION=3.2 \
SPARK_HOME=/opt/spark \
PYTHONHASHSEED=1
# Download and uncompress spark from the apache archive
RUN wget --no-verbose -O apache-spark.tgz "https://archive.apache.org/dist/spark/spark-${SPARK_VERSION}/spark-${SPARK_VERSION}-bin-hadoop${HADOOP_VERSION}.tgz" \
&& mkdir -p ${SPARK_HOME} \
&& tar -xf apache-spark.tgz -C ${SPARK_HOME} --strip-components=1 \
&& rm apache-spark.tgz
我的 Dockerfile-spark
在 Dockerfile 的 ENV 下使用 SPARK_BIN="${SPARK_HOME}/bin/ 时,设置了环境变量。使用printenv在docker容器内可见
FROM apache-spark
WORKDIR ${SPARK_HOME}
ENV SPARK_MASTER_PORT=7077 \
SPARK_MASTER_WEBUI_PORT=8080 \
SPARK_LOG_DIR=${SPARK_HOME}/logs \
SPARK_MASTER_LOG=${SPARK_HOME}/logs/spark-master.out \
SPARK_WORKER_LOG=${SPARK_HOME}/logs/spark-worker.out \
SPARK_WORKER_WEBUI_PORT=8080 \
SPARK_MASTER="spark://spark-master:7077" \
SPARK_WORKLOAD="master"
COPY start-spark.sh /
CMD ["/bin/bash", "/start-spark.sh"]
start-spark.sh
#!/bin/bash
. "$SPARK_HOME/bin/load-spark-env.sh"
export SPARK_BIN="${SPARK_HOME}/bin/" # This doesn't work here
export PATH="${SPARK_HOME}/bin/:${PATH}" # This doesn't work here
# When the spark work_load is master run class org.apache.spark.deploy.master.Master
if [ "$SPARK_WORKLOAD" == "master" ];
then
export SPARK_MASTER_HOST=`hostname` # This works here
cd $SPARK_BIN && ./spark-class org.apache.spark.deploy.master.Master --ip $SPARK_MASTER_HOST --port $SPARK_MASTER_PORT --webui-port $SPARK_MASTER_WEBUI_PORT >> $SPARK_MASTER_LOG.
我的文件结构是
- 码头文件
- dockerfile-spark # 使用 dockerfile 创建的预构建镜像
- start-spark.sh # 调用购买 dockerfile-spark
- docker-compose.yml # 使用 build 参数从 dockerfile-spark 构建镜像
从主容器内部
root@3abbd4508121:/opt/spark# export
declare -x HADOOP_VERSION="3.2"
declare -x HOME="/root"
declare -x HOSTNAME="3abbd4508121"
declare -x JAVA_HOME="/usr/local/openjdk-11"
declare -x JAVA_VERSION="11.0.11+9"
declare -x LANG="C.UTF-8"
declare -x OLDPWD
declare -x PATH="/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/opt/spark"
declare -x PYTHONHASHSEED="1"
declare -x SHLVL="1"
declare -x SPARK_HOME="/opt/spark"
declare -x SPARK_LOCAL_IP="spark-master"
declare -x SPARK_LOG_DIR="/opt/spark/logs"
declare -x SPARK_MASTER="spark://spark-master:7077"
declare -x SPARK_MASTER_LOG="/opt/spark/logs/spark-master.out"
declare -x SPARK_MASTER_PORT="7077"
declare -x SPARK_MASTER_WEBUI_PORT="8080"
declare -x SPARK_VERSION="3.1.2"
declare -x SPARK_WORKER_LOG="/opt/spark/logs/spark-worker.out"
declare -x SPARK_WORKER_WEBUI_PORT="8080"
declare -x SPARK_WORKLOAD="master"
declare -x TERM="xterm"
root@3abbd4508121:/opt/spark#
【问题讨论】:
-
是什么让您认为没有设置环境变量?您显示的任何内容实际上都没有包含
printenv电话。 -
@DavidMaze 在 DockerFile 中使用
SPARK_BIN="${SPARK_HOME}/bin/" andPATH="${SPARK_HOME}/bin/:${PATH}"` 时,它可以在start-spark.sh中使用它们。它不起作用有人告诉我使用. /start-spark.sh由于 shell 全局与本地上下文。但是它抛出了不同的错误。 -
你说的“容器内”的几个地方,你是怎么到那里的? (
docker exec调试 shell 不会看到您在入口点包装脚本中设置的环境变量。) -
我使用
docker exec -it sparkdocs_spark-master_1 /bin/bash进入容器外壳
标签: docker apache-spark docker-compose environment-variables sh