ZooKeeper從入門到精通13:使用ZooKeeper實(shí)現(xiàn)Hadoop的HA

Hadoop本身并沒有提供HA的功能,需要借助ZooKeeper來實(shí)現(xiàn)Hadoop的HA功能。Hadoop的HA搭建過程是所有Hadoop生態(tài)圈組件中最復(fù)雜的铺韧,本節(jié)就來詳細(xì)說明如何使用ZooKeeper來搭建Hadoop的HA環(huán)境。

環(huán)境說明:

bigdata131 192.168.126.131
bigdata132 192.168.126.132
bigdata133 192.168.126.133
bigdata134 192.168.126.134

安裝介質(zhì)下載:

zookeeper-3.4.10.tar.gz 提取碼:nvv2
hadoop-2.7.3.tar.gz 提取碼:r8xo

1.使用ZooKeeper實(shí)現(xiàn)Hadoop的HA的原理

image

通過上圖的分析,使用ZooKeeper集群搭建一個(gè)最小規(guī)模的Hadoop HA集群至少需要4臺(tái)機(jī)器:

Zookeeper集群:

bigdata131
bigdata132
bigdata133

Hadoop集群:

bigdata131 NameNode1 ResourceManager1 Journalnode
bigdata132 NameNode2 ResourceManager2 Journalnode
bigdata133 DataNode1
bigdata134 DataNode2

2.搭建ZooKeeper集群

參考文章《ZooKeeper從入門到精通9:ZooKeeper環(huán)境搭建之集群模式》捧存。

3.搭建Hadoop的HA集群環(huán)境

以下步驟在bigdata131節(jié)點(diǎn)上執(zhí)行:

3.1上傳Hadoop安裝包

使用winscp工具將Hadoop安裝包上傳到bigdata131節(jié)點(diǎn)的/root/tools/目錄中,該目錄是事先創(chuàng)建的担败。

# ls /root/tools/
hadoop-2.7.3.tar.gz

3.2解壓Hadoop安裝包

進(jìn)入/root/tools/目錄昔穴,將hadoop安裝包解壓到/root/trainings/目錄中,該目錄也是事先創(chuàng)建的提前。

# cd /root/tools/
# tar -zxvf hadoop-2.7.3.tar.gz -C /root/trainings/

3.3配置Hadoop環(huán)境變量(4臺(tái)主機(jī)上都做一遍)

# cd /root/trainings/hadoop-2.7.3/
# pwd
/root/trainings/hadoop-2.7.3
# vim /root/.bash_profile
在文件末尾追加如下內(nèi)容:
HADOOP_HOME=/root/trainings/hadoop-2.7.3
export HADOOP_HOME
PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH
export PATH

按Esc:wq保存退出吗货,使用source命令使配置文件立即生效:

# source /root/.bash_profile

3.4配置Hadoop HA模式的參數(shù)

進(jìn)入Hadoop配置文件目錄:

# cd /root/trainings/hadoop-2.7.3/etc/hadoop/

(1)配置hadoop-env.sh文件:

# echo $JAVA_HOME
/root/trainings/jdk1.8.0_144
# vim hadoop-env.sh
#export JAVA_HOME=${JAVA_HOME}
export JAVA_HOME=/root/trainings/jdk1.8.0_144

(2)配置hdfs-site.xml文件(配置nameservice中有幾個(gè)namenode):

# vim hdfs-site.xml
<configuration>

<property>
<name>dfs.nameservices</name>
<value>ns1</value>
</property>

<property>
<name>dfs.ha.namenodes.ns1</name>
<value>nn1,nn2</value>
</property>

<property>
<name>dfs.namenode.rpc-address.ns1.nn1</name>
<value>bigdata131:9000</value>
</property>

<property>
<name>dfs.namenode.http-address.ns1.nn1</name>
<value>bigdata131:50070</value>
</property>

<property>
<name>dfs.namenode.rpc-address.ns1.nn2</name>
<value>bigdata132:9000</value>
</property>

<property>
<name>dfs.namenode.http-address.ns1.nn2</name>
<value>bigdata132:50070</value>
</property>

<property>
<name>dfs.namenode.shared.edits.dir</name>
<value>qjournal://bigdata131:8485;bigdata132:8485;/ns1</value>
</property>

<property>
<name>dfs.journalnode.edits.dir</name>
<value>/root/trainings/hadoop-2.7.3/journal</value>
</property>

<property>
<name>dfs.ha.automatic-failover.enabled</name>
<value>true</value>
</property>

<property>
<name>dfs.client.failover.proxy.provider.ns1</name>
<value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
</property>

<property>
<name>dfs.ha.fencing.methods</name>
<value>
sshfence
shell(/bin/true)
</value>
</property>

<property>
<name>dfs.ha.fencing.ssh.private-key-files</name>
<value>/root/.ssh/id_rsa</value>
</property>

<property>
<name>dfs.ha.fencing.ssh.connect-timeout</name>
<value>30000</value>
</property>
</configuration>

(3)配置core-site.xml文件:

# mkdir /root/trainings/hadoop-2.7.3/tmp
# vim core-site.xml
<configuration>

<property>
<name>fs.defaultFS</name>
<value>hdfs://ns1</value>
</property>

<property>
<name>hadoop.tmp.dir</name>
<value>/root/trainings/hadoop-2.7.3/tmp</value>
</property>

<property>
<name>ha.zookeeper.quorum</name>
<value>bigdata131:2181,bigdata132:2181,bigdata133:2181</value>
</property>
</configuration>

(4)配置mapred-site.xml文件:
將模板文件mapred-site.xml.template拷貝一份重命名為mapred-site.xml然后編輯:

# cp mapred-site.xml.template mapred-site.xml
# vim mapred-site.xml
<configuration>

<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>

(5)配置yarn-site.xml文件:

# vim yarn-site.xml
<configuration>

<property>
<name>yarn.resourcemanager.ha.enabled</name>
<value>true</value>
</property>

<property>
<name>yarn.resourcemanager.cluster-id</name>
<value>yrc</value>
</property>

<property>
<name>yarn.resourcemanager.ha.rm-ids</name>
<value>rm1,rm2</value>
</property>

<property>
<name>yarn.resourcemanager.hostname.rm1</name>
<value>bigdata131</value>
</property>
<property>
<name>yarn.resourcemanager.hostname.rm2</name>
<value>bigdata132</value>
</property>

<property>
<name>yarn.resourcemanager.zk-address</name>
<value>bigdata131:2181,bigdata132:2181,bigdata133:2181</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>

(6)配置slaves文件:

# vim slaves
bigdata133
bigdata134

3.5將配置好的hadoop拷貝到其他節(jié)點(diǎn)

[root@bigdata131 ~]# scp -r /root/trainings/hadoop-2.7.3/ root@bigdata132:/root/trainings/

[root@bigdata131 ~]# scp -r /root/trainings/hadoop-2.7.3/ root@bigdata133:/root/trainings/
[root@bigdata131 ~]# scp -r /root/trainings/hadoop-2.7.3/ root@bigdata134:/root/trainings/

3.6啟動(dòng)Zookeeper集群

[root@bigdata131 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@bigdata132 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@bigdata133 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

3.7在bigdata131和bigdata132上啟動(dòng)journalnode

[root@bigdata131 ~]# hadoop-daemon.sh start journalnode
starting journalnode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-journalnode-bigdata131.out
[root@bigdata132 ~]# hadoop-daemon.sh start journalnode
starting journalnode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-journalnode-bigdata132.out

3.8格式化HDFS和ZooKeeper(在bigdata131上執(zhí)行)

[root@bigdata131 ~]# hdfs namenode -format
18/12/02 00:08:47 INFO common.Storage: Storage directory /root/trainings/hadoop-2.7.3/tmp/dfs/name has been successfully formatted.
[root@bigdata131 ~]# scp -r /root/trainings/hadoop-2.7.3/tmp root@bigdata132:/root/trainings/hadoop-2.7.3/
# 格式化zookeeper
[root@bigdata131 ~]# hdfs zkfc -formatZK
18/12/02 00:09:59 INFO ha.ActiveStandbyElector: Successfully created /hadoop-ha/ns1 in ZK.

3.9啟動(dòng)Hadoop HA集群

在bigdata131上啟動(dòng)Hadoop集群:

[root@bigdata131 ~]# start-all.sh
This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh
Starting namenodes on [bigdata131 bigdata132]
bigdata131: starting namenode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-namenode-bigdata131.out
bigdata132: starting namenode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-namenode-bigdata132.out
bigdata133: starting datanode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-datanode-bigdata133.out
bigdata134: starting datanode, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-datanode-bigdata134.out
Starting journal nodes [bigdata131 bigdata132 ]
bigdata131: journalnode running as process 1275. Stop it first.
bigdata132: journalnode running as process 1265. Stop it first.
Starting ZK Failover Controllers on NN hosts [bigdata131 bigdata132]
bigdata131: starting zkfc, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-zkfc-bigdata131.out
bigdata132: starting zkfc, logging to /root/trainings/hadoop-2.7.3/logs/hadoop-root-zkfc-bigdata132.out

starting yarn daemons
starting resourcemanager, logging to /root/trainings/hadoop-2.7.3/logs/yarn-root-resourcemanager-bigdata131.out
bigdata133: starting nodemanager, logging to /root/trainings/hadoop-2.7.3/logs/yarn-root-nodemanager-bigdata133.out
bigdata134: starting nodemanager, logging to /root/trainings/hadoop-2.7.3/logs/yarn-root-nodemanager-bigdata134.out

[root@bigdata131 ~]# jps
1232 QuorumPeerMain
1939 ResourceManager
1524 NameNode
2200 Jps
1275 JournalNode
1839 DFSZKFailoverController

[root@bigdata132 ~]# jps
1217 QuorumPeerMain
1265 JournalNode
1346 NameNode
1461 DFSZKFailoverController
1518 Jps

[root@bigdata133 ~]# jps
1365 NodeManager
1213 QuorumPeerMain
1469 Jps
1263 DataNode

[root@bigdata134 ~]# jps
1303 NodeManager
1435 Jps
1228 DataNode

在bigdata132上需要單獨(dú)啟動(dòng)ResourceManager:

[root@bigdata132 ~]# yarn-daemon.sh start resourcemanager
starting resourcemanager, logging to /root/trainings/hadoop-2.7.3/logs/yarn-root-resourcemanager-bigdata132.out
[root@bigdata132 ~]# jps
1217 QuorumPeerMain
1265 JournalNode
1346 NameNode
1603 Jps
1556 ResourceManager
1461 DFSZKFailoverController

4.測(cè)試Hadoop的HA集群環(huán)境

(1)正常情況下

image
image
image

可以看到:ZooKeeper中ns1當(dāng)前Active的是bigdata131;通過hadoop網(wǎng)頁(yè)發(fā)現(xiàn):bigdata131是active狀態(tài)狈网,bigdata132是standby狀態(tài)宙搬。

(2)殺死bigdata131上的NameNode進(jìn)程,刷新ZooKeeper和網(wǎng)頁(yè)觀察變化

[root@bigdata131 ~]# jps
1232 QuorumPeerMain
1939 ResourceManager
1524 NameNode
2266 Jps
1275 JournalNode
1839 DFSZKFailoverController
[root@bigdata131 ~]# kill -9 1524

image
image
image

可以看到:ZooKeeper中ns1當(dāng)前Active的變成了bigdata132拓哺;通過hadoop網(wǎng)頁(yè)發(fā)現(xiàn):bigdata131已經(jīng)無法訪問勇垛,bigdata132變成active狀態(tài)。

因此Hadoop HA能夠?qū)崿F(xiàn)正確的失敗遷移功能士鸥,可以更加高可用的對(duì)外提供Hadoop服務(wù)了闲孤。

至此,使用ZooKeeper搭建Hadoop的HA環(huán)境已經(jīng)介紹完畢烤礁。祝你玩得愉快崭放!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末哨苛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子币砂,更是在濱河造成了極大的恐慌建峭,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件决摧,死亡現(xiàn)場(chǎng)離奇詭異亿蒸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)掌桩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門边锁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人波岛,你說我怎么就攤上這事茅坛。” “怎么了则拷?”我有些...
    開封第一講書人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵贡蓖,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我煌茬,道長(zhǎng)斥铺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任坛善,我火速辦了婚禮晾蜘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘眠屎。我一直安慰自己剔交,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開白布改衩。 她就那樣靜靜地躺著省容,像睡著了一般。 火紅的嫁衣襯著肌膚如雪燎字。 梳的紋絲不亂的頭發(fā)上腥椒,一...
    開封第一講書人閱讀 49,079評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音候衍,去河邊找鬼笼蛛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蛉鹿,可吹牛的內(nèi)容都是我干的滨砍。 我是一名探鬼主播,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼惋戏!你這毒婦竟也來了领追?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤响逢,失蹤者是張志新(化名)和其女友劉穎绒窑,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舔亭,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡些膨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钦铺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片订雾。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖矛洞,靈堂內(nèi)的尸體忽然破棺而出洼哎,到底是詐尸還是另有隱情,我是刑警寧澤沼本,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布噩峦,位于F島的核電站,受9級(jí)特大地震影響擅威,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜冈钦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一郊丛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瞧筛,春花似錦厉熟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至乍炉,卻和暖如春绢片,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背岛琼。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工底循, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人槐瑞。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓熙涤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子祠挫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容