1.简介:
hadoop中NameNode可以有多个(目前只支持2个)。每一个都有相同的职能。一个是active状态的,一个是standby状态的。当集群运行时,只有active状态的NameNode是正常工作的,standby状态的NameNode是处于待命状态的,时刻同步active状态NameNode的数据。一旦active状态的NameNode不能工作,standby状态的NameNode就可以转变为active状态的,就可以继续工作了。
2个NameNode的数据其实是实时共享的。新HDFS采用了一种共享机制,Quorum Journal Node(JournalNode)集群或者Nnetwork File System(NFS)进行共享。NFS是操作系统层面的,JournalNode是hadoop层面的,我们这里使用JournalNode集群进行数据共享(这也是主流的做法)。
JournalNode的架构图如下:
两个NameNode为了数据同步,会通过一组称作JournalNodes的独立进程进行相互通信。当active状态的NameNode的命名空间有任何修改时,会告知大部分的JournalNodes进程。standby状态的NameNode有能力读取JNs中的变更信息,并且一直监控edit log的变化,把变化应用于自己的命名空间。standby可以确保在集群出错时,命名空间状态已经完全同步了。
对于HA集群而言,确保同一时刻只有一个NameNode处于active状态是至关重要的。否则,两个NameNode的数据状态就会产生分歧,可能丢失数据,或者产生错误的结果。为了保证这点,这就需要利用使用ZooKeeper了。首先HDFS集群中的两个NameNode都在ZooKeeper中注册,当active状态的NameNode出故障时,ZooKeeper能检测到这种情况,它就会自动把standby状态的NameNode切换为active状态。
hadoop-ha包含HDFS的HA和YARN的HA,下面就2个部件的HA进行搭建。
JournalNodes:namenode数据共享
ZooKeeper:心跳检测
hadoop-ha包含HDFS的HA和YARN的HA,
2.环境准备 (ps: The picture is replicating)
(1)各服务器其修改主机名,添加hosts文件,关闭防火墙
[root@c7001 ~]# cat >> /etc/hosts << EOF
192.168.16.135 c7001
192.168.16.80 c7002
192.168.16.95 c7003
192.168.16.97 c7004
192.168.16.101 c7005
EOF
(2)c7001配置ssh免密登陆,用于启动集群
ssh-keygen -t rsa
sh-copy-id -i ~/.ssh/id_rsa.pub c7001
ssh-copy-id -i ~/.ssh/id_rsa.pub c7002
ssh-copy-id -i ~/.ssh/id_rsa.pub c7003
ssh-copy-id -i ~/.ssh/id_rsa.pub c7004
ssh-copy-id -i ~/.ssh/id_rsa.pub c7005
(3) 各服务器配置jdk1.7+
[root@c7001 ~]# tar zxf jdk-8u171-linux-x64.tar.gz -C /opt/
[root@c7001 opt]# mv jdk1.8.0_171/ jdk1.8
[root@c7001 opt]# vim /etc/profile
export JAVA_HOME=/opt/jdk1.8
export PATH=$PATH:$JAVA_HOME/bin
[root@c7001 ~] source /etc/profile
[root@c7001 opt]# java -version
java version "1.8.0_171
(4)安装Hadoop集群
[root@c7001 ~]# tar zxf hadoop-2.7.6.tar.gz -C /opt/
在c7001终端修改hadoop配置文件,这里需要修改的有
core-site.xml、hdfs-site.xml、mapreduce-site.xml、yarn-site.xml、hadoop-env.sh、mapred-env.sh、yarn-env.sh这7个文件
core-site.xml
fs.defaultFS
hdfs://bdcluster
hadoop.tmp.dir
/opt/hadoop-2.7.6/tmp
ha.zookeeper.quorum
c7003:2181,c7004:2181,c7005:2181
ha.zookeeper.session-timeout.ms
3000
hdfs-site.xml
dfs.nameservices
bdcluster
dfs.ha.namenodes.bdcluster
nn1,nn2
dfs.namenode.rpc-address.bdcluster.nn1
c7001:9000
dfs.namenode.rpc-address.bdcluster.nn2
c7002:9000
dfs.namenode.http-address.bdcluster.nn1
c7001:50070
dfs.namenode.http-address.bdcluster.nn2
c7002:50070
dfs.namenode.shared.edits.dir
qjournal://c7003:8485;c7004:8485;c7005:8485/bdcluster
dfs.journalnode.edits.dir
/opt/hadoop-2.7.6/tmp/journal
dfs.ha.automatic-failover.enabled
true
dfs.client.failover.proxy.provider.bdcluster
org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider
dfs.ha.fencing.methods
sshfence
shell(/bin/true)
dfs.ha.fencing.ssh.private-key-files
/root/.ssh/id_rsa
dfs.ha.fencing.ssh.connect-timeout
30000
dfs.namenode.name.dir
file:///opt/hadoop-2.7.6/hdfs/name
dfs.datanode.data.dir
file:///opt/hadoop-2.7.6/hdfs/data
dfs.replication
3
mapred-site.xml
mapreduce.framework.name
yarn
mapreduce.jobhistory.address
0.0.0.0:10020
mapreduce.jobhistory.webapp.address
0.0.0.0:19888
yarn-site.xml
yarn.resourcemanager.ha.enabled
true
yarn.resourcemanager.recovery.enabled
true
yarn.resourcemanager.cluster-id
yrc
yarn.resourcemanager.ha.rm-ids
rm1,rm2
yarn.resourcemanager.hostname.rm1
c7001
yarn.resourcemanager.hostname.rm2
c7002
ha.zookeeper.quorum
c7003:2181,c7004:2181,c7005:2181
!--配置与zookeeper的连接地址-->
yarn.resourcemanager.zk-state-store.address
c7003:2181,c7004:2181,c7005:2181
yarn.resourcemanager.store.class
org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore
yarn.resourcemanager.zk-address
c7003:2181,c7004:2181,c7005:2181
yarn.resourcemanager.ha.automatic-failover.zk-base-path
/yarn-leader-election
Optionalsetting.Thedefaultvalueis/yarn-leader-election
yarn.nodemanager.aux-services
mapreduce_shuffle
设置环境变量文件
hadoop-env.sh & mapred-env.sh & yarn-env.s
export JAVA_HOME=/opt/jdk1.8
export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export HADOOP_HOME=/opt/hadoop-2.7.6
export HADOOP_PID_DIR=/opt/hadoop-2.7.6/pids
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_OPTS="$HADOOP_OPTS-Djava.library.path=$HADOOP_HOME/lib/native"
export HADOOP_PREFIX=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
export HDFS_CONF_DIR=$HADOOP_HOME/etc/hadoop
export YARN_CONF_DIR=$HADOOP_HOME/etc/hadoop
export JAVA_LIBRARY_PATH=$HADOOP_HOME/lib/native
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
slaves
c7001
c7002
c7003
c7004
c7005
配置完毕后发送到其他主机
scp -r /opt/hadoop-2.7.6/etc/hadoop root@c7002:/opt/hadoop-2.7.6/etc/
scp -r /opt/hadoop-2.7.6/etc/hadoop root@c7003:/opt/hadoop-2.7.6/etc/
scp -r /opt/hadoop-2.7.6/etc/hadoop root@c7004:/opt/hadoop-2.7.6/etc/
scp -r /opt/hadoop-2.7.6/etc/hadoop root@c7005:/opt/hadoop-2.7.6/etc/
(5)启动Hadoop集群 (jps 确认进程启动)
先启动zookeeper集群,部署看上一篇
https://www.jianshu.com/p/f562d6d85f93
分别在c7003、c7004、c7005上执行如下命令启动zookeeper集群;
[root@c7003 bin]$ sh zkServer.sh start
[root@c7004 bin]$ sh zkServer.sh start
[root@c7005 bin]$ sh zkServer.sh start
启动journalnode集群
在c7001上执行如下命令完成JournalNode集群的启动
[root@c7001 hadoop-2.7.6]$ sbin/hadoop-daemons.sh start journalnode
格式化zkfc,让在zookeeper中生成ha节点
[root@c7001 hadoop-2.7.6]# bin/hdfs zkfc -formatZK
格式成功后,查看zookeeper中可以看到
[root@c7003 bin]# ./zkCli.sh -server c7003:2181
[zk: localhost:2181(CONNECTED) 1] ls /hadoop-ha
[bdcluster]
格式化hdfs
[root@c7001 hadoop-2.7.6]# bin/hadoop namenode -format
启动NameNode
首先在c7001上启动active节点,在c7001上执行如下命令
[root@c7001 hadoop-2.7.6]# sbin/hadoop-daemons.sh start namenode
在c7002上同步namenode的数据,同时启动standby的namenod,命令如下
把NameNode的数据同步到c7002上
[root@c7002 hadoop-2.7.6]# bin/hdfs namenode -bootstrapStandby
启动c7002上的namenode作为standby
[root@c7002 hadoop-2.7.6]$ sbin/hadoop-daemon.sh start namenode
启动datanode
在c7001上执行如下命令
[root@c7001 hadoop-2.7.6]$ sbin/hadoop-daemons.sh start datanode
启动yarn
在作为资源管理器上的机器上启动,我这里是c7001,执行如下命令完成year的启动
[root@c7001 hadoop-2.7.6]$ sbin/start-yarn.sh
启动ZKFC
在c7001上执行如下命令,完成ZKFC的启动
[root@c7001 hadoop-2.7.6]$ sbin/hadoop-daemons.sh start zkfc
全部启动完后分别在c7001、c7002、c7003、c7004、c7005上执行jps是可以看到下面这些进程的
各节点jps PID进程
#c7001上的java PID进程
[root@c7001 hadoop-2.7.6]$ jps
7857 DataNode
7270 JournalNode
8118 NodeManager
8550 DFSZKFailoverController
8007 ResourceManager
8968 NameNode
9065 Jps
#c7002上的java PID进程
[root@c7002 hadoop-2.7.6]$ jps
6929 DFSZKFailoverController
6738 NodeManager
6441 NameNode
6603 DataNode
6221 JournalNode
7615 Jps
#c7003上的java PID进程
[root@c7003 hadoop-2.7.6]$ jps
6040 DataNode
6473 Jps
4764 QuorumPeerMain
5870 JournalNode
6175 NodeManager
#c7004上的java PID进程
[root@c7004 hadoop-2.7.6]$ jps
4739 QuorumPeerMain
5875 JournalNode
6180 NodeManager
6475 Jps
6046 DataNode
#c7005上的java PID进程
6227 NodeManager
6518 Jps
6091 DataNode
5916 JournalNode
4813 QuorumPeerMain
测试访问web
测试高可用
此时在c7001上执行如下命令关闭c7001上的namenode
[root@c7001 hadoop-2.7.6]$ sbin/hadoop-daemon.sh stop namenode
再次查看c7002上的namenode,发现自动切换为active了!
各服务端口(参考)
http://www.cnblogs.com/netbloomy/p/6660131.html