要用docker搭建集群,首先需要构造集群所需的docker镜像。构建镜像的一种方式是,利用一个已有的镜像比如简单的linux系统,运行一个容器,在容器中手动的安装集群所需要的软件并进行配置,然后commit容器到新的镜像。另一种方式是,使用Dockerfile来自动化的构造镜像。
下面采用第二种.
1. 创建带ssh服务的ubuntu14.04系统镜像
使用ubuntu14系统来安装hadoop和hbase,由于hadoop集群机器之间通过ssh通信,所以需要在ubuntu14系统中安装ssh服务。
编写Dockerfile如下:
############################################ # version : debugman007/ssh:v1 # desc : ubuntu14.04 上安装的ssh ############################################ # 设置继承自ubuntu14.04官方镜像 FROM ubuntu:14.04 # 下面是一些创建者的基本信息 MAINTAINER debugman007 (skc361@163.com) RUN rm -vf /var/lib/apt/lists/* RUN apt-get update RUN apt-get install -y openssh-server openssh-client vim wget curl sudo #添加用户 test,设置密码为 test, 赋予sudo权限 RUN useradd -m test RUN echo "test:test" | chpasswd RUN cd /etc/sudoers.d && touch nopasswdsudo && echo "test ALL=(ALL) ALL" >> nopasswdsudo #把test用户的shell改成bash,否则ssh登录ubuntu服务器,命令行不显示用户名和目录 RUN usermod -s /bin/bash test RUN echo "root:root" | chpasswd #配置ssh RUN mkdir /var/run/sshd RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config RUN sed 's@sessions*requireds*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile EXPOSE 22 USER test RUN ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa RUN cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys USER root RUN ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa RUN cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
创建好的系统镜像位于:https://hub.docker.com/r/debugman007/ubt14-ssh/
Dockerfile 位于:https://github.com/gtarcoder/dockerfiles/blob/master/ubt14-ssh/Dockerfile
2. 创建hadoop和hbase基本镜像
编写Dockerfile如下:
############################################ # version : debugman007/ubt14-hadoop-hbase:v1 # desc : ubuntu上安装的ssh,java, hadoop ############################################ FROM debugman007/ubt14-ssh:v1 # 下面是一些创建者的基本信息 MAINTAINER debugman007 (skc361@163.com) #为hadoop集群提供dns服务 RUN sudo apt-get -y install dnsmasq #安装和配置java环境 #RUN yum -y install java-1.7.0-openjdk* ADD http://mirrors.linuxeye.com/jdk/jdk-7u80-linux-x64.tar.gz /usr/local/ RUN cd /usr/local && tar -zxvf jdk-7u80-linux-x64.tar.gz && rm -f jdk-7u80-linux-x64.tar.gz ENV JAVA_HOME /usr/local/jdk1.7.0_80 ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV PATH $PATH:${JAVA_HOME}/bin #安装和配置 hadoop RUN groupadd hadoop RUN useradd -m hadoop -g hadoop RUN echo "hadoop:hadoop" | chpasswd ADD http://www-eu.apache.org/dist/hadoop/common/hadoop-2.7.3/hadoop-2.7.3.tar.gz /usr/local/ RUN cd /usr/local && tar -zxvf hadoop-2.7.3.tar.gz && rm -f hadoop-2.7.3.tar.gz RUN chown -R hadoop:hadoop /usr/local/hadoop-2.7.3 RUN cd /usr/local && ln -s ./hadoop-2.7.3 hadoop ENV HADOOP_PREFIX /usr/local/hadoop ENV HADOOP_HOME /usr/local/hadoop ENV HADOOP_COMMON_HOME /usr/local/hadoop ENV HADOOP_HDFS_HOME /usr/local/hadoop ENV HADOOP_MAPRED_HOME /usr/local/hadoop ENV HADOOP_YARN_HOME /usr/local/hadoop ENV HADOOP_CONF_DIR /usr/local/hadoop/etc/hadoop ENV PATH ${HADOOP_HOME}/bin:$PATH ADD http://www-eu.apache.org/dist/hbase/1.2.4/hbase-1.2.4-bin.tar.gz /usr/local/ RUN cd /usr/local && tar -zxvf hbase-1.2.4-bin.tar.gz && rm -f hbase-1.2.4-bin.tar.gz RUN chown -R hadoop:hadoop /usr/local/hbase-1.2.4 RUN cd /usr/local && ln -s ./hbase-1.2.4 hbase ENV HBASE_HOME /usr/local/hbase ENV PATH ${HBASE_HOME}/bin:$PATH RUN echo "hadoop ALL= NOPASSWD: ALL" >> /etc/sudoers USER hadoop RUN ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa RUN cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
创建好的镜像位于:https://hub.docker.com/r/debugman007/ubt14-hadoop-hbase/
Dockerfile 位于:https://github.com/gtarcoder/dockerfiles/blob/master/ubt14-hadoop-hbase/Dockerfile
3. 给hadoop,hbase镜像进行配置
Dockerfile 如下:
############################################ # version : debugman007/ubt14-hadoop-hbase:master # desc : ubuntu上安装的ssh,java, hadoop, hbase ############################################ FROM debugman007/ubt14-hadoop-hbase:base # 下面是一些创建者的基本信息 MAINTAINER debugman007 (skc361@163.com) ADD hadoop-env.sh $HADOOP_HOME/etc/hadoop/ ADD mapred-env.sh $HADOOP_HOME/etc/hadoop/ ADD yarn-env.sh $HADOOP_HOME/etc/hadoop/ ADD core-site.xml $HADOOP_HOME/etc/hadoop/ ADD hdfs-site.xml $HADOOP_HOME/etc/hadoop/ ADD mapred-site.xml $HADOOP_HOME/etc/hadoop/ ADD yarn-site.xml $HADOOP_HOME/etc/hadoop/ ADD slaves $HADOOP_HOME/etc/hadoop/ ADD hbase-env.sh $HBASE_HOME/conf/ ADD hbase-site.xml $HBASE_HOME/conf/ ADD regionservers $HBASE_HOME/conf/ USER hadoop RUN sudo mkdir -p /opt/hadoop/data/zookeeper RUN sudo chown -R hadoop:hadoop $HADOOP_HOME/etc/hadoop RUN sudo chown -R hadoop:hadoop $HBASE_HOME/conf RUN sudo chown -R hadoop:hadoop /opt/hadoop RUN sudo chown -R hadoop:hadoop /home/hadoop COPY bootstrap.sh /home/hadoop/ RUN chmod 766 /home/hadoop/bootstrap.sh ENTRYPOINT ["/home/hadoop/bootstrap.sh"] CMD ["/bin/bash"]
除了Dockerfile文件,还需要hadoop和hbase的一些配置文件,包括 core-site.xml, hadoop-env.sh, hbase-env.sh, hbase-site.xml, hdfs-site.xml, mapred-env.sh, regionservers, slaves, yarn-env.sh, yarn-site.xml等。
创建好的镜像位于:https://hub.docker.com/r/debugman007/ubt14-hadoop-hbase/
Dockerfile以及配置文件位于:https://github.com/gtarcoder/dockerfiles/tree/master/ubt14-hadoop-hbase-v1
4. 启动
(1)启动一个容器作为hadoop master节点:docker run -it --name hadoop-master -h hadoop-master -d -P -p 50070:50070 -p 8088:8088 debugman007/ubt14-hadoop-hbase:v1
(2)启动三个容器作为hadop slave节点docker run -it --name hadoop-slave1 -h hadoop-slave1 debugman007/ubt14-hadoop-hbase:v1
三个容器名和容器内的主机名分别设为 hadoop-slave1, hadoop-slave2, hadoop-slave3.
(3)设置各个节点的/etc/hosts文件
编辑一个run_hosts.sh 脚本文件,用来设置各个节点的名称解析。假设hadoop-master容器内的IP地址为10.0.1.2, hadoop-slave1/2/3 的IP地址分别为10.0.1.3/4/5.
#!/bin/bash echo 10.0.1.2 hadoop-master >> /etc/hosts echo 10.0.1.3 hadoop-slave1 >> /etc/hosts echo 10.0.1.4 hadoop-slave2 >> /etc/hosts echo 10.0.1.5 hadoop-slave3 >> /etc/hosts echo 10.0.1.3 regionserver1 >> /etc/hosts #hbase的regionserver服务器 echo 10.0.1.4 regionserver2 >> /etc/hosts
在hadoop-master和hadoop-salve1/2/3容器中都执行该脚本。
(4)进入hadoop-master容器docker exec -it hadoop-master bash
然后在容器内 /usr/local/hadoop/sbin 目录下执行:
hdfs namenode -format
./start-dfs.sh
./start-yarn.sh
启动hadoop服务。
在容器内执行 start-hbase.sh 启动hbase服务。
(5)在 hadoop-master 节点和hadoop-slave节点内查看服务是否正常启动
在各个节点容器内都执行 jps 查看,发现在mster节点中出现
Jps, ResourceManager, NameNode, SecondaryNamenode, HMaster等服务;
在slave节点中出现 Jps, DataNode, NodeManager, HRegionServer等服务。
出现以上服务,说明集群正常启动。
参考:
http://blog.csdn.net/god_wot/article/details/52550657
http://blog.csdn.net/god_wot/article/details/52554313