Hadoop 2.7.2 集群搭建

版本信息:Centos7 + Hadoop 2.7.2

Hadoop + Spark 集群搭建系列文章,建议按顺序参考:

Hadoop & Spark 集群搭建 理念思想

Hadoop 2.7.2 集群搭建-预备工作

Hadoop 2.7.2 集群搭建 (不用点了,就是本文)

Spark 1.6.2 + Hadoop 2.7.2 集群搭建


Hadoop 共有3种运行模式,不同模式需要不同的配置:

  • 独立(或本地)模式:无需任何守护进程,所有程序都在同一个 JVM 上执行,开发阶段测试和调试 MapReduce 程序很方便
  • 伪公布模式:守护进程运行在本地机器上,模拟小规模的集群
  • 全分布模式:守护进程运行在集群上

注意:

1.开展以下操作前,请先做好上一篇的预备工作

2.开展以下操作时,尽量用集群里各节点相同的用户名 liuyao 来操作,除非需要 root 权限时使用 root


独立模式

独立模式是默认模式,不需要配置,此时 core-site.xml、hdfs-site.xml、mapred-site.xml 和 yarn-site.xml 等配置文件为空。


伪分布模式

以下几乎参考官网 documentation

配置xml文件

# etc/hadoop/core-site.xml
<configuration>
    <property>
        <name>fs.defaultFSname>  #配置Hadoop的默认文件系统,默认为HDFS
        <value>hdfs://localhost:9000value>
    property>
    <property>
        <name>hadoop.tmp.dirname>  #配置Hadoop临时文件根目录,namenode和datanode分别存放在该目录下的/dfs/name和/dfs/data
        <value>/home/liuyao/00Hadoop/hadoop-2.7.2/tmpvalue> 
    property>
configuration>
# etc/hadoop/hdfs-site.xml
<configuration>
    <property>
        <name>dfs.replicationname>  #配置文件系统块复本数量,默认为3,当本地或伪公布模式时,设为1
        <value>1value>
    property>
configuration>

执行 (Locally)

1.Format the filesystem

[liuyao@master hadoop-2.7.2]$ bin/hdfs namenode -format
16/07/14 19:39:56 INFO namenode.NameNode: STARTUP_MSG: 
/************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG:   host = localhost/127.0.0.1
……

注意:!!只需要对master上的namenode格式化(在{dfs.name.dir}/里生成相应的文件),且只需要一次!!除非把{dfs.name.dir}/里的内容删除了才需要重新格式化。

2.Start NameNode daemon and DataNode daemon

[liuyao@master hadoop-2.7.2]# sbin/start-dfs.sh 
Starting namenodes on [localhost]
localhost: starting namenode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-namenode-master.out
localhost: starting datanode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-datanode-master.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-secondarynamenode-master.out

[liuyao@master hadoop-2.7.2]# jps
24785 DataNode
25986 Jps
24494 NameNode
25166 SecondaryNameNode

可以看出,在 Localhost 上创建了 namenode 和 datanode,在0.0.0.0上创建了 secondary namenode。

3.Browse the web interface for the NameNode

By default it is available at: NameNode - http://localhost:50070/

4.Make the HDFS directories required to execute MapReduce jobs

[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -mkdir /user
[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -mkdir /user/liuyao

注意:此处是在 HDFS 分布式文件系统上创建文件夹/user/user/liuyao,用于表示用户目录,注意不是在本地文件系统!

5.Copy the input files into the distributed filesystem

[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -mkdir /user/liuyao/input
[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -put etc/hadoop/*.xml /user/liuyao/input

注意:官网上代码报错!需改成上面两行!(可能原因:分布式系统当前目录并没有在 /user/liuyao/ 下,理想情况下应该是在 /user/liuyao/ 下的)

6.Run some of the examples provided

[liuyao@master hadoop-2.7.2]#  bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar grep input output 'dfs[a-z.]+'
……

7.Examine the output files: Copy the output files from the distributed filesystem to the local filesystem and examine them

[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -get /user/root/output output
[liuyao@master hadoop-2.7.2]# cat output/output/*
1   dfsadmin
1   dfs.replication

Or view the output files on the distributed filesystem

[liuyao@master hadoop-2.7.2]# bin/hdfs dfs -cat output/*
1   dfsadmin
1   dfs.replication

8.When you’re done, stop the daemons with

[liuyao@master hadoop-2.7.2]# sbin/stop-dfs.sh
Stopping namenodes on [localhost]
localhost: stopping namenode
localhost: stopping datanode
Stopping secondary namenodes [0.0.0.0]
0.0.0.0: stopping secondarynamenode

执行 (YARN)

You can run a MapReduce job on YARN in a pseudo-distributed mode by setting a few parameters and running ResourceManager daemon and NodeManager daemon in addition.

0.Execute 1. ~ 5. steps of the above instructions

注意:官网上只写1~4步,其实第5步(在分布式系统上创建 input 并把配置文件复制进去)也需要(可能会提示文件夹已经存在),否则报错!

1.Configure parameters as follows

# etc/hadoop/mapred-site.xml
<configuration>
    <property>
        <name>mapreduce.framework.namename> #配置mapreduce框架,默认为classic,这里设为yarn
        <value>yarnvalue>
    property>
configuration>
# etc/hadoop/yarn-site.xml
<configuration>
    <property>
        <name>yarn.nodemanager.aux-servicesname>
        <value>mapreduce_shufflevalue>
    property>
configuration>

2.Start ResourceManager daemon and NodeManager daemon

[liuyao@master hadoop-2.7.2]# sbin/start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/yarn-liuyao-resourcemanager-master.out
localhost: starting nodemanager, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/yarn-liuyao-nodemanager-master.out

# 使用jps命令查看当前进程
[liuyao@master hadoop-2.7.2]# jps
10017 SecondaryNameNode
10840 NodeManager
9641 DataNode
10506 ResourceManager
11852 Jps
9325 NameNode

可以看到,先启动 yarn 守护进程,然后启动 resourcemanager,最后启动 nodemanager。加上 HDFS,一共有 NameNode、DataNode、SecondaryNameNode、ResourceManager 和 NodeManager 五个进程。

3.Browse the web interface for the ResourceManager

By default it is available at: ResourceManager - http://localhost:8088/

4.Run a MapReduce job

[liuyao@master hadoop-2.7.2]# bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar grep input output 'dfs[a-z.]+'

5.When you’re done, stop the daemons with

[liuyao@master hadoop-2.7.2]# sbin/stop-dfs.sh
……
[liuyao@master hadoop-2.7.2]# sbin/stop-yarn.sh
……

或者使用stop-all.sh命令:

[liuyao@master hadoop-2.7.2]# sbin/stop-all.sh
This script is Deprecated. Instead use stop-dfs.sh and stop-yarn.sh
Stopping namenodes on [localhost]
localhost: stopping namenode
localhost: stopping datanode
Stopping secondary namenodes [0.0.0.0]
0.0.0.0: stopping secondarynamenode
stopping yarn daemons
stopping resourcemanager
localhost: stopping nodemanager
localhost: nodemanager did not stop gracefully after 5 seconds: killing with kill -9
no proxyserver to stop

依次停止namenode、datanode、secondarynamenode、resoucemanager和nodemanager。


全分布模式

基于YARN的全分布模式集群各节点守护进程如下表所示

节点 HDFS YARN
master NameNode & SecondaryNameNode ResourceManager
slave1 DataNode NodeManager
slave2 DataNode NodeManager
…… …… ……

配置相关文件

注意:以下配置先只在 master 上配置。

很多配置同伪分布模式,比如:

  • 配置 hadoop-env.sh 里的 JAVA_HOME
  • 配置 core-site.xml 里的 fs.defaultFS 和 hadoop.tmp.dir
  • 配置 mapred-site.xml 里的 mapreduce.framework.name
  • 配置 yarn-site.xml 里的 yarn.nodemanager.aux-services

不过,在伪分布模式配置基础上,需要添加一些额外的配置,依次配置 yarn-env.sh、slaves、core-site.xml、hdfs-site.xml、mapred-site.xml 和 yarn-site.xml 共6个文件。

1.配置 yarn-env.sh 里的 JAVA_HOME,方法同 hadoop-env.sh

2.配置 slaves 文件,添加作为 slave 的节点IP,添加后的结果可以是以下2种,推荐第1种,因为第1种稳定保险,而第2种需要保证在 /etc/hosts 里成功配置了 slave1 和 slave2 的IP(详情见上一篇文章:预备工作)

[liuyao@master hadoop]$ cat slaves
101.X.XX.XX1
101.X.XX.XX2
[liuyao@master hadoop]$ cat slaves
slave1
slave2

3.配置 core-site.xml 文件,添加核心配置,配置后结果如下

<configuration>
    <property>
        <name>fs.defaultFSname>
      <value>hdfs://master:9000value> #此处不能是localhost!
    property>
    <property> 
        <name>hadoop.tmp.dirname> 
        <value>/home/liuyao/00Hadoop/hadoop-2.7.2/tmpvalue> 
    property>
    <property>
        <name>io.file.buffer.sizename> #有没有必要,有些教程里没有
        <value>131072value>
    property>
    <property>
        <name>hadoop.proxyuser.spark.hostsname> #有没有必要,有些教程里没有
        <value>*value>
    property>
    <property>
        <name>hadoop.proxyuser.spark.groupsname> #有没有必要,有些教程里没有
        <value>*value>
    property>
configuration>

4.配置 hdfs-site.xml 文件,配置后结果如下:

<configuration>
    <property>
        <name>dfs.namenode.secondary.http-addressname> #有没有必要,有些教程里没有
        <value>master:9001value> #此处不能是localhost!
    property>
    <property>
        <name>dfs.replicationname>
        <value>2value> #此处因为只有2个从节点,所以是2?!
    property>
    <property>
        <name>dfs.webhdfs.enabledname> #有没有必要,有些教程里没有
        <value>truevalue>
    property>
configuration>

5.配置 mapred-site.xml 文件,配置后结果如下:

<configuration>
    <property>
        <name>mapreduce.framework.namename>
        <value>yarnvalue>
    property>
    <property>
        <name>mapreduce.jobhistory.addressname> #有没有必要,有些教程里没有
        <value>master:10020value> #此处不能是localhost!
    property>
    <property>
        <name>mapreduce.jobhistory.webapp.addressname> #有没有必要,有些教程里没有
        <value>master:19888value> #此处不能是localhost!
    property>
configuration>

6.配置 yarn-site.xml 文件,配置后结果如下:

<configuration>
    <property>
        <name>yarn.nodemanager.aux-servicesname>
        <value>mapreduce_shufflevalue>
    property>
    <property>
        <name>yarn.nodemanager.aux-services.mapreduce.shuffle.classname>
        <value>org.apache.hadoop.mapred.ShuffleHandlervalue>
    property>
    <property>
        <name>yarn.resourcemanager.addressname>
        <value>master:8032value> #此处不能是localhost!下同!
    property>
    <property>
        <name>yarn.resourcemanager.scheduler.addressname>
        <value>master:8030value>
    property>
    <property>
        <name>yarn.resourcemanager.resource-tracker.addressname>
        <value>master:8035value>
    property>
    <property>
        <name>yarn.resourcemanager.admin.addressname>
        <value>master:8033value>
    property>
    <property>
       <name>yarn.resourcemanager.webapp.addressname>
       <value>master:8088value>
    property>
configuration>

7.将以上配置好的文件发送到各从节点上,注意尽量让 slave 各节点与 master 节点的目录结构完全一致!

[liuyao@master ~]$ scp -r 00Hadoop/ slave1:~/
……
[liuyao@master ~]$ scp -r 00Hadoop/ slave2:~/
……

注意:此时需要检查从节点的 JAVA_HOME 是否需要修改,若需要,则修改 hadoop-env.sh 和 yarn-env.sh 里相应内容。

启动集群

0.在启动集群前,需要关闭所有节点的防火墙

注意:Centos7 里用 firewalld 代替了传统的 iptables

[root@master hadoop-2.7.2]# firewall-cmd --state
running
[root@master hadoop-2.7.2]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@master hadoop-2.7.2]# systemctl stop firewalld
[root@master hadoop-2.7.2]# exit
exit
[liuyao@master hadoop-2.7.2]$ firewall-cmd --state
not running

否则会警告:No Route to Host,若使用传统 iptables,则输入命令service iptables stop即可。

1.格式化 namenode,只需在 master 上操作(且只需格式化一次)

[liuyao@master hadoop-2.7.2]$ hdfs namenode -format

2.启动 HDFS,只需在 master 上操作,各 slave 上相应进程会被激活

[liuyao@master hadoop-2.7.2]$ start-dfs.sh
Starting namenodes on [localhost]
localhost: starting namenode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-namenode-master.out
101.X.XX.XX2: starting datanode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-datanode-slave2.out
101.X.XX.XX1: starting datanode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-datanode-slave1.out
Starting secondary namenodes [localhost]
localhost: starting secondarynamenode, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/hadoop-liuyao-secondarynamenode-master.out

在 master 里使用 jps,可以看到 master 里进程 NameNode 和 SecondaryNameNode 在运行(并没有伪分布模式中有的 DataNode )

[liuyao@master hadoop-2.7.2]$ jps
26386 SecondaryNameNode
25993 NameNode
26686 Jps

在 slave 里使用 jps,可以看到 slave 里进程 DataNode 被 master 激活

[liuyao@slave1 hadoop-2.7.2]$ jps
6260 Jps
5917 DataNode
[liuyao@slave2 hadoop-2.7.2]$ jps
5746 Jps
5365 DataNode

3.启动 YARN,只需在 master 上操作,各 slave 上相应进程会被激活

[liuyao@master hadoop-2.7.2]$ start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/yarn-liuyao-resourcemanager-master.out
101.X.XX.XX2: starting nodemanager, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/yarn-liuyao-nodemanager-slave2.out
101.X.XX.XX1: starting nodemanager, logging to /home/liuyao/00Hadoop/hadoop-2.7.2/logs/yarn-liuyao-nodemanager-slave1.out

在 master 里使用 jps,可以看到 master 里进程 NameNode、SecondaryNameNode 和 ResourceManager 在运行(并没有伪分布模式中有的 DataNode 和 NodeManager )

[liuyao@master hadoop-2.7.2]$ jps
26386 SecondaryNameNode
25993 NameNode
27658 ResourceManager
27931 Jps

在 slave 里使用 jps,可以看到 slave 里进程 DataNode 和 NodeManager 被 master 激活

[liuyao@slave1 hadoop-2.7.2]$ jps
7127 NodeManager
5917 DataNode
7438 Jps
[liuyao@slave1 hadoop-2.7.2]$ jps
5365 DataNode
6616 NodeManager
6936 Jps

4.查看集群状态

[liuyao@master hadoop-2.7.2]$ hdfs dfsadmin -report
Configured Capacity: 0 (0 B)
Present Capacity: 0 (0 B)
DFS Remaining: 0 (0 B)
DFS Used: 0 (0 B)
DFS Used%: NaN%
Under replicated blocks: 0
Blocks with corrupt replicas: 0
Missing blocks: 0
Missing blocks (with replication factor 1): 0

-------------------------------------------------

查看HDFS: http://master:50070/
查看RM: http://master:8088/

运行程序

以 Hadoop 里示例 wordcount 举例说明

1.在本地创建 input 目录,新建2个文件并填写内容

[liuyao@master hadoop-2.7.2]$ mkdir input
[liuyao@master hadoop-2.7.2]$ vi input/file1
#添加内容:Hello world bye liuyao
[liuyao@master hadoop-2.7.2]$ vi input/file2
#添加内容:Hello Hadoop bye Hadoop

2.在 HDFS 上创建 input 目录,把以上2个文件复制进去

[liuyao@master hadoop-2.7.2]$ hdfs dfs -mkdir /user
[liuyao@master hadoop-2.7.2]$ hdfs dfs -mkdir /user/liuyao
[liuyao@master hadoop-2.7.2]$ hdfs dfs -mkdir /user/liuyao/input
[liuyao@master hadoop-2.7.2]$ hdfs dfs -put input/ /user/liuyao/
[liuyao@master hadoop-2.7.2]$ hdfs dfs -ls /user/liuyao/input/
Found 2 items
-rw-r--r--   2 liuyao supergroup         23 2016-07-16 14:28 /user/liuyao/input/file1
-rw-r--r--   2 liuyao supergroup         24 2016-07-16 14:28 /user/liuyao/input/file2

3.执行 wordcount 程序

[liuyao@master hadoop-2.7.2]$ hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar wordcount /user/liuyao/input/ output
16/07/16 14:31:24 INFO client.RMProxy: Connecting to ResourceManager at master/101.6.86.175:8032
16/07/16 14:31:25 INFO input.FileInputFormat: Total input paths to process : 2
16/07/16 14:31:25 INFO mapreduce.JobSubmitter: number of splits:2

4.查看程序结果

[liuyao@master hadoop-2.7.2]$ hdfs dfs -cat /user/liuyao/output/*
Hadoop  2
Hello   2
bye 2
liuyao  1
world   1

常见问题

1.datanode没有启动

原因:多次格式化hdfs namenode -format后,master和slave里的clusterID或namespaceID不一致:master的{dfs.name.dir}下VERSION里的clusterID每次格式化都会改变,而slave的{dfs.data.dir}下VERSION里的clusterID保留的只是namenode第一次格式化的clusterID(准确来说,保留的是{dfs.data.dir}文件夹创建时master里的clusterID)。

解决:方法1-停止 Hadoop,删除master里{dfs.name.dir}/(若没设置,则默认是 {dfs.tmp.dir}/dfs/name)文件夹及其内容,删除slave里{dfs.data.dir}/(若没设置,则默认是{dfs.tmp.dir}/dfs/data)文件夹及其内容,再次格式化 namenode,重启 Hadoop。方法2-复制master里的clusterID,slave里相应clusterID为该复制的clusterID。

建议:除特殊需要之外,只需对master的namenode格式化,且只需格式化一次。

2.namenode没有启动

原因:master的{dfs.name.dir}文件夹不存在

解决:停止Hadoop,重新对master的namenode格式化,即可生成{dfs.name.dir}文件夹


参考资料

http://www.bkjia.com/Linux/941106.html#top

http://www.cnblogs.com/xia520pi/archive/2012/05/16/2503949.html

http://blog.csdn.net/zolalad/article/details/11470449

你可能感兴趣的:(大数据)