vmware搭建hadoop集群

0. 安装环境

安装系统版本,集群IP列表

  • ubunut 16.04 server
192.168.24.128  ubuntu-master
192.168.24.129  ubuntu-slave1
192.168.24.130  ubuntu-slave2

1. 创建用户 和用户组(hadoop 用户的密码也是hadoop 方便记忆)

useradd hadoop
passwd hadoop
groupadd bigdata
usermod -a -G bigdata hadoop
mkdir /home/hadoop
chown -R hadoop:bigdata /home/hadoop

2. 更改hosts 文件以及对应的机器hostname

host文件 /etc/hosts
hostname文件 /etc/hostname

cat /etc/hosts
192.168.24.128  ubuntu-master
192.168.24.129  ubuntu-slave1
192.168.24.130  ubuntu-slave2

在这里要注意,我安装的集群是用虚拟机创建的,机器之间是直接复制虚拟机的方式,这时有一个问题就是机器的hostname都是一样的,这样一来,即使修改了hosts 文件,但是每台机器的hostname 并没有生效,需要修改各自的hostname保持和上述文件中一样的对应关系。

3. 无密码登陆

在每台虚拟机上分别执行以下命令

ssh-keygen -t rsa -P ""
cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

执行

ssh localhost

可以发现此时无需输入密码

集群之间的无密码ssh 通信

以192.168.24.128/129/130 三台机为例

登录192.168.24.128

ssh-copy-id -i $HOME/.ssh/id_rsa.pub hadoop@192.168.24.129
ssh-copy-id -i $HOME/.ssh/id_rsa.pub hadoop@192.168.24.130

5. 设置环境变量

安装jdk,并设置环境变量,我的.bashrc文件的相关内容如下

#set oracle jdk environment
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79  ## 这里要注意目录要换成自己解压的jdk 目录
export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib   
export PATH=${JAVA_HOME}/bin:$PATH

5. 上传hadoop压缩包到指定目录并解压

$ ls
hadoop-2.6.2  hadoop-2.6.2.tar.gz

6. 修改相应的配置文件

需要修改的文件列表如下

~/hadoop/etc/hadoop/hadoop-env.sh
~/hadoop/etc/hadoop/yarn-env.sh
~/hadoop/etc/hadoop/slaves
~/hadoop/etc/hadoop/core-site.xml
~/hadoop/etc/hadoop/hdfs-site.xml
~/hadoop/etc/hadoop/mapred-site.xml
~/hadoop/etc/hadoop/yarn-site.xml
  • 以下两个文件设置的内容为JAVA_HOME环境变量
~/hadoop/etc/hadoop/hadoop-env.sh
~/hadoop/etc/hadoop/yarn-env.sh
  • 设置slave机器的列表
cat ~/hadoop/etc/hadoop/slaves
192.168.24.129
192.168.24.130
  • ~/hadoop/etc/hadoop/core-site.xml
<configuration>
  <property>
    <name>hadoop.tmp.dir</name>  
    <value>/home/hadoop/bigdata/hadoop/tmp</value>
  </property>
  <property>
    <name>fs.default.name</name>  
    <value>hdfs://192.168.24.128:9000</value>
  </property>
</configuration>
  • hdfs-site.xml
<configuration>
  <property>
    <name>dfs.http.address</name>  
    <value>192.168.24.128:50070</value>
  </property>  
  <property>
    <name>dfs.namenode.secondary.http-address</name>  
    <value>192.168.24.128:50090</value>
  </property>  
  <property>
    <name>dfs.replication</name>  
    <value>1</value>
  </property>
</configuration>
  • mapred-site.xml
<configuration>
  <property>
    <name>mapred.job.tracker</name>  
    <value>192.168.24.128:9001</value>
  </property>  
  <property>
    <name>mapred.map.tasks</name>  
    <value>2</value>
  </property>  
  <property>
    <name>mapred.reduce.tasks</name>  
    <value>2</value>
  </property>  
  <property>
    <name>mapreduce.framework.name</name>  
    <value>yarn</value>
  </property>  
  <property>
    <name>mapreduce.jobhistory.address</name>  
    <value>192.168.24.128:10020</value>
  </property>  
  <property>
    <name>mapreduce.jobhistory.webapp.address</name>  
    <value>192.168.24.128:19888</value>
  </property>
</configuration>
  • yarn-site.xml
<configuration>
  <!-- Site specific YARN configuration properties -->  
  <property>
    <name>yarn.resourcemanager.address</name>  
    <value>192.168.24.128:8032</value>
  </property>  
  <property>
    <name>yarn.resourcemanager.scheduler.address</name>  
    <value>192.168.24.128:8030</value>
  </property>  
  <property>
    <name>yarn.resourcemanager.webapp.address</name>  
    <value>192.168.24.128:8088</value>
  </property>  
  <property>
    <name>yarn.resourcemanager.resource-tracker.address</name>  
    <value>192.168.24.128:8031</value>
  </property>  
  <property>
    <name>yarn.resourcemanager.admin.address</name>  
    <value>192.168.24.128:8033</value>
  </property>  
  <property>
    <name>yarn.nodemanager.aux-services</name>  
    <value>mapreduce_shuffle</value>
  </property>  
  <property>
    <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>  
    <value>org.apache.hadoop.mapred.ShuffleHandler</value>
  </property>
</configuration>

按照上面的配置之后应该来说就可以了,但是在之前安装的过程中,我遇到过任务hang住的情况,查找资料发现是内存不够的原因,由于是自己电脑跑的虚拟机,内存有限,于是针对内存情况增加如下配置:

mapred-site.xml
<property>
 <name>mapreduce.map.memory.mb</name>
 <value>230</value>
</property>

<property>
 <name>mapreduce.reduce.memory.mb</name>
 <value>460</value>
</property>

<property>
 <name>mapreduce.map.java.opts</name>
 <value>-Xmx184m</value>
</property>

<property>
 <name>mapreduce.reduce.java.opts</name>
 <value>-Xmx368m</value>
</property>

<property>
 <name>yarn.app.mapreduce.am.resource.mb</name>
 <value>460</value>
</property>

<property>
 <name>yarn.app.mapreduce.am.command-opts</name>
 <value>-Xmx368m</value>
</property>
yarn-site.xml
<property>
 <name>yarn.nodemanager.resource.memory-mb</name>
 <value>700</value>
</property>

<property>
 <name>yarn.scheduler.minimum-allocation-mb</name>
 <value>230</value>
</property>

<property>
 <name>yarn.scheduler.maximum-allocation-mb</name>
 <value>700</value>
</property>

7. 启动

首次启动需要先在 Master 节点执行 NameNode 的格式化:

./hdfs namenode -format
进入~/hadoop/sbin目录
./start-dfs.sh
./start-yarn.sh
./mr-jobhistory-daemon.sh start historyserver

查看进程

hadoop@ubuntu-master:~$ jps
6193 ResourceManager
5863 NameNode
6051 SecondaryNameNode
6476 JobHistoryServer
6937 Jps
hadoop@ubuntu-slave1:~$ jps
3536 Jps
2884 NodeManager
2771 DataNode
hadoop@ubuntu-slave2:~$ jps
2937 NodeManager
2826 DataNode
3653 Jps

登录192.168.24.128:50070可以看到hadoop集群基本信息

8. 运行测试例子

进入 ~/hadoop/bin目录

./hdfs dfs -mkdir -p /user/hadoop
./hdfs dfs -mkdir input
./hdfs dfs -put /home/hadoop/bigdata/hadoop/etc/hadoop/*.xml input

./hadoop jar /home/hadoop/bigdata/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar grep input output 'dfs[a-z.]+'

运行程序部分结果如下:

17/01/13 23:45:14 INFO client.RMProxy: Connecting to ResourceManager at /192.168.24.128:8032
17/01/13 23:45:17 WARN mapreduce.JobResourceUploader: No job jar file set.  User classes may not be found. See Job or Job#setJar(String).
17/01/13 23:45:17 INFO input.FileInputFormat: Total input paths to process : 9
17/01/13 23:45:18 INFO mapreduce.JobSubmitter: number of splits:9
17/01/13 23:45:19 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1484322039697_0001
17/01/13 23:45:20 INFO mapred.YARNRunner: Job jar is not present. Not adding any jar to the list of resources.
17/01/13 23:45:21 INFO impl.YarnClientImpl: Submitted application application_1484322039697_0001
17/01/13 23:45:22 INFO mapreduce.Job: The url to track the job: http://ubuntu-master:8088/proxy/application_1484322039697_0001/
17/01/13 23:45:22 INFO mapreduce.Job: Running job: job_1484322039697_0001
17/01/13 23:45:53 INFO mapreduce.Job: Job job_1484322039697_0001 running in uber mode : false
17/01/13 23:45:53 INFO mapreduce.Job:  map 0% reduce 0%
17/01/13 23:46:22 INFO mapreduce.Job:  map 11% reduce 0%
17/01/13 23:46:43 INFO mapreduce.Job:  map 22% reduce 0%
17/01/13 23:47:08 INFO mapreduce.Job:  map 33% reduce 0%
17/01/13 23:47:09 INFO mapreduce.Job:  map 44% reduce 0%
17/01/13 23:47:10 INFO mapreduce.Job:  map 56% reduce 0%
17/01/13 23:47:11 INFO mapreduce.Job:  map 67% reduce 0%
17/01/13 23:47:59 INFO mapreduce.Job:  map 78% reduce 0%
17/01/13 23:48:03 INFO mapreduce.Job:  map 100% reduce 0%
17/01/13 23:48:24 INFO mapreduce.Job:  map 100% reduce 50%
17/01/13 23:48:46 INFO mapreduce.Job:  map 100% reduce 100%
17/01/13 23:48:47 INFO mapreduce.Job: Job job_1484322039697_0001 completed successfully
... ...

9. 后记

  1. 之前也零零散散装过几次hadoop,但是没有记录笔记,之前安装的虚拟机也被删除了,这此就重新整理了一下,以便以后参考。
  2. 安装的过程并不顺利,中间遇到了磕磕绊绊的小问题,之后会整理单独一个遇到问题的笔记,暂时就这样
    原文作者:isoldier
    原文地址: https://www.jianshu.com/p/d283757f50f8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞