Hadoop的全分布模式最少需要三臺機器:一臺主節(jié)點,兩臺從節(jié)點努酸。全分布模式主要用于生產(chǎn)環(huán)境抖韩。本節(jié)將來介紹一下Hadoop2.x全分布模式的環(huán)境搭建過程篙挽。
軟件安裝包準(zhǔn)備
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環(huán)境準(zhǔn)備
master:192.168.112.10
slave1:192.168.112.11
slave2:192.168.112.12
.1.關(guān)閉每臺主機的Selinux防火墻和iptables防火墻:
關(guān)閉Selinux:
臨時關(guān)閉:[root@localhost ~]# setenforce 0
永久關(guān)閉:[root@localhost ~]# vi /etc/selinux/config颠毙,
修改其中的參數(shù):SELINUX=disabled嗦随,按Esc:qw保存退出骚烧。
關(guān)閉iptables:
安裝服務(wù):[root@master ~]# yum -y install iptables-services
臨時關(guān)閉:[root@master ~]# systemctl stop firewalld.service
永久關(guān)閉:[root@master ~]# systemctl disable firewalld.service
開啟防火墻:[root@master ~]# systemctl start firewalld.service
查看firewall服務(wù)狀態(tài):[root@master ~]# systemctl status firewalld
查看firewall的運行狀態(tài):[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.設(shè)置每臺主機的IP地址:
編輯網(wǎng)卡配置文件:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
修改參數(shù):
BOOTPROTO=static
ONBOOT=yes
追加參數(shù):
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
重啟網(wǎng)絡(luò)服務(wù):[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.設(shè)置每臺主機的hostname主機名:
方式1:使用hostnamectl命令:
[root@localhost ~]# hostnamectl set-hostname master
方式2:編輯配置文件:
[root@localhost ~]# vi /etc/hostname
清空內(nèi)容后寫入:master
重新登錄系統(tǒng)會顯示新的主機名:
[root@master ~]#
4.設(shè)置每臺主機的主機名到IP的映射關(guān)系:
[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臺主機之間的秘鑰認(rèn)證
(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)將生成的公鑰發(fā)給每臺主機(包括自己):
[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臺主機之間的互信關(guān)系是否建立成功:
[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臺主機的時間(選做)
由于各個主機上的時間可能不一致嫩挤,會導(dǎo)致執(zhí)行MapReduce程序出現(xiàn)異常,因此需要同步各個主機的時間绘沉。在實際生成環(huán)境中煎楣,一般使用時間服務(wù)器來同步時間,但是搭建時間服務(wù)器相對較為復(fù)雜车伞。這里介紹一種簡單的方法來快速同步每臺主機的時間择懂。我們知道,使用date命令可以設(shè)置主機的時間另玖,因此這里使用putty的插件來同時向每一臺主機發(fā)送date命令休蟹,以到達同步時間的目的。
(1)使用TPuTTY工具連接三臺主機日矫,點擊MTPuTTY工具的Tools菜單下的“Send script…”子菜單,打開發(fā)送腳本工具窗口绑榴。
(2)輸入命令:date -s 2018-10-8哪轿,然后回車(注意:一定要回車,否則只發(fā)送不執(zhí)行)翔怎,在下面服務(wù)器列表中選擇要同步的主機窃诉,然后點擊“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環(huán)境變量
[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節(jié)點安裝好的jdk分發(fā)到每個節(jié)點:
scp -rp jdk1.8.0_162 slave1:/usr/local/src/
scp -rp jdk1.8.0_162 slave2:/usr/local/src/
6.將master節(jié)點配置好的環(huán)境變量分發(fā)到每個節(jié)點:
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.配置環(huán)境變量:[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>
<!--配置數(shù)據(jù)塊的冗余度飘痛,默認(rèn)是3-->
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<!--是否進行權(quán)限檢查,默認(rèn)是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保存數(shù)據(jù)的目錄容握,默認(rèn)值: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的主節(jié)點ResourceManger-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>master</value>
</property>
<!--配置從節(jié)點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
出現(xiàn)下面的日志說明格式化成功:
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將配置好的目錄分別復(fù)制給兩個從節(jié)點,并驗證是否成功剔氏。
[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繼續(xù)塑猖。
Are you sure you want to continue connecting (yes/no)? yes
啟動成功后竹祷,使用jps命令查看各個節(jié)點上開啟的進程:
[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的狀態(tài):
[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的狀態(tài):http://192.168.112.10:50070
使用瀏覽器查看YARN的狀態(tài):http://192.168.112.10:8088
Step 4:測試
1.在HDFS上創(chuàng)建新建目錄/input:
root@master ~]# hdfs dfs -mkdir /input
2.將本地數(shù)據(jù)文件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.執(zhí)行示例程序中的Wordcount程序,以HDFS上的/input/data.txt作為輸入數(shù)據(jù)羊苟,
輸出結(jié)果存放到HDFS上的/out/wc目錄下:
[root@master mapreduce]# hadoop jar hadoop-mapreduce-examples-2.6.1.jar \
wordcount /input/data.txt /output/wc
執(zhí)行以上命令塑陵,結(jié)果報錯,如下:
解決辦法:
-
yarn-site.xml增加如下配置蜡励,不需要重啟hadoop
- 啟動historyserver
/usr/local/hadoop-2.7.3/sbin目錄下執(zhí)行如下命令
mr-jobhistory-daemon.sh start historyserver
再次運行jar包
4.查看進度和結(jié)果:
可以通過終端打印出來的日志信息知道執(zhí)行進度:
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.執(zhí)行結(jié)束后可以在HDFS上的/out/wc目錄下查看是否有_SUCCESS標(biāo)志文件來判斷是否執(zhí)行成功令花。
[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.如果執(zhí)行成功,可以在part-r-00000文件中查看到wordcount程序的結(jié)果凉倚。
[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全分布模式的開發(fā)環(huán)境已經(jīng)完成!