Hadoop的全分布模式最少需要三台机器:一台主节点,两台从节点。全分布模式主要用于生产环境。本节将来介绍一下Hadoop2.x全分布模式的环境搭建过程。
软件安装包准备
VMware Workstation 14 Pro:https://pan.baidu.com/s/13xFtXXaH_HpbwpOrki78bw
CentOS 7 64位:https://pan.baidu.com/s/11hrNQ4A7argCSVBU7WaAtA
JDK1.8 64位:https://pan.baidu.com/s/1cvaqKgFzkSBOutr6xwqN6w
Hadoop2.7.3:https://pan.baidu.com/s/1Smbw074oNoq1Io23-etMhg
Step 1:Linux环境准备
master:192.168.112.10
slave1:192.168.112.11
slave2:192.168.112.12
.1.关闭每台主机的Selinux防火墙和iptables防火墙:
关闭Selinux:
临时关闭:[root@localhost ~]# setenforce 0
永久关闭:[root@localhost ~]# vi /etc/selinux/config,
修改其中的参数:SELINUX=disabled,按Esc:qw保存退出。
关闭iptables:
安装服务:[root@master ~]# yum -y install iptables-services
临时关闭:[root@master ~]# systemctl stop firewalld.service
永久关闭:[root@master ~]# systemctl disable firewalld.service
开启防火墙:[root@master ~]# systemctl start firewalld.service
查看firewall服务状态:[root@master ~]# systemctl status firewalld
查看firewall的运行状态:[root@master ~]#firewall-cmd --state
查询端口是否开放:[root@master ~]#firewall-cmd --query-port=8080/tcp
开放80端口:[root@master ~]#firewall-cmd --permanent --add-port=80/tcp
移除端口:[root@master ~]# firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙):[root@master ~]# firewall-cmd --reload
2.设置每台主机的IP地址:
编辑网卡配置文件:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
修改参数:
BOOTPROTO=static
ONBOOT=yes
追加参数:
IPADDR=192.168.254.113
NETMASK=255.255.255.0
GATEWAY=192.168.112.2
DNS1=8.8.8.8
DNS2=119.29.29.29
重启网络服务:[root@localhost ~]# systemctl restart network.service
查看配置的IP地址:[root@localhost ~]# ip add
或者 [root@localhost ~]# ifconfig -a
测试IP是否可用:[root@localhost ~]# curl www.baidu.com
或者 [root@localhost ~]# ping www.baidu.com
3.设置每台主机的hostname主机名:
方式1:使用hostnamectl命令:
[root@localhost ~]# hostnamectl set-hostname master
方式2:编辑配置文件:
[root@localhost ~]# vi /etc/hostname
清空内容后写入:master
重新登录系统会显示新的主机名:
[root@master ~]#
4.设置每台主机的主机名到IP的映射关系:
[root@master ~]# vi /etc/hosts
192.168.254.113 master
192.168.254.114 slave1
192.168.254.115 slave2
5.测试主机名是否可用:
[root@master ~]# ping master
PING master (192.168.254.113) 56(84) bytes of data.
64 bytes from master (192.168.254.113): icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from master (192.168.254.113): icmp_seq=2 ttl=64 time=0.092 ms
64 bytes from master (192.168.254.113): icmp_seq=3 ttl=64 time=0.094 ms
[root@master ~]# ping slave1
PING slave1 (192.168.254.114) 56(84) bytes of data.
64 bytes from slave1 (192.168.254.114): icmp_seq=7 ttl=64 time=0.715 ms
64 bytes from slave1 (192.168.254.114): icmp_seq=8 ttl=64 time=0.628 ms
64 bytes from slave1 (192.168.254.114): icmp_seq=9 ttl=64 time=0.590 ms
[root@master ~]# ping slave2
PING slave2 (192.168.254.115) 56(84) bytes of data.
64 bytes from slave2 (192.168.254.115): icmp_seq=1 ttl=64 time=0.255 ms
64 bytes from slave2 (192.168.254.115): icmp_seq=2 ttl=64 time=0.677 ms
64 bytes from slave2 (192.168.254.115): icmp_seq=3 ttl=64 time=0.638 ms
6.配置3台主机之间的秘钥认证
(1)使用ssh-keygen工具生成秘钥对:
[root@master ~]# ssh-keygen -t rsa
私钥:Your identification has been saved in /root/.ssh/id_rsa.
公钥:Your public key has been saved in /root/.ssh/id_rsa.pub.
(2)将生成的公钥发给每台主机(包括自己):
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@master
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@slave1
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@slave2
(3)测试3台主机之间的互信关系是否建立成功:
[root@master ~]# ssh root@slave1
Last login: Tue Apr 10 22:04:08 2018 from 192.168.254.113
[root@slave1 ~]# 登出
Connection to slave1 closed.
[root@master ~]# ssh root@slave2
Last login: Tue Apr 10 22:06:07 2018 from 192.168.254.113
[root@slave2 ~]# 登出
Connection to slave2 closed.
[root@master ~]# ssh root@master
Last login: Tue Apr 10 22:07:30 2018 from 192.168.254.113
[root@master ~]#
7.同步3台主机的时间(选做)
由于各个主机上的时间可能不一致,会导致执行MapReduce程序出现异常,因此需要同步各个主机的时间。在实际生成环境中,一般使用时间服务器来同步时间,但是搭建时间服务器相对较为复杂。这里介绍一种简单的方法来快速同步每台主机的时间。我们知道,使用date命令可以设置主机的时间,因此这里使用putty的插件来同时向每一台主机发送date命令,以到达同步时间的目的。
(1)使用TPuTTY工具连接三台主机,点击MTPuTTY工具的Tools菜单下的“Send script…”子菜单,打开发送脚本工具窗口。
(2)输入命令:date -s 2018-10-8,然后回车(注意:一定要回车,否则只发送不执行),在下面服务器列表中选择要同步的主机,然后点击“Send script”,即可将时间同步为2018-10-8 00:00:00。
Step 2:安装JDK
1.上传JDK安装包文件
/usr/local/src/ jdk-8u162-linux-x64.tar.gz
2.解压JDK安装包文件
tar -zxvf jdk-8u162-linux-x64.tar.gz
3.配置Java环境变量
[root@master jdk1.8.0_162]# vim /root/.bash_profile
JAVA_HOME=/usr/local/src/jdk1.8.0_162
export JAVA_HOME
PATH=$JAVA_HOME/bin:$JAVA_HOME/sbin:$PATH
export PATH
4.测试JDK是否安装成功
[root@master jdk1.8.0_162]# java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
5.将master节点安装好的jdk分发到每个节点:
scp -rp jdk1.8.0_162 slave1:/usr/local/src/
scp -rp jdk1.8.0_162 slave2:/usr/local/src/
6.将master节点配置好的环境变量分发到每个节点:
scp -rp /root/.bash_profile slave1:/root/.bash_profile
scp -rp /root/.bash_profile slave2:/root/.bash_profile
Step 3:安装Hadoop
1.上传Hadoop安装文件
[root@master hadoop-2.7.3]# pwd
/usr/local/src/hadoop-2.7.3
2.解压:tar -zxvf hadoop-2.7.3.tar.gz
3.配置环境变量:[root@master hadoop-2.7.3]# vim /root/.bash_profile
HADOOP_HOME=/usr/local/src/hadoop-2.7.3
export HADOOP_HOME
PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH
export PATH
4.使用source命令使配置文件立即生效:source /root/.bash_profile
5.配置hadoop:
5.1配置hadoop-env.sh文件:
[root@master hadoop]# echo $JAVA_HOME
/root/trainings/jdk1.8.0_162
[root@master hadoop]# vi hadoop-env.sh
#export JAVA_HOME=${JAVA_HOME}
export JAVA_HOME=/root/trainings/jdk1.8.0_162
5.2配置hdfs-site.xml文件:
[root@master hadoop]# vi hdfs-site.xml
<configuration>
<!--配置数据块的冗余度,默认是3-->
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<!--是否进行权限检查,默认是true-->
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
5.3配置core-site.xml文件:
<configuration>
<property><!--配置NameNode的地址-->
<name>fs.defaultFS</name>
<value>hdfs://master:9000</value>
</property>
<property><!--配置DataNode保存数据的目录,默认值:Linux的临时目录/tmp,重启清空-->
<name>hadoop.tmp.dir</name>
<value>/usr/local/src/hadoop-2.7.3/tmp</value>
</property>
</configuration>
5.4配置mapred-site.xml文件:
将模板文件mapred-site.xml.template重命名为mapred-site.xml然后编辑:
[root@master hadoop]# mv mapred-site.xml.template mapred-site.xml
[root@master hadoop]# vi mapred-site.xml
<configuration>
<!--配置MapReduce的运行方式-->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
5.5配置yarn-site.xml文件:
[root@master hadoop]# vi yarn-site.xml
<configuration>
<!--配置Yarn的主节点ResourceManger-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>master</value>
</property>
<!--配置从节点NodeManager运行MR程序的方式-->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
5.6配置slaves文件:
[root@master hadoop]# vi slaves
slave1
slave2
5.7对NameNode进行格式化
[root@master hadoop]# hdfs namenode -format
出现下面的日志说明格式化成功:
18/04/11 00:43:10 INFO common.Storage: Storage directory
/root/trainings/hadoop-2.6.1/tmp/dfs/name has been successfully formatted.
5.8将配置好的目录分别复制给两个从节点,并验证是否成功。
[root@master src]# scp -rp hadoop-2.7.3 slave1:/usr/local/src/
[root@master src]# scp -rp hadoop-2.7.3 slave2:/usr/local/src/
5.9在master上启动Hadoop全分布模式:
[root@master ~]# start-all.sh
第一次启动需要输入yes继续。
Are you sure you want to continue connecting (yes/no)? yes
启动成功后,使用jps命令查看各个节点上开启的进程:
[root@master ~]# jps
2129 Jps
1875 ResourceManager
1577 NameNode
1741 SecondaryNameNode
[root@slave1 ~]# jps
1504 NodeManager
1426 DataNode
1597 Jps
[root@slave2 ~]# jps
1424 DataNode
1594 Jps
1500 NodeManager
使用命令行查看HDFS的状态:
[root@master ~]# hdfs dfsadmin -report
Configured Capacity: 36477861888 (33.97 GB)
Present Capacity: 32422076416 (30.20 GB)
DFS Remaining: 32422068224 (30.20 GB)
DFS Used: 8192 (8 KB)
DFS Used%: 0.00%
Under replicated blocks: 0
Blocks with corrupt replicas: 0
Missing blocks: 0
-------------------------------------------------
Live datanodes (2):
Name: 192.168.254.115:50010 (slave2)
Hostname: slave2
Decommission Status : Normal
Configured Capacity: 18238930944 (16.99 GB)
DFS Used: 4096 (4 KB)
Non DFS Used: 2027802624 (1.89 GB)
DFS Remaining: 16211124224 (15.10 GB)
DFS Used%: 0.00%
DFS Remaining%: 88.88%
Configured Cache Capacity: 0 (0 B)
Cache Used: 0 (0 B)
Cache Remaining: 0 (0 B)
Cache Used%: 100.00%
Cache Remaining%: 0.00%
Xceivers: 1
Last contact: Wed Apr 11 23:54:34 CST 2018
Name: 192.168.254.114:50010 (slave1)
Hostname: slave1
Decommission Status : Normal
Configured Capacity: 18238930944 (16.99 GB)
DFS Used: 4096 (4 KB)
Non DFS Used: 2027982848 (1.89 GB)
DFS Remaining: 16210944000 (15.10 GB)
DFS Used%: 0.00%
DFS Remaining%: 88.88%
Configured Cache Capacity: 0 (0 B)
Cache Used: 0 (0 B)
Cache Remaining: 0 (0 B)
Cache Used%: 100.00%
Cache Remaining%: 0.00%
Xceivers: 1
Last contact: Wed Apr 11 23:54:34 CST 2018
使用浏览器查看HDFS的状态:http://192.168.112.10:50070
image使用浏览器查看YARN的状态:http://192.168.112.10:8088
imageStep 4:测试
1.在HDFS上创建新建目录/input:
root@master ~]# hdfs dfs -mkdir /input
2.将本地数据文件data.txt上传至该目录:
[root@master ~]# hdfs dfs -put /home/zhangdd/data.txt /input
[root@master ~]# hdfs dfs -ls /input
Found 1 items
-rw-r--r-- 1 root supergroup 60 2018-04-09 22:39 /input/data.txt
[root@master ~]# hdfs dfs -cat /input/data.txt
I love Beijing
I love China
Beijing is the capital of China
3.进入到Hadoop的示例程序目录:
[root@master zhangdd]# cd /usr/local/src/hadoop-2.7.3/share/hadoop/mapreduce/
[root@master mapreduce]# ls
hadoop-mapreduce-client-app-2.7.3.jar hadoop-mapreduce-client-jobclient-2.7.3-tests.jar
hadoop-mapreduce-client-common-2.7.3.jar hadoop-mapreduce-client-shuffle-2.7.3.jar
hadoop-mapreduce-client-core-2.7.3.jar hadoop-mapreduce-examples-2.7.3.jar
hadoop-mapreduce-client-hs-2.7.3.jar lib
hadoop-mapreduce-client-hs-plugins-2.7.3.jar lib-examples
hadoop-mapreduce-client-jobclient-2.7.3.jar sources
[root@master mapreduce]#
4.执行示例程序中的Wordcount程序,以HDFS上的/input/data.txt作为输入数据,
输出结果存放到HDFS上的/out/wc目录下:
[root@master mapreduce]# hadoop jar hadoop-mapreduce-examples-2.6.1.jar \
wordcount /input/data.txt /output/wc
执行以上命令,结果报错,如下:
解决办法:
-
yarn-site.xml增加如下配置,不需要重启hadoop
- 启动historyserver
/usr/local/hadoop-2.7.3/sbin目录下执行如下命令
mr-jobhistory-daemon.sh start historyserver
再次运行jar包
4.查看进度和结果:
可以通过终端打印出来的日志信息知道执行进度:
18/04/12 21:06:11 INFO mapreduce.Job: map 0% reduce 0%
18/04/12 21:06:22 INFO mapreduce.Job: map 100% reduce 0%
18/04/12 21:06:35 INFO mapreduce.Job: map 100% reduce 100%
18/04/12 21:06:36 INFO mapreduce.Job: Job job_1523537300303_0001 completed successfully
5.执行结束后可以在HDFS上的/out/wc目录下查看是否有_SUCCESS标志文件来判断是否执行成功。
[root@master mapreduce]# hdfs dfs -ls /output/wc
Found 2 items
-rw-r--r-- 2 root supergroup 0 2018-04-12 21:06 /output/wc/_SUCCESS
-rw-r--r-- 2 root supergroup 55 2018-04-12 21:06 /output/wc/part-r-00000
6.如果执行成功,可以在part-r-00000文件中查看到wordcount程序的结果。
[root@master mapreduce]# hdfs dfs -cat /output/wc/part-r-00000
Beijing 2
China 2
I 2
capital 1
is 1
love 2
of 1
the 1
至此,在CentOS7上搭建的Hadoop2.x全分布模式的开发环境已经完成!
网友评论