Linux下Hadoop2.7.1集群環境的搭建(超詳細版)


本文旨在提供最基本的,可以用於在生產環境進行Hadoop、HDFS分布式環境的搭建,對自己是個總結和整理,也能方便新人學習使用。

一、基礎環境

在Linux上安裝Hadoop之前,需要先安裝兩個程序:

1.1 安裝說明

1. JDK 1.6或更高版本(本文所提到的安裝的是jdk1.7);
2. SSH(安全外殼協議),推薦安裝OpenSSH。
 
下面簡述一下安裝這兩個程序的原因:
1. Hadoop是用Java開發的,Hadoop的編譯及MapReduce的運行都需要使用JDK。
2. Hadoop需要通過SSH來啟動salve列表中各台主機的守護進程,因此SSH也是必須安裝的,即使是安裝偽分布式版本(因為Hadoop並沒有區分集群式和偽分布式)。對於偽分布式,Hadoop會采用與集群相同的處理方式,即依次序啟動文件conf/slaves中記載的主機上的進程,只不過偽分布式中salve為localhost(即為自身),所以對於偽分布式Hadoop,SSH一樣是必須的。
1.1 JDK的安裝與配置
1、上傳壓縮包
我這里使用的是WinScp工具 上傳jdk-7u76-linux-x64.tar.gz壓縮包
2、解壓壓縮包
tar -zxvf jdk-7u76-linux-x64.tar.gz
3、將解壓的目錄移動到/usr/local目錄下
mv /lutong/jdk1.7.0_76/ /usr/local/
4、配置環境變量
vim /etc/profile
5、重新加載/etc/profile,使配置生效
source /etc/profile
6、查看配置是否生效
echo $PATH
java -version
出現如上信息表示已經配置好了。
 

二、Host配置

由於我搭建Hadoop集群包含三台機器,所以需要修改調整各台機器的hosts文件配置,進入/etc/hosts,配置主機名和ip的映射,命令如下:
vim /etc/hosts
如果沒有足夠的權限,可以切換用戶為root。
三台機器的內容統一增加以下host配置:
可以通過hostname來修改服務器名稱為master、slave1、slave2
hostname master

 

三、Hadoop的安裝與配置

3.1 創建文件目錄

為了便於管理,給Master的hdfs的NameNode、DataNode及臨時文件,在用戶目錄下創建目錄:
/data/hdfs/name
/data/hdfs/data
/data/hdfs/tmp
然后將這些目錄通過scp命令拷貝到Slave1和Slave2的相同目錄下。

3.2 下載

首先到Apache官網(http://www.apache.org/dyn/closer.cgi/hadoop/common/)下載Hadoop,從中選擇推薦的下載鏡像(http://mirrors.hust.edu.cn/apache/hadoop/common/),我選擇hadoop-2.6.0的版本,並使用以下命令下載到Master機器的
/data目錄:
wget http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.7.1/hadoop-2.7.1.tar.gz
然后使用以下命令將 hadoop-2.7.1.tar.gz 解壓縮到/data目錄
tar -zxvf hadoop-2.7.1.tar.gz

3.3 配置環境變量

回到/data目錄,配置hadoop環境變量,命令如下:
vim /etc/profile
在/etc/profile添加如下內容
立刻讓hadoop環境變量生效,執行如下命令:
source /etc/profile
再使用hadoop命令,發現可以有提示了,則表示配置生效了。

3.4 Hadoop的配置

進入hadoop-2.7.1的配置目錄:
cd /data/hadoop-2.7.1/etc/hadoop
依次修改core-site.xml、hdfs-site.xml、mapred-site.xml、yarn-site.xml以及slaves文件。
3.4.1 修改core-site.xml
vim core-site.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
 3 <!--
 4   Licensed under the Apache License, Version 2.0 (the "License");
 5   you may not use this file except in compliance with the License.
 6   You may obtain a copy of the License at
 7 
 8     http://www.apache.org/licenses/LICENSE-2.0
 9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License. See accompanying LICENSE file.
14 -->
15 
16 <!-- Put site-specific property overrides in this file. -->
17 <configuration>
18 <property>
19   <name>hadoop.tmp.dir</name>
20   <value>file:/data/hdfs/tmp</value>
21   <description>A base for other temporary directories.</description>
22 </property>
23 <property>
24   <name>io.file.buffer.size</name>
25   <value>131072</value>
26 </property>
27 <property>
28   <name>fs.default.name</name>
29   <value>hdfs://master:9000</value>
30 </property>
31 <property>
32 <name>hadoop.proxyuser.root.hosts</name>
33 <value>*</value>
34 </property>
35 <property>
36 <name>hadoop.proxyuser.root.groups</name>
37 <value>*</value>
38 </property>
39 </configuration>
注意:hadoop.tmp.dir的value填寫對應前面創建的目錄
 
3.4.2 修改vim hdfs-site.xml
vim hdfs-site.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
 3 <!--
 4   Licensed under the Apache License, Version 2.0 (the "License");
 5   you may not use this file except in compliance with the License.
 6   You may obtain a copy of the License at
 7 
 8     http://www.apache.org/licenses/LICENSE-2.0
 9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License. See accompanying LICENSE file.
15 -->
16 
17 <!-- Put site-specific property overrides in this file. -->
18 
19 <configuration>
20 <property>
21 <name>dfs.replication</name>
22   <value>2</value>
23 </property>
24 <property>
25   <name>dfs.namenode.name.dir</name>
26   <value>file:/data/hdfs/name</value>
27   <final>true</final>
28 </property>
29 <property>
30   <name>dfs.datanode.data.dir</name>
31   <value>file:/data/hdfs/data</value>
32   <final>true</final>
33 </property>
34 <property>
35   <name>dfs.namenode.secondary.http-address</name>
36   <value>master:9001</value>
37 </property>
38 <property>
39   <name>dfs.webhdfs.enabled</name>
40   <value>true</value>
41 </property>
42 <property>
43   <name>dfs.permissions</name>
44   <value>false</value>
45 </property>
46 </configuration>

注意:dfs.namenode.name.dir和dfs.datanode.data.dir的value填寫對應前面創建的目錄


3.4.3 修改vim mapred-site.xml

復制template,生成xml,命令如下:

cp mapred-site.xml.template mapred-site.xml

vim  mapred-site.xml
 1 <?xml version="1.0"?>
 2 <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
 3 <!--
 4   Licensed under the Apache License, Version 2.0 (the "License");
 5   you may not use this file except in compliance with the License.
 6   You may obtain a copy of the License at
 7 
 8     http://www.apache.org/licenses/LICENSE-2.0
 9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License. See accompanying LICENSE file.
15 -->
16 
17 <!-- Put site-specific property overrides in this file. -->
18 
19 <configuration>
20 
21 <property>
22   <name>mapreduce.framework.name</name>
23   <value>yarn</value>
24 </property>
25 
26 </configuration>
 
3.4.4 修改vim yarn-site.xml
vim  yarn-site.xml
 1 <?xml version="1.0"?>
 2 <!--
 3   Licensed under the Apache License, Version 2.0 (the "License");
 4   you may not use this file except in compliance with the License.
 5   You may obtain a copy of the License at
 6 
 7     http://www.apache.org/licenses/LICENSE-2.0
 8 
 9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License. See accompanying LICENSE file.
14 -->
15 <configuration>
16 
17 <!-- Site specific YARN configuration properties -->
18 <property>
19 <name>yarn.resourcemanager.address</name>
20   <value>master:18040</value>
21 </property>
22 <property>
23   <name>yarn.resourcemanager.scheduler.address</name>
24   <value>master:18030</value>
25 </property>
26 <property>
27   <name>yarn.resourcemanager.webapp.address</name>
28   <value>master:18088</value>
29 </property>
30 <property>
31   <name>yarn.resourcemanager.resource-tracker.address</name>
32   <value>master:18025</value>
33 </property>
34 <property>
35   <name>yarn.resourcemanager.admin.address</name>
36   <value>master:18141</value>
37 </property>
38 <property>
39   <name>yarn.nodemanager.aux-services</name>
40   <value>mapreduce.shuffle</value>
41 </property>
42 <property>
43   <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
44   <value>org.apache.hadoop.mapred.ShuffleHandler</value>
45 </property>
46 </configuration>
由於我們已經配置了JAVA_HOME的環境變量,所以hadoop-env.sh與yarn-env.sh這兩個文件不用修改,因為里面的配置是:
export JAVA_HOME=${JAVA_HOME} 
 
3.4.5 修改data/hadoop-2.7.1/etc/hadoop/slaves
將原來的localhost刪除,改成如下內容
vim /data/hadoop-2.7.1/etc/hadoop/slaves

最后,將整個hadoop-2.7.1文件夾及其子文件夾使用scp復制到slave1和slave2的相同目錄中:

scp -r /data/hadoop-2.7.1 root@slave1:/data

scp -r /data/hadoop-2.7.1 root@slave2:/data
 

四、運行Hadoop

4.1 格式化NameNode

執行命令:
hadoop namenode -format
執行過程如下圖:

最后的執行結果如下圖:

4.2 啟動NameNode

執行命令如下:
/data/hadoop-2.7.1/sbin/hadoop-daemon.sh start namenode

 

在Master上執行jps命令,得到如下結果:

4.3 啟動DataNode

執行命令如下:
/data/hadoop-2.7.1/sbin/hadoop-daemons.sh start datanode
執行結果如下:

master

slave1

slave2

說明Slave1和Slave2上的DataNode運行正常。
以上啟動NameNode和DataNode的方式,可以用start-dfs.sh腳本替代:
/data/hadoop-2.7.1/sbin/start-all.sh

4.4 運行YARN

運行Yarn也有與運行HDFS類似的方式。啟動ResourceManager使用以下命令:
以上方式我們就不贅述了,來看看使用start-yarn.sh的簡潔的啟動方式:
在Master上執行jps:

說明ResourceManager運行正常。

在兩台Slave上執行jps,也會看到NodeManager運行正常,如下圖:
 

4.5 查看集群是否啟動成功:

  jps

  Master顯示:

  SecondaryNameNode

  ResourceManager

  NameNode

  

  Slave顯示:

  NodeManager

  DataNode


五、測試hadoop

5.1 測試HDFS

最后測試下親手搭建的Hadoop集群是否執行正常,測試的命令如下圖所示:

5.2 查看集群狀態

  /data/hadoop-2.7.1/bin/hdfs dfsadmin -report

  

5.3 測試YARN

可以訪問YARN的管理界面,驗證YARN,如下圖所示:

5.4 測試mapreduce

不想編寫mapreduce代碼。幸好Hadoop安裝包里提供了現成的例子,在Hadoop的share/hadoop/mapreduce目錄下。運行例子:

 5.5 測試查看HDFS:

http://115.29.51.97:50070/dfshealth.html#tab-overview

 

六、配置運行Hadoop中遇見的問題

6.1 JAVA_HOME未設置

啟動的時候報:

則需要/data/hadoop-2.7.1/etc/hadoop/hadoop-env.sh,添加JAVA_HOME路徑

6.2 ncompatible clusterIDs

由於配置Hadoop集群不是一蹴而就的,所以往往伴隨着配置——>運行——>。。。——>配置——>運行的過程,所以DataNode啟動不了時,往往會在查看日志后,發現以下問題:


此問題是由於每次啟動Hadoop集群時,會有不同的集群ID,所以需要清理啟動失敗節點上data目錄(比如我創建的/home/jiaan.gja/hdfs/data)中的數據。

6.3 NativeCodeLoader的警告

在測試Hadoop時,細心的人可能看到截圖中的警告信息:

 

學習本就是一個不斷模仿、練習、再到最后面自己原創的過程。

雖然可能從來不能寫出超越網上通類型同主題博文,但為什么還是要寫?
於自己而言,博文主要是自己總結。假設自己有觀眾,畢竟講是最好的學(見下圖)。

於讀者而言,筆者能在這個過程get到知識點,那就是雙贏了。
當然由於筆者能力有限,或許文中存在描述不正確,歡迎指正、補充!
感謝您的閱讀。如果本文對您有用,那么請點贊鼓勵。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM