记录在64位CentOS 7环境下搭建Hadoop 2.7集群的步骤,这些记录都仅供参考!

1、操作系统环境配置

1.1、操作系统环境

主机名 IP地址 角色 Hadoop用户
hadoop-master 192.168.30.60 NameNode、ResourceManager、SecondaryNameNode hadoop
hadoop-slave01 192.168.30.61 DataNode、NodeManager hadoop
hadoop-slave02 192.168.30.62 DataNode、NodeManager hadoop
hadoop-slave03 192.168.30.63 DataNode、NodeManager hadoop

1.2、关闭防火墙和SELinux

1.2.1、关闭防火墙

$ systemctl stop firewalld 
$ systemctl disable firewalld

1.2.2、关闭SELinux

$ setenforce 0
$ sed -i 's/enforcing/disabled/' /etc/sysconfig/selinux

注:以上操作需要使用root用户

1.3、hosts配置

$ vi /etc/hosts

########## Hadoop host ##########
192.168.30.60   hadoop-master
192.168.30.61   hadoop-slave01
192.168.30.62   hadoop-slave02
192.168.30.63   hadoop-slave03

注:以上操作需要使用root用户,通过ping 主机名可以返回对应的IP即可

1.4、配置无密码访问

首先要创建hadoop用户,然后在4台主机上使用hadoop用户配置无密码访问,所有主机的操作相同,以hadoop-master为例

生成私钥和公钥
$ ssh-keygen -t rsa

拷贝公钥到主机(需要输入密码)

$ ssh-copy-id hadoop@hadoop-master
$ ssh-copy-id hadoop@hadoop-slave01
$ ssh-copy-id hadoop@hadoop-slave02
$ ssh-copy-id hadoop@hadoop-slave03

注:以上操作需要在hadoop用户,通过hadoop用户ssh到其他主机不需要密码即可。

2、Java环境配置

2.1、下载JDK

注:使用hadoop用户操作

$ cd /home/hadoop
$ curl -o jdk-8u151-linux-x64.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz?AuthParam=1516091623_fa4174d4b1eed73f36aa38230498cd48

2.2、安装java

安装java可使用hadoop用户操作;

$ mkdir -p /home/hadoop/app/java
$ tar -zxf jdk-8u151-linux-x64.tar.gz
$ mv jdk1.8.0_151 /home/hadoop/app/java/jdk1.8
  • 配置Java环境变量:
    $ vi /home/hadoop/.bash_profile
export JAVA_HOME=/home/hadoop/app/java/jdk1.8
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

启用环境变量
$ source /home/hadoop/.bash_profile

注:通过java –version命令返回Java的版本信息即可

3、Hadoop安装配置

hadoop的安装配置使用hadoop用户操作;

3.1、安装Hadoop

  • 下载hadoop 2.7.5

$ curl -O http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.7.5/hadoop-2.7.5.tar.gz

  • 创建hadoop安装目录

$ mkdir -p /home/hadoop/app/hadoop/{tmp,hdfs/{data,name}}

  • 解压hadoop文件并移动到hadoop安装目录下

$ tar zxf tar -zxf hadoop-2.7.5.tar.gz -C /home/hadoop/app/hadoop

3.2、配置Hadoop

Hadoop配置文件都是XML文件,使用hadoop用户操作即可;

3.2.1、配置core-site.xml

$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/core-site.xml

<configuration>
    <property>
        <name>hadoop.tmp.dir</name>
        <value>file:/home/hadoop/app/hadoop/tmp</value>
    </property>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://hadoop-master:9000</value>
    </property>
    <property>
        <name>io.file.buffer.size</name>
        <value>131072</value>
    </property>
</configuration>

core-site默认参数参考

3.2.2、配置hdfs-site.xml

$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/hdfs-site.xml

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>3</value>
    </property>
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>/home/hadoop/app/hadoop/hdfs/name</value>
    </property>
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>/home/hadoop/app/hadoop/hdfs/data</value>
    </property>
    <property>
        <name>dfs.webhdfs.enabled</name>
        <value>true</value>
    </property>
</configuration>

hdfs-site默认参数配置

3.2.3、配置mapred-site.xml

mapred-site.xml需要从一个模板拷贝在修改
$ cp /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/mapred-site.xml.template /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/mapred-site.xml

$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/mapred-site.xml

<configuration>
    <property>
        <name>mapreduce.framework.name</name>
        <value>yarn</value>
    </property>
    <property>
        <name>mapreduce.jobhistory.address</name>
        <value>hadoop-master:10020</value>
    </property>
    <property>
        <name>mapreduce.jobhistory.webapp.address</name>
        <value>hadoop-master:19888</value>
    </property>
    <property>
        <name>mapreduce.jobhistory.done-dir</name>
        <value>/history/done</value>
    </property>
    <property>
        <name>mapreduce.jobhistory.intermediate-done-dir</name>
        <value>/history/done_intermediate</value>
    </property>
</configuration>

mapred-site默认参数配置

3.2.4、配置yarn-site.xml

$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/yarn-site.xml

<configuration>

<!-- Site specific YARN configuration properties -->
    <property>
        <name>yarn.nodemanager.aux-services</name>
        <value>mapreduce_shuffle</value>
    </property>
    <property>
        <name>yarn.resourcemanager.hostname</name>
        <value>hadoop-master</value>
    </property>
    <property>
        <name>yarn.resourcemanager.address</name>
        <value>hadoop-master:8032</value>
    </property>
    <property>
        <name>yarn.resourcemanager.scheduler.address</name>
        <value>hadoop-master:8030</value>
    </property>
    <property>
        <name>yarn.resourcemanager.resource-tracker.address</name>
        <value>hadoop-master:8031</value>
    </property>
    <property>
        <name>yarn.resourcemanager.admin.address</name>
        <value>hadoop-master:8033</value>
    </property>
    <property>
        <name>yarn.resourcemanager.webapp.address</name>
        <value>hadoop-master:8088</value>
    </property>
</configuration>

yarn-site默认参数配置

3.2.5、配置slaves

$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/slaves

hadoop-slave01
hadoop-slave02
hadoop-slave03

3.2.6、配置hadoop-env

修改hadoop-env.sh文件的JAVA_HOME环境变量,操作如下:
$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/hadoop-env.sh

export JAVA_HOME=/home/hadoop/app/java/jdk1.8

3.2.7、配置yarn-env

修改yarn-env.sh文件的JAVA_HOME环境变量,操作如下:
$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/yarn-env.sh

export JAVA_HOME=/home/hadoop/app/java/jdk1.8

3.2.8、配置mapred-env

修改mapred-env.sh文件的JAVA_HOME环境变量,操作如下:
$ vi /home/hadoop/app/hadoop/hadoop-2.7.5/etc/hadoop/mapred-env.sh

export JAVA_HOME=/home/hadoop/app/java/jdk1.8

3.3、拷贝Hadoop程序到slave

$ scp -r /home/hadoop/app/hadoop hadoop@hadoop-slave01:/home/hadoop/app/
$ scp -r /home/hadoop/app/hadoop hadoop@hadoop-slave02:/home/hadoop/app/
$ scp -r /home/hadoop/app/hadoop hadoop@hadoop-slave03:/home/hadoop/app/

3.4、配置Hadoop环境变量

在所有机器hadoop用户家目录下编辑 .bash_profile 文件,在最后追加:
$ vi /home/hadoop/.bash_profile

### Hadoop PATH
export HADOOP_HOME=/home/hadoop/app/hadoop/hadoop-2.7.5
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin

让环境变量生效:
source /home/hadoop/.bash_profile

注:这是配置hadoop的用户环境变量,如果系统变量设置在 /etc/profile.d/ 目录下新增

3.5、启动Hadoop

在hadoop主节点上初始化HDFS文件系统,然后启动hadoop集群

3.5.1、初始化HDFS文件系统

$ hdfs namenode –format

3.5.2、启动和关闭Hadoop集群

  • 启动:
    $ start-all.sh

注:在mapreduce.site.xml中配置了jobhistory,需要启动日志记录服务:
$ mr-jobhistory-daemon.sh start historyserver

  • 关闭:
    $ stop-all.sh

注:也可以一步一步执行启动,首先启动namenode-->datanode-->YARN -->NodeManagers -->historyserver

  • master进程:

    $ jps
    3124 NameNode
    3285 SecondaryNameNode
    3451 ResourceManager
    4254 Jps
  • slave进程:

    $ jps
    3207 Jps
    2409 NodeManager
    2332 DataNode
  • MapReducer PI运算
    $ hadoop jar /home/hadoop/app/hadoop/hadoop-2.7.5/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.5.jar pi 5 10

返回的结果是:Estimated value of Pi is 3.28000000000000000000

3.6、MapReduce wordcount测试

  • 创建存放文件目录和计算结果输出目录
    $ hadoop fs -mkdir  /user/hadoop/input
    $ hadoop fs -mkdir  /user/hadoop/output
  • 上传测试文件The_Man_of_Property
    $ hadoop fs -put The_Man_of_Property /user/hadoop/input
  • 启动测试

    $ hadoop jar /home/hadoop/app/hadoop/hadoop-2.7.5/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.5.jar wordcount /user/hadoop/input /user/hadoop/output/wordcounttest
  • 查看输出结果
$ hadoop fs -ls /user/hadoop/output/wordcounttest
Found 2 items
-rw-r--r--   3 hadoop supergroup          0 2018-01-17 14:32 /user/hadoop/output/wordcounttest/_SUCCESS
-rw-r--r--   3 hadoop supergroup     181530 2018-01-17 14:32 /user/hadoop/output/wordcounttest/part-r-00000
$ hadoop fs -get /user/hadoop/output/wordcounttest/part-r-00000 ./
$ cat part-r-00000 |sort -k2 -nr|head
the 5144
of  3407
to  2782
and 2573
a   2543
he  2139
his 1912
was 1702
in  1694
had 1526

4、参考资料

https://hadoop.apache.org/docs/r2.7.5/hadoop-project-dist/hadoop-common/ClusterSetup.html