本篇介紹在centos7中搭建hadoop2.10高可用集羣,首先準備6臺機器:2臺nn(namenode);4臺dn(datanode);3臺jns(journalnodes)html
IP | hostname | 進程 |
192.168.30.141 | s141 | nn1(namenode),zkfc(DFSZKFailoverController),zk(QuorumPeerMain) |
192.168.30.142 | s142 | dn(datanode), jn(journalnode),zk(QuorumPeerMain) |
192.168.30.143 | s143 | dn(datanode), jn(journalnode),zk(QuorumPeerMain) |
192.168.30.144 | s144 | dn(datanode), jn(journalnode) |
192.168.30.145 | s145 | dn(datanode) |
192.168.30.146 | s146 | nn2(namenode),zkfc(DFSZKFailoverController) |
各個機器 jps進程:java
因爲本人使用的是vmware虛擬機,因此在配置好一臺機器後,使用克隆,克隆出剩餘機器,並修改hostname和IP,這樣每臺機器配置就都統一了每臺機器配置 添加hdfs用戶及用戶組,配置jdk環境,安裝hadoop,本次搭建高可用集羣在hdfs用戶下,能夠參照:centos7搭建hadoop2.10僞分佈模式node
下面是安裝高可用集羣的一些步驟和細節:web
1.設置每臺機器的hostname 和 hostsapache
修改hosts文件,hosts設置有後可使用hostname訪問機器,這樣比較方便,修改以下:bootstrap
127.0.0.1 locahost 192.168.30.141 s141 192.168.30.142 s142 192.168.30.143 s143 192.168.30.144 s144 192.168.30.145 s145 192.168.30.146 s146
2.設置ssh無密登陸,因爲s141和s146都爲namenode,因此要將這兩臺機器無密登陸到全部機器,最好hdfs用戶和root用戶都設置無密登陸centos
咱們將s141設置爲nn1,s146設置爲nn2,就須要s14一、s146可以經過ssh無密登陸到其餘機器,這樣就須要在s141和s146機器hdfs用戶下生成密鑰對,並將s141和s146公鑰發送到其餘機器放到~/.ssh/authorized_keys文件中,更確切的說要將公鑰添加的全部機器上(包括本身)瀏覽器
在s141和s146機器上生成密鑰對:app
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
將id_rsa.pub文件內容追加到s141-s146機器的/home/hdfs/.ssh/authorized_keys中,如今其餘機器暫時沒有authorized_keys文件,咱們就將id_rsa.pub改名爲authorized_keys便可,若是其餘機器已存在authorized_keys文件能夠將id_rsa.pub內容追加到該文件後,遠程複製可使用scp命令:ssh
s141機器公鑰複製到其餘機器
scp id_rsa.pub hdfs@s141:/home/hdfs/.ssh/id_rsa_141.pub scp id_rsa.pub hdfs@s142:/home/hdfs/.ssh/id_rsa_141.pub scp id_rsa.pub hdfs@s143:/home/hdfs/.ssh/id_rsa_141.pub scp id_rsa.pub hdfs@s144:/home/hdfs/.ssh/id_rsa_141.pub scp id_rsa.pub hdfs@s145:/home/hdfs/.ssh/id_rsa_141.pub scp id_rsa.pub hdfs@s146:/home/hdfs/.ssh/id_rsa_141.pub
s146機器公鑰複製到其餘機器
scp id_rsa.pub hdfs@s141:/home/hdfs/.ssh/id_rsa_146.pub scp id_rsa.pub hdfs@s142:/home/hdfs/.ssh/id_rsa_146.pub scp id_rsa.pub hdfs@s143:/home/hdfs/.ssh/id_rsa_146.pub scp id_rsa.pub hdfs@s144:/home/hdfs/.ssh/id_rsa_146.pub scp id_rsa.pub hdfs@s145:/home/hdfs/.ssh/id_rsa_146.pub scp id_rsa.pub hdfs@s146:/home/hdfs/.ssh/id_rsa_146.pub
在每臺機器上可使用cat將祕鑰追加到authorized_keys文件
cat id_rsa_141.pub >> authorized_keys
cat id_rsa_146.pub >> authorized_keys
此時authorized_keys文件權限須要改成644(注意,常常會由於這個權限問題致使ssh無密登陸失敗)
chmod 644 authorized_keys
3.配置hadoop配置文件(${hadoop_home}/etc/hadoop/)
配置細節:
注意: s141和s146具備徹底一致的配置,尤爲是ssh.
1) 配置nameservice
[hdfs-site.xml]
<property>
<name>dfs.nameservices</name>
<value>mycluster</value>
</property>
2) dfs.ha.namenodes.[nameservice ID]
[hdfs-site.xml]
<!-- myucluster下的名稱節點兩個id -->
<property>
<name>dfs.ha.namenodes.mycluster</name>
<value>nn1,nn2</value>
</property>
3) dfs.namenode.rpc-address.[nameservice ID].[name node ID]
[hdfs-site.xml]
配置每一個nn的rpc地址。
<property>
<name>dfs.namenode.rpc-address.mycluster.nn1</name>
<value>s141:8020</value>
</property>
<property>
<name>dfs.namenode.rpc-address.mycluster.nn2</name>
<value>s146:8020</value>
</property>
4) dfs.namenode.http-address.[nameservice ID].[name node ID]
配置webui端口
[hdfs-site.xml]
<property>
<name>dfs.namenode.http-address.mycluster.nn1</name>
<value>s141:50070</value>
</property>
<property>
<name>dfs.namenode.http-address.mycluster.nn2</name>
<value>s146:50070</value>
</property>
5) dfs.namenode.shared.edits.dir
名稱節點共享編輯目錄.選擇三臺journalnode節點,這裏選擇s14二、s14三、s144三臺機器
[hdfs-site.xml]
<property>
<name>dfs.namenode.shared.edits.dir</name>
<value>qjournal://s142:8485;s143:8485;s144:8485/mycluster</value>
</property>
6) dfs.client.failover.proxy.provider.[nameservice ID]
配置一個HA失敗轉移的java類(改配置是固定的),client使用它判斷哪一個節點是激活態。
[hdfs-site.xml]
<property>
<name>dfs.client.failover.proxy.provider.mycluster</name>
<value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
</property>
7) dfs.ha.fencing.methods
腳本列表或者java類,在容災保護激活態的nn.
[hdfs-site.xml]
<property>
<name>dfs.ha.fencing.methods</name>
<value>sshfence</value>
</property>
<property>
<name>dfs.ha.fencing.ssh.private-key-files</name>
<value>/home/hdfs/.ssh/id_rsa</value>
</property>
8) fs.defaultFS
配置hdfs文件系統名稱服務。這裏的mycluster爲上面配置的dfs.nameservices
[core-site.xml]
<property>
<name>fs.defaultFS</name>
<value>hdfs://mycluster</value>
</property>
9) dfs.journalnode.edits.dir
配置JN存放edit的本地路徑。
[hdfs-site.xml]
<property>
<name>dfs.journalnode.edits.dir</name>
<value>/home/hdfs/hadoop/journal</value>
</property>
完整配置文件:
core-site.xml
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>fs.defaultFS</name> <value>hdfs://mycluster/</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/home/hdfs/hadoop</value> </property> </configuration>
hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>dfs.replication</name> <value>3</value> </property> <property> <name>dfs.hosts</name> <value>/opt/soft/hadoop/etc/dfs.include.txt</value> </property> <property> <name>dfs.hosts.exclude</name> <value>/opt/soft/hadoop/etc/dfs.hosts.exclude.txt</value> </property> <property> <name>dfs.nameservices</name> <value>mycluster</value> </property> <property> <name>dfs.ha.namenodes.mycluster</name> <value>nn1,nn2</value> </property> <property> <name>dfs.namenode.rpc-address.mycluster.nn1</name> <value>s141:8020</value> </property> <property> <name>dfs.namenode.rpc-address.mycluster.nn2</name> <value>s146:8020</value> </property> <property> <name>dfs.namenode.http-address.mycluster.nn1</name> <value>s141:50070</value> </property> <property> <name>dfs.namenode.http-address.mycluster.nn2</name> <value>s146:50070</value> </property> <property> <name>dfs.namenode.shared.edits.dir</name> <value>qjournal://s142:8485;s143:8485;s144:8485/mycluster</value> </property> <property> <name>dfs.client.failover.proxy.provider.mycluster</name> <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value> </property> <property> <name>dfs.ha.fencing.methods</name> <value>sshfence</value> </property> <property> <name>dfs.ha.fencing.ssh.private-key-files</name> <value>/home/hdfs/.ssh/id_rsa</value> </property> <property> <name>dfs.journalnode.edits.dir</name> <value>/home/hdfs/hadoop/journal</value> </property> </configuration>
mapred-site.xml
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>mapreduce.framework.name</name> <value>yarn</value> </property> </configuration>
yarn-site.xml
<?xml version="1.0"?> <configuration> <!-- Site specific YARN configuration properties --> <property> <name>yarn.resourcemanager.hostname</name> <value>s141</value> </property> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> </configuration>
4. 部署細節
1)在jn節點分別啓動jn進程(s142,s143,s144)
hadoop-daemon.sh start journalnode
2)啓動jn以後,在兩個NN之間進行disk元數據同步
a)若是是全新集羣,先format文件系統,只須要在一個nn上執行。
[s141|s146]
hadoop namenode -format
b)若是將非HA集羣轉換成HA集羣,複製原NN的metadata到另外一個NN上.
1.步驟一
在s141機器上,將hadoop數據複製到s146對應的目錄下
scp -r /home/hdfs/hadoop/dfs hdfs@s146:/home/hdfs/hadoop/
2.步驟二
在新的nn(未格式化的nn,我這裏是s146)上運行如下命令,實現待命狀態引導。注意:須要s141namenode爲啓動狀態(能夠執行:hadoop-daemon.sh start namenode )。
hdfs namenode -bootstrapStandby
若是沒有啓動s141名稱節點,就會失敗,如圖:
啓動s141名稱節點後,在s141上執行命令
hadoop-daemon.sh start namenode
而後在執行待命引導命令,注意:提示是否格式化,選擇N,如圖:
3. 步驟三
在其中一個NN上執行如下命令,完成edit日誌到jn節點的傳輸。
hdfs namenode -initializeSharedEdits
若是執行過程當中報:java.nio.channels.OverlappingFileLockException 錯誤:
說明namenode在啓動中,須要停掉namenode節點(hadoop-daemon.sh stop namenode)
執行完後查看s142,s143,s144是否有edit數據,這裏查看生產了mycluster目錄,裏面有編輯日誌數據,以下:
4.步驟四
啓動全部節點.
在s141上啓動名稱節點和全部數據節點:
hadoop-daemon.sh start namenode
hadoop-daemons.sh start datanode
在s146上啓動名稱節點
hadoop-daemon.sh start namenode
此時在瀏覽器中訪問 http://192.168.30.141:50070/ 和 http://192.168.30.146:50070/ 你會發現兩個namenode都爲standby
這時須要手動使用命令將其中一個切換爲激活態,這裏將s141(nn1)設置爲active
hdfs haadmin -transitionToActive nn1
此時s141就爲active
hdfs haadmin經常使用命令:
至此手動容災高可用配置完成,可是這種方式不智能,不可以自動感知容災,因此下面介紹自動容災配置
5.自動容災配置
須要引入zookeeper quarum 和 zk 容災控制器(ZKFC)兩個組件
搭建zookeeper集羣,選擇s141,s142,s143三臺機器,下載 zookeeper:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.6
1) 解壓zookeeper:
tar -xzvf apache-zookeeper-3.5.6-bin.tar.gz -C /opt/soft/zookeeper-3.5.6
2) 配置環境變量,在/etc/profile中添加zk環境變量,並從新編譯/etc/profile文件
source /etc/profile
3) 配置zk配置文件,三臺機器配置文件統一
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/home/hdfs/zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 server.1=s141:2888:3888 server.2=s142:2888:3888 server.3=s143:2888:3888
4)分別
在s141的/home/hdfs/zookeeper(在zoo.cfg配置文件中配置的dataDir路徑)目錄下建立myid文件,值爲1(對應zoo.cfg配置文件中的server.1)
在s142的/home/hdfs/zookeeper(在zoo.cfg配置文件中配置的dataDir路徑)目錄下建立myid文件,值爲2(對應zoo.cfg配置文件中的server.2)
在s143的/home/hdfs/zookeeper(在zoo.cfg配置文件中配置的dataDir路徑)目錄下建立myid文件,值爲3(對應zoo.cfg配置文件中的server.3)
5) 分別在每臺機器上啓動zk
zkServer.sh start
啓動成功會出現zk進程:
配置hdfs相關配置:
1)中止hdfs全部進程
stop-all.sh
2) 配置hdfs-site.xml,啓用自動容災.
[hdfs-site.xml] <property> <name>dfs.ha.automatic-failover.enabled</name> <value>true</value> </property>
3) 配置core-site.xml,指定zk的鏈接地址.
<property> <name>ha.zookeeper.quorum</name> <value>s141:2181,s142:2181,s143:2181</value> </property>
4) 分發以上兩個文件到全部節點。
5) 在其中的一臺NN(s141),在ZK中初始化HA狀態
hdfs zkfc -formatZK
出現以下結果說明成功:
也可去zk中查看:
6) 啓動hdfs集羣
start-dfs.sh
查看各個機器進程:
啓動成功,再看一下webui
s146爲激活態
s141爲待命態
至此hadoop 自動容災HA搭建完成