美文网首页我爱编程
使用docker搭建hadoop集群

使用docker搭建hadoop集群

作者: Gavin_hello | 来源:发表于2018-02-24 17:15 被阅读0次

    一:环境准备:

    1. 首先要有一个Centos7操作系统,可以在虚拟机中安装。
    2. 在centos7中安装docker,docker的版本为1.8.2
    安装步骤如下: 
    <1>安装制定版本的dockeryum install -y docker-1.8.2-10.el7.centos 
    <2>安装的时候可能会报错,需要安装这个依赖
    rpm -e lvm2-7:2.02.105-14.el7.x86_64
    

    启动docker:

    root@gavin:/zhangkun/docker# service docker start
    
    1. 关闭防火墙并重启系统:
    root@gavin:/zhangkun/docker# systemctl stop firewalld
    root@gavin:/zhangkun/docker# systemctl disable firewalld
    注意:执行完上面的命令之后需要重启系统
    reboot -h(重启系统)
    
    1. 关闭selinux
    1:setenforce 0(立刻生效,不需要重启操作系统)
    2:修改/etc/selinux/config文件中的SELINUX=disabled ,然后重启系统生效
    建议两个步骤都执行,这样可以确保系统重启之后selinux也是关闭状态
    

    二:构建一个hadoop的基础镜像,使用dockerfile文件方式进行构建

    1. 先构建一个具备ssh功能的镜像,方便后期使用
    • 创建目录和Dockerfile

    root@gavin:/zhangkun/docker# mkdir centos-ssh-root
    root@gavin:/zhangkun/docker# cd centos-ssh-root
    root@gavin:/zhangkun/docker/centos-ssh-root# vim Dockerfile

    Dockerfile内容如下:
    
    # 选择一个已有的os镜像作为基础  
    FROM centos 
    
    # 镜像的作者  
    MAINTAINER gavin
    
    # 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no  
    RUN yum install -y openssh-server sudo  
    RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config  
    #安装openssh-clients
    RUN yum  install -y openssh-clients
    
    # 添加测试用户root,密码wjht2014,并且将此用户添加到sudoers里  
    RUN echo "root:wjht2014" | chpasswd  
    RUN echo "root   ALL=(ALL)       ALL" >> /etc/sudoers  
    # 下面这两句比较特殊,在centos6上必须要有,否则创建出来的容器sshd不能登录  
    RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key  
    RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  
    
    # 启动sshd服务并且暴露22端口  
    RUN mkdir /var/run/sshd  
    EXPOSE 22  
    CMD ["/usr/sbin/sshd", "-D"]
    
    
    • 执行docker build 命令

    root@gavin:/zhangkun/docker/centos-ssh-root# docker build -t gavin/centos-ssh-root:1.0 .

    结果如下:
    Sending build context to Docker daemon  2.56 kB
    Step 1 : FROM centos
     ---> a8493f5f50ff
    Step 2 : MAINTAINER gavin
     ---> Running in 61eb94a2295e
     ---> 699e5edbbe6c
    Removing intermediate container 61eb94a2295e
    Step 3 : RUN yum install -y openssh-server sudo
     ---> Running in c0b03b62bc6a
    Loaded plugins: fastestmirror, ovl
    Determining fastest mirrors
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package openssh-server.x86_64 0:7.4p1-13.el7_4 will be installed
    --> Processing Dependency: openssh = 7.4p1-13.el7_4 for package: openssh-server-7.4p1-13.el7_4.x86_64
    --> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-7.4p1-13.el7_4.x86_64
    --> Processing Dependency: libcrypto.so.10(OPENSSL_1.0.2)(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
    --> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
    --> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-7.4p1-13.el7_4.x86_64
    ---> Package sudo.x86_64 0:1.8.19p2-11.el7_4 will be installed
    --> Running transaction check
    ---> Package fipscheck-lib.x86_64 0:1.4.1-6.el7 will be installed
    --> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-6.el7.x86_64
    ---> Package openssh.x86_64 0:7.4p1-13.el7_4 will be installed
    ---> Package openssl-libs.x86_64 1:1.0.1e-60.el7_3.1 will be updated
    ---> Package openssl-libs.x86_64 1:1.0.2k-8.el7 will be an update
    ---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
    --> Running transaction check
    ---> Package fipscheck.x86_64 0:1.4.1-6.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package                Arch        Version                  Repository    Size
    ================================================================================
    Installing:
     openssh-server         x86_64      7.4p1-13.el7_4           updates      458 k
     sudo                   x86_64      1.8.19p2-11.el7_4        updates      1.1 M
    Installing for dependencies:
     fipscheck              x86_64      1.4.1-6.el7              base          21 k
     fipscheck-lib          x86_64      1.4.1-6.el7              base          11 k
     openssh                x86_64      7.4p1-13.el7_4           updates      509 k
     tcp_wrappers-libs      x86_64      7.6-77.el7               base          66 k
    Updating for dependencies:
     openssl-libs           x86_64      1:1.0.2k-8.el7           base         1.2 M
    
    Transaction Summary
    ================================================================================
    Install  2 Packages (+4 Dependent packages)
    Upgrade             ( 1 Dependent package)
    
    Total download size: 3.3 M
    Downloading packages:
    Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
    Public key for fipscheck-lib-1.4.1-6.el7.x86_64.rpm is not installed
    Public key for openssh-server-7.4p1-13.el7_4.x86_64.rpm is not installed
    --------------------------------------------------------------------------------
    Total                                              1.6 MB/s | 3.3 MB  00:02     
    Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    Importing GPG key 0xF4A80EB5:
     Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
     Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
     Package    : centos-release-7-3.1611.el7.centos.x86_64 (@CentOS)
     From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Updating   : 1:openssl-libs-1.0.2k-8.el7.x86_64                           1/8 
      Installing : fipscheck-1.4.1-6.el7.x86_64                                 2/8 
      Installing : fipscheck-lib-1.4.1-6.el7.x86_64                             3/8 
      Installing : openssh-7.4p1-13.el7_4.x86_64                                4/8 
      Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          5/8 
      Installing : openssh-server-7.4p1-13.el7_4.x86_64                         6/8 
      Installing : sudo-1.8.19p2-11.el7_4.x86_64                                7/8 
      Cleanup    : 1:openssl-libs-1.0.1e-60.el7_3.1.x86_64                      8/8 
      Verifying  : fipscheck-lib-1.4.1-6.el7.x86_64                             1/8 
      Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          2/8 
      Verifying  : fipscheck-1.4.1-6.el7.x86_64                                 3/8 
      Verifying  : openssh-7.4p1-13.el7_4.x86_64                                4/8 
      Verifying  : openssh-server-7.4p1-13.el7_4.x86_64                         5/8 
      Verifying  : 1:openssl-libs-1.0.2k-8.el7.x86_64                           6/8 
      Verifying  : sudo-1.8.19p2-11.el7_4.x86_64                                7/8 
      Verifying  : 1:openssl-libs-1.0.1e-60.el7_3.1.x86_64                      8/8 
    
    Installed:
      openssh-server.x86_64 0:7.4p1-13.el7_4     sudo.x86_64 0:1.8.19p2-11.el7_4    
    
    Dependency Installed:
      fipscheck.x86_64 0:1.4.1-6.el7      fipscheck-lib.x86_64 0:1.4.1-6.el7       
      openssh.x86_64 0:7.4p1-13.el7_4     tcp_wrappers-libs.x86_64 0:7.6-77.el7    
    
    Dependency Updated:
      openssl-libs.x86_64 1:1.0.2k-8.el7                                            
    
    Complete!
     ---> c996aae83415
    Removing intermediate container c0b03b62bc6a
    Step 4 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
     ---> Running in 1dfac6d4e6c4
     ---> 2659da5a35a8
    Removing intermediate container 1dfac6d4e6c4
    Step 5 : RUN yum  install -y openssh-clients
     ---> Running in aad53bc05a7b
    Loaded plugins: fastestmirror, ovl
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package openssh-clients.x86_64 0:7.4p1-13.el7_4 will be installed
    --> Processing Dependency: libedit.so.0()(64bit) for package: openssh-clients-7.4p1-13.el7_4.x86_64
    --> Running transaction check
    ---> Package libedit.x86_64 0:3.0-12.20121213cvs.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package             Arch       Version                       Repository   Size
    ================================================================================
    Installing:
     openssh-clients     x86_64     7.4p1-13.el7_4                updates     654 k
    Installing for dependencies:
     libedit             x86_64     3.0-12.20121213cvs.el7        base         92 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package (+1 Dependent package)
    
    Total download size: 746 k
    Installed size: 2.8 M
    Downloading packages:
    --------------------------------------------------------------------------------
    Total                                              2.0 MB/s | 746 kB  00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : libedit-3.0-12.20121213cvs.el7.x86_64                        1/2 
      Installing : openssh-clients-7.4p1-13.el7_4.x86_64                        2/2 
      Verifying  : libedit-3.0-12.20121213cvs.el7.x86_64                        1/2 
      Verifying  : openssh-clients-7.4p1-13.el7_4.x86_64                        2/2 
    
    Installed:
      openssh-clients.x86_64 0:7.4p1-13.el7_4                                       
    
    Dependency Installed:
      libedit.x86_64 0:3.0-12.20121213cvs.el7                                       
    
    Complete!
     ---> 1fd66ba4a305
    Removing intermediate container aad53bc05a7b
    Step 6 : RUN echo "root:wjht2014" | chpasswd
     ---> Running in f1386389be9d
     ---> 11efd1c6468e
    Removing intermediate container f1386389be9d
    Step 7 : RUN echo "root   ALL=(ALL)       ALL" >> /etc/sudoers
     ---> Running in f2fa0bf8f0ff
     ---> 66f1dbf23fcb
    Removing intermediate container f2fa0bf8f0ff
    Step 8 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
     ---> Running in f200ca8c6f74
    Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private dsa key pair.
    Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
    Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
    The key fingerprint is:
    SHA256:JXnM8q/diLBuHm4imfhwLXuI1LijSKSkPfAXWwf5V+E root@f85c553c1496
    The key's randomart image is:
    +---[DSA 1024]----+
    |            .    |
    |       . + . .   |
    |      o + = E    |
    |       o * .     |
    |.o o. . S o      |
    |=+o .= . . .     |
    |oo=+=+. o   .    |
    |o ===oo.o+ + o   |
    |o. ooo *= o o .  |
    +----[SHA256]-----+
     ---> e2e0212a4a62
    Removing intermediate container f200ca8c6f74
    Step 9 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
     ---> Running in 13e6e8697fae
    Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key pair.
    Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
    Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
    The key fingerprint is:
    SHA256:CP3IFXtxF21OCa3DQ8pqXCT/P0t4laSMnFhlylIBbMY root@f85c553c1496
    The key's randomart image is:
    +---[RSA 2048]----+
    |        +.ooo+=o.|
    |     .   Eo++o o+|
    |    . . =.*++ .= |
    |     o = .==+=o o|
    |      + S.o+.oo..|
    |         +   .. .|
    |        .    ..o |
    |              oo |
    |               .o|
    +----[SHA256]-----+
     ---> 62506c433eda
    Removing intermediate container 13e6e8697fae
    Step 10 : RUN mkdir /var/run/sshd
     ---> Running in 3db5261c4757
     ---> cec11f2ba2d8
    Removing intermediate container 3db5261c4757
    Step 11 : EXPOSE 22
     ---> Running in bd793050ef89
     ---> d8afedd8edc3
    Removing intermediate container bd793050ef89
    Step 12 : CMD /usr/sbin/sshd -D
     ---> Running in c44d33719c82
     ---> 7fe06a0a3cf2
    Removing intermediate container c44d33719c82
    Successfully built 7fe06a0a3cf2
    
    

    看到Successfully built 即构建成功

    1. 基于这个镜像再构建一个带有jdk的镜像
    • 创建目录和Dockerfile

    root@gavin:/zhangkun/docker# mkdir centos-ssh-root-jdk
    root@gavin:/zhangkun/docker# cd centos-ssh-root-jdk
    root@gavin:/zhangkun/docker/centos-ssh-root-jdk#vim Dockerfile

    Dockerfile内容如下:
    FROM gavin/centos-ssh-root:v1.0
    ADD jdk-8u161-linux-x64.tar.gz /usr/local/
    RUN mv /usr/local/jdk1.8.0_161 /usr/local/jdk1.8
    ENV JAVA_HOME /usr/local/jdk1.8
    ENV PATH $JAVA_HOME/bin:$PATH
    
    

    root@gavin:/zhangkun/docker/centos-ssh-root-jdk#sudo cp /home/gavin/下载/jdk-8u161-linux-x64.tar.gz ./

    • 构建镜像:

    root@gavin:/zhangkun/docker/centos-ssh-root-jdk# docker build -t gavin/centos-ssh-root-jdk:v1.0 .

    结果如下:
    Sending build context to Docker daemon 189.8 MB
    Step 1 : FROM gavin/centos-ssh-root:v1.0
     ---> 7fe06a0a3cf2
    Step 2 : ADD jdk-8u161-linux-x64.tar.gz /usr/local/
     ---> dbd69ff9138e
    Removing intermediate container 349dc08eeb98
    Step 3 : RUN mv /usr/local/jdk1.8.0_161 /usr/local/jdk1.8
     ---> Running in 749f2cd8d1eb
     ---> 1b3da34e1112
    Removing intermediate container 749f2cd8d1eb
    Step 4 : ENV JAVA_HOME /usr/local/jdk1.8
     ---> Running in 3ca9941d839e
     ---> 570fe947048e
    Removing intermediate container 3ca9941d839e
    Step 5 : ENV PATH $JAVA_HOME/bin:$PATH
     ---> Running in dac0d7608397
     ---> bfa7c2cb375e
    Removing intermediate container dac0d7608397
    Successfully built bfa7c2cb375e
    
    1. 基于这个jdk镜像再构建一个带有hadoop的镜像
    • 创建目录和Dockerfile

    root@gavin:/zhangkun/docker# mkdir centos-ssh-root-jdk-hadoop
    root@gavin:/zhangkun/docker# cd centos-ssh-root-jdk-hadoop/
    root@gavin:/zhangkun/docker/centos-ssh-root-jdk-hadoop# vim Dockerfile

    Dockerfile内容如下:
    FROM gavin/centos-ssh-root-jdk:v1.0
    ADD hadoop-2.4.1.tar.gz /usr/local
    RUN mv /usr/local/hadoop-2.4.1 /usr/local/hadoop
    ENV HADOOP_HOME /usr/local/hadoop
    ENV PATH $HADOOP_HOME/bin:$PATH
    
    

    root@gavin:/zhangkun/docker/centos-ssh-root-jdk-hadoop# docker build -t gavin/gavin/centos-ssh-root-jdk-hadoop:v1.0 .

    结果如下:
    Sending build context to Docker daemon 138.7 MB
    Step 1 : FROM gavin/centos-ssh-root-jdk:v1.0
     ---> bfa7c2cb375e
    Step 2 : ADD hadoop-2.4.1.tar.gz /usr/local
     ---> d7615a5d6599
    Removing intermediate container 85de43d7f162
    Step 3 : RUN mv /usr/local/hadoop-2.4.1 /usr/local/hadoop
     ---> Running in 889d669375bd
     ---> ef463ffd5e70
    Removing intermediate container 889d669375bd
    Step 4 : ENV HADOOP_HOME /usr/local/hadoop
     ---> Running in 34aeb57db70d
     ---> f24fdd8f7e2d
    Removing intermediate container 34aeb57db70d
    Step 5 : ENV PATH $HADOOP_HOME/bin:$PATH
     ---> Running in 5bee04e663b4
     ---> 556d46804532
    Removing intermediate container 5bee04e663b4
    Successfully built 556d46804532
    
    

    构建的三个镜像如下:

    root@gavin:/zhangkun/docker/centos-ssh-root-jdk-hadoop# docker images

    REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
    gavin/gavin/centos-ssh-root-jdk-hadoop   v1.0                556d46804532        3 hours ago         1.631 GB
    gavin/centos-ssh-root-jdk                v1.0                bfa7c2cb375e        4 hours ago         1.162 GB
    gavin/centos-ssh-root                    v1.0                7fe06a0a3cf2        4 hours ago         393 MB
    
    

    三:搭建hadoop分布式集群

    1. 集群规划
      准备搭建一个具有三个节点的集群,一主两从
      主节点:hadoop0 ip:192.168.10.3(5022)
      从节点1:hadoop1 ip:192.168.10.4 (3022)
      从节点2:hadoop2 ip:192.168.10.5(4022)

    但是由于docker容器重新启动之后ip会发生变化,所以需要我们给docker设置固定ip。使用pipework给 docker容器设置固定ip

    1. 启动三个容器,分别作为hadoop0 hadoop1 hadoop2
      在宿主机上执行下面命令,给容器设置主机名和容器的名称,并且在hadoop0中对外开放端口50070 和8088
    docker run --name hadoop0 --hostname hadoop0 -d -P -p 50070:50070 -p 8088:8088 crxy/centos-ssh-root-jdk-hadoop
    
    docker run --name hadoop1 --hostname hadoop1 -d -P crxy/centos-ssh-root-jdk-hadoop
    
    docker run --name hadoop2 --hostname hadoop2 -d -P crxy/centos-ssh-root-jdk-hadoop
    
    
    1. 给这三台容器设置固定IP
    • 下载PipeWoke
    root@gavin:/zhangkun/docker#git clone https://github.com/jpetazzo/pipework
    root@gavin:/zhangkun/docker#cp pipework/pipework /usr/local/bin/
    
    
    • 安装网桥工具
    root@gavin:/zhangkun/docker#yum -y install bridge-utils
    
    • 创建网络
    root@gavin:/zhangkun/docker#brctl addbr br0
    root@gavin:/zhangkun/docker#ip link set dev br0 up
    root@gavin:/zhangkun/docker#ip addr add 192.168.10.1/24 dev br0
    
    • 给容器设置固定ip
    root@gavin:/zhangkun/docker#pipework br0 hadoop0 192.168.10.3/24
    root@gavin:/zhangkun/docker#pipework br0 hadoop1 192.168.10.4/24
    root@gavin:/zhangkun/docker#pipework br0 hadoop2 192.168.10.5/24
    
    
    1. 配置hadoop集群
    • 先连接到hadoop0上,
     root@gavin:/zhangkun/docker#docker exec -it hadoop0 /bin/bash
    

    下面的步骤就是hadoop集群的配置过程

    • 设置主机名与ip的映射,修改三台容器:vi /etc/hosts
      添加下面配置
    192.168.2.10    hadoop0
    192.168.2.11    hadoop1
    192.168.2.12    hadoop2
    
    • 设置ssh免密码登录
      在hadoop0上执行下面操作
    cd  ~
    mkdir .ssh
    cd .ssh
    ssh-keygen -t rsa(一直按回车即可)
    ssh-copy-id -i localhost
    ssh-copy-id -i hadoop0
    ssh-copy-id -i hadoop1
    ssh-copy-id -i hadoop2
    

    在hadoop1上执行下面操作

    cd  ~
    cd .ssh
    ssh-keygen -t rsa(一直按回车即可)
    ssh-copy-id -i localhost
    ssh-copy-id -i hadoop1
    

    在hadoop2上执行下面操作

    cd  ~
    cd .ssh
    ssh-keygen -t rsa(一直按回车即可)
    ssh-copy-id -i localhost
    ssh-copy-id -i hadoop2
    
    • 在hadoop0上修改hadoop的配置文件
      进入到/usr/local/hadoop/etc/hadoop目录
      修改目录下的配置文件core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml
      (1) hadoop-env.sh
    export JAVA_HOME=/usr/local/jdk1.8
    

    (2)core-site.xml

    <configuration>
            <property>
                    <name>fs.defaultFS</name>
                    <value>hdfs://hadoop0:9000</value>
            </property>
            <property>
                    <name>hadoop.tmp.dir</name>
                    <value>/usr/local/hadoop/tmp</value>
            </property>
             <property>
                     <name>fs.trash.interval</name>
                     <value>1440</value>
            </property>
    </configuration>
    

    (3)hdfs-site.xml

    <configuration>
        <property>
            <name>dfs.replication</name>
            <value>1</value>
        </property>
        <property>
            <name>dfs.permissions</name>
            <value>false</value>
        </property>
    </configuration>
    

    (4)yarn-site.xml

    <configuration>
            <property>
                    <name>yarn.nodemanager.aux-services</name>
                    <value>mapreduce_shuffle</value>
            </property>
            <property> 
                    <name>yarn.log-aggregation-enable</name> 
                    <value>true</value> 
            </property>
    </configuration>
    

    (5)修改文件名:mv mapred-site.xml.template mapred-site.xml
    vi mapred-site.xml

    <configuration>
        <property>
            <name>mapreduce.framework.name</name>
            <value>yarn</value>
        </property>
    </configuration>
    

    (6)格式化
    进入到/usr/local/hadoop目录下

    • 执行格式化命令
    bin/hdfs namenode -format
    注意:在执行的时候会报错,是因为缺少which命令,安装即可
    
    执行下面命令安装
    yum install -y which
    

    看到下面命令说明格式化成功。


    这里写图片描述

    格式化操作不能重复执行。如果一定要重复格式化,带参数-force即可。
    (7)启动伪分布hadoop

    命令:sbin/start-all.sh
    

    第一次启动的过程中需要输入yes确认一下。


    这里写图片描述

    使用jps,检查进程是否正常启动?能看到下面几个进程表示伪分布启动成功

    [root@hadoop0 hadoop]# jps 
    818 ResourceManager
    1239 Jps
    522 DataNode
    411 NameNode
    669 SecondaryNameNode
    909 NodeManager
    
    

    (8)停止伪分布hadoop

    命令:sbin/stop-all.sh
    

    (9)指定nodemanager的地址,修改文件yarn-site.xml

    <property>
        <description>The hostname of the RM.</description>
        <name>yarn.resourcemanager.hostname</name>
        <value>hadoop0</value>
      </property>
    

    (10)修改hadoop0中hadoop的一个配置文件etc/hadoop/slaves
    删除原来的所有内容,修改为如下

    hadoop1
    hadoop2
    

    (11)在hadoop0中执行命令

      scp  -rq /usr/local/hadoop   hadoop1:/usr/local
      scp  -rq /usr/local/hadoop   hadoop2:/usr/local
    

    (12)启动hadoop分布式集群服务

    执行sbin/start-all.sh
    

    注意:在执行的时候会报错,是因为两个从节点缺少which命令,安装即可

    分别在两个从节点执行下面命令安装

    yum install -y which
    1
    再启动集群(如果集群已启动,需要先停止)

    sbin/start-all.sh
    

    (13)验证集群是否正常
    首先查看进程:
    Hadoop0上需要有这几个进程

    [root@hadoop0 hadoop]# jps
    4643 Jps
    4073 NameNode
    4216 SecondaryNameNode
    4381 ResourceManager
    

    Hadoop1上需要有这几个进程

    [root@hadoop1 hadoop]# jps
    715 NodeManager
    849 Jps
    645 DataNode
    

    Hadoop2上需要有这几个进程

    [root@hadoop2 hadoop]# jps
    456 NodeManager
    589 Jps
    388 DataNode
    

    使用程序验证集群服务
    创建一个本地文件

    vi a.txt
    hello you
    hello me
    

    上传a.txt到hdfs上

    hdfs dfs -put a.txt /
    

    执行wordcount程序

    cd /usr/local/hadoop/share/hadoop/mapreduce
    hadoop jar hadoop-mapreduce-examples-2.4.1.jar wordcount /a.txt /out
    

    查看程序执行结果


    这里写图片描述

    这样就说明集群正常了。

    在这可以直接通过宿主机访问容器中hadoop集群的服务
    宿主机的ip为:192.168.1.27

    http://192.168.1.27:50070/
    http://192.168.1.27:8088/

    四:集群节点重启

    停止三个容器,在宿主机上执行下面命令

    docker stop hadoop0
    docker stop hadoop1
    docker stop hadoop2
    

    容器停止之后,之前设置的固定ip也会消失,重新再使用这几个容器的时候还需要重新设置固定ip
    先把之前停止的三个容器起来

    docker start hadoop0
    docker start hadoop1
    docker start hadoop2
    

    在宿主机上执行下面命令重新给容器设置固定ip

    pipework br0 hadoop0 192.168.2.10/24
    pipework br0 hadoop1 192.168.2.11/24
    pipework br0 hadoop2 192.168.2.12/24
    

    还需要重新在容器中配置主机名和ip的映射关系,每次都手工写比较麻烦
    写一个脚本,runhosts.sh

    #!/bin/bash
    echo 192.168.2.10       hadoop0 >> /etc/hosts
    echo 192.168.2.11       hadoop1 >> /etc/hosts
    echo 192.168.2.12       hadoop2 >> /etc/hosts
    

    添加执行权限,chmod +x runhosts.sh
    把这个脚本拷贝到所有节点,并且分别执行这个脚本

    scp runhosts.sh  hadoop1:~
    scp runhosts.sh  hadoop2:~
    

    执行脚本的命令 ./runhosts.sh

    查看/etc/hosts文件中是否添加成功


    这里写图片描述

    注意:有一些docker版本中不会在hosts文件中自动生成下面这些映射,所以我们才在这里手工给容器设置固定ip,并设置主机名和ip的映射关系。

    172.17.0.25     hadoop0
    172.17.0.25     hadoop0.bridge
    172.17.0.26     hadoop1
    172.17.0.26     hadoop1.bridge
    172.17.0.27     hadoop2
    172.17.0.27     hadoop2.bridge
    

    启动hadoop集群

    sbin/start-all.sh
    

    相关文章

      网友评论

        本文标题:使用docker搭建hadoop集群

        本文链接:https://www.haomeiwen.com/subject/alrqxftx.html