环境:centos7
准备工作:
- 三台机器(内存大于2G) 分别写hosts、设定hostname
192.168.8.139 master
192.168.8.131 slave1
192.168.8.132 slave2
关闭防火墙
关闭selinux
关闭firewalld设定密钥登录
- master可以通过密钥登陆本机和两台slave
- master上生成密钥对:
ssh-keygen 一直回车 - 复制~/.ssh/id_rsa.pub 内容到本机和两台slave的 ~/.ssh/authorized_keys
- 设置本机和两台slave机器上的~/.ssh/authorized_keys文件权限为600
- chmod 600 ~/.ssh/authorized_keys
- 在master上
ssh master
ssh slave1
ssh slave2 - 可以直接登陆
安装Hadoop
部署Hadoop环境最少需要三台机器(运行内存大于2G),本次实验只是在master上安装Hadoop。
部署JDK环境
参考:https://my.oschina.net/u/3497124/blog/1525269
[root@study src]# mv jdk-8u144-linux-x64.tar.gz\?AuthParam\=1507689673_6c0c57db8c3433e1c37ba89d61ea4cf4 jdk-8u144-linux-x64.tar.gz
[root@study src]# tar zxf jdk-8u144-linux-x64.tar.gz
[root@study src]# mv jdk1.8.0_144 /usr/local/jdk1.8
环境配置:
[root@study src]# vim /etc/profile
#jdk
JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charset.jar
[root@study src]# source /etc/profile
检测配置是否成功:
[root@study src]# java -version
安装Hadoop
下载二进制包:
[root@study src]# wget http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.8.1/hadoop-2.8.1.tar.gz
[root@study src]# tar zxvf hadoop-2.8.1.tar.gz
[root@study src]# mv hadoop-2.8.1 /usr/local/hadoop
[root@study src]# cd /usr/local/hadoop/
[root@study hadoop]# mkdir tmp dfs dfs/data dfs/name
配置Hadoop
[root@study hadoop]# vim /usr/local/hadoop/etc/hadoop/core-site.xml
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://192.168.8.139:9000</value>
#注意更改此处IP为自己主机的IP
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>file:/usr/local/hadoop/tmp</value>
</property>
<property>
<name>io.file.buffer.size</name>
<value>131702</value>
</property>
</configuration>
[root@study hadoop]# vim /usr/local/hadoop/etc/hadoop/hdfs-site.xml
<configuration>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/usr/local/hadoop/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/usr/local/hadoop/dfs/data</value>
</property>
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>192.168.8.139:9001</value>
#更改为master主机IP
</property>
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
</configuration>
[root@study hadoop]# vim /usr/local/hadoop/etc/hadoop/mapred--site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.jobhistory.address</name>
<value>192.168.8.139:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>192.168.8.139:19888</value>
</property>
</configuration>
[root@study hadoop]# vim /usr/local/hadoop/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.nodemanager.auxservices.mapreduce.shuffle.class</name>
<value>org.apache.hadoop.mapred.ShuffleHandler</value>
</property>
<property>
<name>yarn.resourcemanager.address</name>
<value>192.168.8.139:8032</value>
</property>
<property>
<name>yarn.resourcemanager.scheduler.address</name>
<value>192.168.8.139:8030</value>
</property>
<property>
<name>yarn.resourcemanager.resource-tracker.address</name>
<value>192.168.8.139:8031</value>
</property>
<property>
<name>yarn.resourcemanager.admin.address</name>
<value>192.168.8.139:8033</value>
</property>
<property>
<name>yarn.resourcemanager.webapp.address</name>
<value>192.168.8.139:8088</value>
</property>
<property>
<name>yarn.nodemanager.resource.memory-mb</name>
<value>768</value>
</property>
</configuration>
注意更改IP。
更改脚本环境变量:
[root@study hadoop]# pwd
/usr/local/hadoop/etc/hadoop
[root@study hadoop]# vim hadoop-env.sh
export JAVA_HOME=/usr/local/jdk1.8
[root@study hadoop]# vim yarn-env.sh
export JAVA_HOME=/usr/local/jdk1.8
配置从机器:
[root@study hadoop]# vim slaves
192.168.8.131
192.168.8.132
##在此添加slaves的IP
至此,master配置完成。然后将Hadoop的配置文件同步到slave机器,便完成了Hadoop的配置!
启动
在master上操作即可,两台slave机器上会自动启动。
初始化;
[root@study hadoop]# /usr/local/hadoop/bin/hdfs namenode -format
[root@study hadoop]# echo $?
0
启动服务:
[root@study hadoop]# /usr/local/hadoop/sbin/start-all.sh
查看服务状态:
[root@study hadoop]# netstat -lntp |grep java
tcp 0 0 0.0.0.0:50070 0.0.0.0:* LISTEN 4026/java
tcp 0 0 192.168.8.139:9000 0.0.0.0:* LISTEN 4026/java
tcp 0 0 192.168.8.139:9001 0.0.0.0:* LISTEN 4216/java
tcp6 0 0 192.168.8.139:8088 :::* LISTEN 4368/java
tcp6 0 0 192.168.8.139:8030 :::* LISTEN 4368/java
tcp6 0 0 192.168.8.139:8031 :::* LISTEN 4368/java
tcp6 0 0 192.168.8.139:8032 :::* LISTEN 4368/java
tcp6 0 0 192.168.8.139:8033 :::* LISTEN 4368/java
停止服务:
[root@study hadoop]# /usr/local/hadoop/sbin/stop-all.sh
访问Hadoop的web端主页:
浏览器打开 http://192.168.8.139:8088/
浏览器打开 http://192.168.8.139:50070/
测试
以下操作在master上实现:
[root@study ~]# cd /usr/local/hadoop/
建立测试目录
[root@study hadoop]# bin/hdfs dfs -mkdir /123
如果在此提示:
copyFromLocal: Cannot create directory /123/. Name node is in safe mode.
这是因为开启了安全模式,解决办法:
[root@study hadoop]# bin/hdfs dfsadmin -safemode leave
查看123目录是否创建成功:
[root@study hadoop]# bin/hdfs dfs -ls /
Found 1 items
drwxr-xr-x - root supergroup 0 2017-10-11 12:57 /123
将当前目录下的LICENSE.txt复制到hadopp中:
[root@study hadoop]# bin/hdfs dfs -copyFromLocal ./LICENSE.txt /123
#不开启slave无法执行该操作
创建测试文件:
[root@study hadoop]# bin/hdfs dfs -touchz /123/123.txt
查看/123/下有哪些文件:
[root@study hadoop]# bin/hdfs dfs -ls /123/
Found 1 items
-rw-r--r-- 2 root supergroup 0 2017-10-11 13:06 /123/123.txt
用wordcount分析LICENSE.txt:
[root@study hadoop]# bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar wordcount /123/LICENSE.txt /output/123
查看分析后的文件:
[root@study hadoop]# bin/hdfs dfs -ls /output/123
查看分析结果:
[root@study hadoop]# bin/hdfs dfs -cat /output/123/part-r-00000
由于本实验机器配置较低,无法完成数据分析工作,操作流程无误!