重複造輪子,這裏使用從新打包生成一個基於Docker的Hadoop鏡像;
Hadoop集羣依賴的軟件分別爲:jdk、ssh等,因此只要這兩項還有Hadoop相關打包進鏡像中去便可;java
一、Hadoop相關配置文件:core-site.xml、hdfs-site.xml、mapred-site.xml、yarn-site.xml、slaves、hadoop-env.sh
二、ssh配置文件:ssh_config
三、Hadoop集羣啓動文件:start-hadoop.shnode
一、安裝依賴docker
RUN apt-get update && \ apt-get install -y openssh-server openjdk-8-jdk wget
二、下載Hadoop包apache
RUN wget http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.10.0/hadoop-2.10.0.tar.gz && \ tar -xzvf hadoop-2.10.0.tar.gz && \ mv hadoop-2.10.0 /usr/local/hadoop && \ rm hadoop-2.10.0.tar.gz && \ rm /usr/local/hadoop/share/doc -rf
三、配置環境變量網絡
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 ENV HADOOP_HOME=/usr/local/hadoop ENV PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin
四、生成SSH key,用於節點免密登陸架構
RUN ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' && \ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
五、建立Hadoop相關目錄、複製相關配置文件、相關文件添加執行權限,最後格式化namenode節點,各個節點啓動時,啓動ssh服務;ssh
RUN mkdir -p ~/hdfs/namenode && \ mkdir -p ~/hdfs/datanode && \ mkdir $HADOOP_HOME/logs COPY config/* /tmp/ #複製ssh、hadoop配置相關 RUN mv /tmp/ssh_config ~/.ssh/config && \ mv /tmp/hadoop-env.sh /usr/local/hadoop/etc/hadoop/hadoop-env.sh && \ mv /tmp/hdfs-site.xml $HADOOP_HOME/etc/hadoop/hdfs-site.xml && \ mv /tmp/core-site.xml $HADOOP_HOME/etc/hadoop/core-site.xml && \ mv /tmp/mapred-site.xml $HADOOP_HOME/etc/hadoop/mapred-site.xml && \ mv /tmp/yarn-site.xml $HADOOP_HOME/etc/hadoop/yarn-site.xml && \ mv /tmp/slaves $HADOOP_HOME/etc/hadoop/slaves && \ mv /tmp/start-hadoop.sh ~/start-hadoop.sh && \ mv /tmp/run-wordcount.sh ~/run-wordcount.sh #添加執行權限 RUN chmod +x ~/start-hadoop.sh && \ chmod +x ~/run-wordcount.sh && \ chmod +x $HADOOP_HOME/sbin/start-dfs.sh && \ chmod +x $HADOOP_HOME/sbin/start-yarn.sh # format namenode RUN /usr/local/hadoop/bin/hdfs namenode -format
經過上面的Dockerfile生成了鏡像後,這裏便可使用上面所生成的鏡像搭建Hadoop集羣;這裏啓動一個master、兩個slave節點;jvm
添加橋接網絡:oop
docker network create --driver=bridge solinx-hadoop
啓動Master節點:code
docker run -itd --net=solinx-hadoop -p 10070:50070 -p 8088:8088 --name solinx-hadoop-master --hostname solinx-hadoop-master solinx/hadoop:0.1
啓動Slave1節點:
docker run -itd --net=solinx-hadoop --name solinx-hadoop-slave1 --hostname solinx-hadoop-slave1 solinx/hadoop:0.1
啓動Slave2節點:
docker run -itd --net=solinx-hadoop --name solinx-hadoop-slave2 --hostname solinx-hadoop-slave1 solinx/hadoop:0.1
進入Master節點,執行啓動Hadoop集羣腳本便可: