具体可以参考Hadoop官网 http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/SingleCluster.html 及 http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/ClusterSetup.html
1. 安装前准备
需要安装对应的java和ssh及sshd

Centos7下已经安装好了ssh及sshd,这里就不描述了。
java安装详见:http://www.jianshu.com/p/5d3ecf3b30f6
下面的例子都是安装的oracle的jdk8演示的。
java -version 查看是否配置安装成功:

2. 安装hadoop
从官网下载需要的版本,这里用的是当前最新的2.8.1。
用 tar -zxvf 进行解压。
在解压的目录下修改etc/hadoop/hadoop-env.sh,只需要修改对应的JAVA_HOME:

可以看到一bin/hadoop目录的一些用法:

然后官网给出了3种安装方式(本地,伪分布式,分布式):

2.1 Local(Standalone) 本地模式

在安装hadoop的根目录运行:mkdir input

复制etc/hadoop下的xml文件到input文件夹:cp etc/hadoop/*.xml input

运行hadoop的example,这里根据具体的hadoop版本来看(这里用的是2.8.1):bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar grep input output 'dfs[a-z.]+'

这个例子应该是统计对应的文件夹下xml中有多少个'dfs[a-z.]+',然后输出结果到output。再次验证这个例子时需要用不同的输出文件夹,这里方便测试删除了output,运行bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar grep input output 'xml+':

在output文件加下有2个文件:

其中part-r-00000记录了 ‘13 xml’。
2.2 Pseudo-Distributed 伪分布式
2.2.1 按照官网的说明进行配置修改:

修改etc/hadoop/core-site.xml文件(根据自己的域名进行修改,不一定是localhost,可能修改过了):vim etc/hadoop/core-site.xml
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/usr/local/hadoop/hdfs/tmp</value>
</property>
<property>
<name>fs.trash.interval</name>
<value>4320</value>
</property>
</configuration>
修改etc/hadoop/hdfs-site.xml文件:vim etc/hadoop/hdfs-site.xml
<configuration>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>/usr/local/src/hadoop-2.8.1/hdfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/usr/local/src/hadoop-2.8.1/hdfs/data</value>
</property>
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
<property>
<name>dfs.permissions.superusergroup</name>
<value>staff</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
</configuration>
2.2.2 按照官网的配置设置免密码登录:

一般情况下是需要进行设置的(还是根据具体的域名和用户来进行设置):
$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/authorized_keys
运行完上面的命令后进行检测:ssh localhost,发现不需要设置密码了。
2.2.3 运行mapreduce
参考官网进行配置:

首先格式化文件系统:bin/hdfs namenode -format
确定NameNode和DataNode的守护进程:sbin/start-dfs.sh

查看是否启动成功: jps

在web页面访问安装hadoop机器的ip(hadoop2.8.1的默认端口是50070,上图中的是3.X版本的文档,写的是默认端口9870):http://hadoopIp:50070

创建HDFS文件夹来运行mapreduce任务
$ bin/hdfs dfs -mkdir /user
$ bin/hdfs dfs -mkdir /user/<username>
复制输入文件到分布式文件系统hdfs(这里创建的input不是当前hadoop根目录下的input)
$ bin/hdfs dfs -mkdir input
$ bin/hdfs dfs -put etc/hadoop/*.xml input
运行提供的例子(不是很明白这里和前面的操作有什么不同):bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar grep input output 'dfs[a-z.]+'
检查输出文件:bin/hdfs dfs -cat output/*
测试完成,关闭守护进程:sbin/stop-dfs.sh

2.2.4 运行YARN
参考官网进行配置:

修改mapred-site.xml:vim etc/hadoop/mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
修改yarn-site.xml(这里配置一个环节变量的白名单):vim etc/hadoop/yarn-site.xml
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME</value>
</property>
</configuration>
这里要修改环境变量设置,在末尾添加:
export HADOOP_HOME=/usr/local/src/hadoop-2.8.1/
export HADOOP_PREFIX=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_PREFIX
export HADOOP_CONF_DIR=$HADOOP_PREFIX/etc/hadoop
export HADOOP_HDFS_HOME=$HADOOP_PREFIX
export HADOOP_MAPRED_HOME=$HADOOP_PREFIX
export HADOOP_YARN_HOME=$HADOOP_PREFIX
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:
启动ResourceManager和NodeManager守护进程:sbin/start-yarn.sh

在浏览器中查看ResourceManager的web页面:http://hadoopIp:8088

运行一个mapreduce的任务:
先启动dfs:bin/start-dfs.sh,
然后还是继续运行hadoop的example:bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar grep input output 'dfs[a-z.]+' (这里暂时不知道运行结果是否正确,卡在这里没动;这是因为liunx的硬盘空间超过了90%,需要清理)

再次刷新检查ResourceManager的web页面,可以看到下方多了上图中运行的任务

测试完成,关闭Yarn的守护进程: sbin/stop-yarn.sh
2.3 集群配置
集群配置详见(根据对应的版本去查找,这里给的是最新的地址)http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/ClusterSetup.html 。这里简单地介绍下基于前面搭建好的伪分布式进行搭建。
我们先按照1master,3个slave的方式来搭建;首先将搭建伪分布式hadoop的虚拟机复制3份,将机器的域名改成master, slave1, slave2和slave3.
下面提到的要修改的文件都是在hadoop根目录下的etc/hadoop下。
在所有的机器上修改core-site.xml (只要将原来的localhost全部改成master):

在master节点上修改hdfs-site.xml(只要添加dfs.namenode.secondary.http-address):

在所有节点上修改mapred-site.xml(只要添加mapreduce.jobhistory.address和mapreduce.jobhistory.webapp.address):

jobhistory是 Hadoop自带了一个历史服务器,用于记录 Mapreduce历史作业。默认情况下, jobhistory没有启动,可用手动通过命令启动。
这里用的2.8.1的命令是:mapred historyserver。其他版本的需要查看相关文档。
所有节点修改yarn-site.xml(按照下图标红的添加):

master节点修改slaves文件(每个slave域名都需要是独立的一行):

不难发现集群配置的修改主要是把默认配置的localhost域名指向了master。
这里要重新运行格式化文件系统(如果hadoop根目录的hdfs/data下有内容,需要清理,避免造成前面搭建环境引起的问题):hdfs namenode -format
在master上运行start-dfs.sh:

在web页面进行查看:

在所有节点上运行jps查看:

可以发现master上只启动了NameNode和SecondaryNameNode,其他slave节点上只有DataNode启动。
在master上运行start-yarn.sh:

在web页面查看cluster信息:

可以发现3个Node的信息了,在所有节点上运行jps:

发现master上多了ResourceManager,其他slave上多了NodeManager。
最后在master上运行官网的例子,需要重新创建hdfs目录:
$ hdfs dfs -mkdir /user
$ hdfs dfs -mkdir /user/root
复制输入文件到分布式文件系统hdfs(这里创建的input不是当前hadoop根目录下的input)
$ hdfs dfs -mkdir input
$ hdfs dfs -put etc/hadoop/*.xml input
在hadoop根目录运行:bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar grep input output 'dfs[a-z.]+'
查看输出结果:hdfs dfs -cat output/*

在集群的web页面查看:

要查看history需要先运行mapred historyserver:

点击集群web页面下的history链接,可以看具体的信息:

到这里应该表示集群都已经搭建好了,也可以通过web页面进行查看。
网友评论