利用docker快速搭建Spark集群

96
RavenZZ
2017.04.10 23:30 字数 601

适用人群

  • 正在使用spark的开发者
  • 正在学习docker或者spark的开发者

准备工作

  1. 安装docker
  2. (可选)下载java和spark with hadoop

Spark集群

Spark运行时架构图


Spark Cluster(Spark集群).png

如上图: Spark集群由以下两个部分组成

  1. 集群管理器(Mesos, Yarn或者standalone Mode)
  2. 工作节点(worker)

如何docker化(本例使用Standalone模式)

  1. 将spark集群拆分

    • base(基础镜像)
    • master(主节点镜像)
    • worker(工作镜像)
  2. 编写base Dockerfile

    注: 为方便切换版本基础镜像选择的是centos, 所以要下载java和spark, 方便调试, 可以下载好安装文件后本地搭建一个静态文件服务器, 使用Node.js 的http-server可以快速搞定,命令如下

     npm install http-server -g
     http-server -p 54321 ~/Downloads

    正式开始写Dockerfile

    FROM centos:7
    MAINTAINER RavenZZ <raven.zhu@outlook.com>
    
    # 安装系统工具
    RUN yum update -y
    RUN yum upgrade -y
    RUN yum install -y byobu curl htop man unzip nano wget
    RUN yum clean all
    
    # 安装 Java
    ENV JDK_VERSION 8u11
    ENV JDK_BUILD_VERSION b12
    # 如果网速快,可以直接从源站下载
    #RUN curl -LO "http://download.oracle.com/otn-pub/java/jdk/$JDK_VERSION-$JDK_BUILD_VERSION/jdk-$JDK_VERSION-linux-x64.rpm" -H 'Cookie: oraclelicense=accept-securebackup-cookie' && rpm -i jdk-$JDK_VERSION-linux-x64.rpm; rm -f jdk-$JDK_VERSION-linux-x64.rpm;
    RUN curl -LO "http://192.168.199.102:54321/jdk-8u11-linux-x64.rpm" && rpm -i jdk-$JDK_VERSION-linux-x64.rpm; rm -f jdk-$JDK_VERSION-linux-x64.rpm;
    ENV JAVA_HOME /usr/java/default
    RUN yum remove curl;  yum clean all
    WORKDIR spark
    
    RUN \
     curl -LO 'http://192.168.199.102:54321/spark-2.1.0-bin-hadoop2.7.tgz' && \
     tar zxf spark-2.1.0-bin-hadoop2.7.tgz
    
    RUN rm -rf spark-2.1.0-bin-hadoop2.7.tgz
    RUN mv spark-2.1.0-bin-hadoop2.7/* ./
    
    ENV SPARK_HOME /spark
    ENV PATH /spark/bin:$PATH
    ENV PATH /spark/sbin:$PATH
  3. 编写master Dockerfile

    FROM ravenzz/spark-hadoop
    
    MAINTAINER RavenZZ <raven.zhu@outlook.com>
    
    COPY master.sh /
    
    ENV SPARK_MASTER_PORT 7077
    ENV SPARK_MASTER_WEBUI_PORT 8080
    ENV SPARK_MASTER_LOG /spark/logs
    
    EXPOSE 8080 7077 6066
    
    CMD ["/bin/bash","/master.sh"]
  4. 编写worker Dockerfile

     FROM ravenzz/spark-hadoop
    
     MAINTAINER RavenZZ <raven.zhu@outlook.com> 
     COPY worker.sh /
    
     ENV SPARK_WORKER_WEBUI_PORT 8081
     ENV SPARK_WORKER_LOG /spark/logs
     ENV SPARK_MASTER "spark://spark-master:32769"
    
     EXPOSE 8081
    
     CMD ["/bin/bash","/worker.sh"]
  5. docker-compose

     version: '3'
    
    services:
     spark-master:
       build:
         context: ./master
         dockerfile: Dockerfile
       ports:
         - "50001:6066"
         - "50002:7077"   # SPARK_MASTER_PORT
         - "50003:8080"   # SPARK_MASTER_WEBUI_PORT
       expose:
         - 7077
    
     spark-worker1:
       build:
         context: ./worker
         dockerfile: Dockerfile
       ports:
         - "50004:8081"
       links:
         - spark-master
       environment:
         - SPARK_MASTER=spark://spark-master:7077
    
     spark-worker2:
       build:
         context: ./worker
         dockerfile: Dockerfile
       ports:
         - "50005:8081"
       links:
         - spark-master
       environment:
         - SPARK_MASTER=spark://spark-master:7077
  6. 测试集群

    docker-compose up

    访问http://localhost:50003/ 结果如图


Paste_Image.png

参考链接

本例源代码

Diary