注:以下配置描述的是HDFS的QJM方式的HA配置。
这里我使用了三台机器(在笔记本上使用vmware创建了三个虚拟机来实现)部署zookeeper集群,机器IP分别是:
l 192.168.111.130(hostname:hd0)
l 192.168.111.131(hostname:hd1)
l 192.168.111.132(hostname:hd2)
首先建立zookeeper的数据目录,比如:
mkdir -p /opt/hadoop/data/zookeeper
同时建立日志存放目录:
mkdir –p /opt/hadoop/logs/zookeeper
然后修改环境变量(比如修改~/.profile),增加如下变量:
export ZOO_HOME=/opt/hadoop/apps/zookeeper-3.4.5
export ZOO_LOG_DIR=/opt/hadoop/logs/zookeeper
在$ZOO_HOME/conf下创建配置文件:
touch zoo.cfg
在zoo.cfg文件是加入以下配置:
tickTime=2000 dataDir=/opt/hadoop/data/zookeeper clientPort=31315 initLimit=5 syncLimit=2 server.1=192.168.111.130:31316:31317 server.2=192.168.111.131:31316:31317 server.3=192.168.111.132:31316:31317
在三台服务器的目录/opt/hadoop/data/zookeeper下分别创建一个叫myid的文件,内容分别是1,2,3,如:
#在192.168.111.130上执行如下命令
echo 1 >/opt/hadoop/data/zookeeper/myid
#在192.168.111.131上执行如下命令
echo 2 >/opt/hadoop/data/zookeeper/myid
#在192.168.111.132上执行如下命令
echo 3 >/opt/hadoop/data/zookeeper/myid
最后就是分别启动zookeeper服务了:
cd $ZOO_HOME
./bin/zkServer.sh start
通过jps命令可以检查是否启动成功:
hd@hd0:/opt/hadoop/apps/zookeeper-3.4.5$ jps
1239 QuorumPeerMain
看到QuorumPeerMain进程就表示zookeeper启动成功了。
测试zookeeper集群是否建立成功,在$ZOO_HOME目录下执行以下命令即可,如无报错表示集群创建成功:
./bin/zkCli.sh -server localhost:31315
<configuration> <property> <name>fs.defaultFS</name> <value>hdfs://mycluster</value> </property> <property> <name>io.file.buffer.size</name> <value>131072</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/opt/hadoop/data/hadoop/temp</value> <description>Abase for other temporarydirectories.</description> </property> <property> <name>hadoop.proxyuser.hadoop.hosts</name> <value>*</value> </property> <property> <name>hadoop.proxyuser.hadoop.groups</name> <value>*</value> </property> <property> <name>ha.zookeeper.quorum</name> <value>192.168.111.130:31315,192.168.111.131:31315,192.168.111.132:31315</value> </property> <property> <name>ha.zookeeper.session-timeout.ms</name> <value>1000</value> <description>ms</description> </property> </configuration>
<?xml version="1.0"encoding="UTF-8"?> <?xml-stylesheet type="text/xsl"href="configuration.xsl"?> <configuration> <property> <name>dfs.namenode.name.dir</name> <value>/opt/hadoop/data/hadoop/dfs/name</value> </property> <property> <name>dfs.datanode.data.dir</name> <value>/opt/hadoop/data/hadoop/dfs/data</value> </property> <property> <name>dfs.replication</name> <value>1</value> </property> <property> <name>dfs.webhdfs.enabled</name> <value>true</value> </property> <property> <name>dfs.permissions</name> <value>false</value> </property> <property> <name>dfs.permissions.enabled</name> <value>false</value> </property> <property> <name>dfs.nameservices</name> <value>mycluster</value> <description>Logical name for this newnameservice</description> </property> <property> <name>dfs.ha.namenodes.mycluster</name> <value>nn1,nn2</value> <description>Unique identifiers for each NameNode in thenameservice</description> </property> <property> <name>dfs.namenode.rpc-address.mycluster.nn1</name> <value>192.168.111.130:8020</value> </property> <property> <name>dfs.namenode.rpc-address.mycluster.nn2</name> <value>192.168.111.132:8020</value> </property> <property> <name>dfs.namenode.servicerpc-address.mycluster.nn1</name> <value>192.168.111.130:53310</value> </property> <property> <name>dfs.namenode.servicerpc-address.mycluster.nn2</name> <value>192.168.111.132:53310</value> </property> <property> <name>dfs.namenode.http-address.mycluster.nn1</name> <value>192.168.111.130:50070</value> </property> <property> <name>dfs.namenode.http-address.mycluster.nn2</name> <value>192.168.111.132:50070</value> </property> <property> <name>dfs.namenode.shared.edits.dir</name> <value>qjournal://192.168.111.130:8485;192.168.111.131:8485;192.168.111.132: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/hadoop/.ssh/id_rsa_nn1</value> </property> <property> <name>dfs.ha.fencing.ssh.connect-timeout</name> <value>30000</value> </property> <property> <name>dfs.journalnode.edits.dir</name> <value>/opt/hadoop/data/hadoop/journaldata</value> </property> <property> <name>dfs.ha.automatic-failover.enabled</name> <value>true</value> </property> <property> <name>ha.failover-controller.cli-check.rpc-timeout.ms</name> <value>60000</value> </property> <property> <name>ipc.client.connect.timeout</name> <value>60000</value> </property> <property> <name>dfs.image.transfer.bandwidthPerSec</name> <value>4194304</value> </property> </configuration>
在上面的配置中有一个地方要特别说明一下,dfs.ha.fencing.ssh.private-key-files这里指向的是一个本地文件。上面我们是配置了两个namenode来实现HDFS的HA的,分别是nn1和nn2,在nn2的~/.ssh/目录下需要将nn1的~/.ssh/目录下的id_rsa文件copy过来,并且应该重命名成如id_rsa_nn1这样的文件名,以免覆盖了本地的文件。
<?xml version="1.0"?> <configuration> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> <property> <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name> <value>org.apache.hadoop.mapred.ShuffleHandler</value> </property> <property> <name>yarn.resourcemanager.address</name> <value>192.168.111.130:8032</value> </property> <property> <name>yarn.resourcemanager.scheduler.address</name> <value>192.168.111.130:8030</value> </property> <property> <name>yarn.resourcemanager.resource-tracker.address</name> <value>192.168.111.130:8031</value> </property> <property> <name>yarn.resourcemanager.admin.address</name> <value>192.168.111.130:8033</value> </property> <property> <name>yarn.resourcemanager.webapp.address</name> <value>192.168.111.130:8088</value> </property> </configuration>
安装环境:Ubuntu 12.04.3 LTS
硬件环境:三台服务器,两台namenode,一台datanode,分别如下:
ü 192.168.111.130,主namenode,zookeeper,journalnode,zkfc
ü 192.168.111.131,datanode,zookeeper,journalnode
ü 192.168.111.132,备namenode,zookeeper,journalnode,zkfc
0、首先把各个zookeeper起来,如果zookeeper集群还没有启动的话。
./bin/zkServer.sh start
1、然后在某一个namenode节点执行如下命令,创建命名空间
./bin/hdfs zkfc -formatZK
2、在各个节点用如下命令启日志程序
./sbin/hadoop-daemon.sh start journalnode
3、在主namenode节点用./bin/hadoopnamenode -format格式化namenode和journalnode目录
./bin/hadoop namenode -format mycluster
4、在主namenode节点启动./sbin/hadoop-daemon.shstart namenode进程
./sbin/hadoop-daemon.sh start namenode
5、在备节点执行第一行命令,这个是把备namenode节点的目录格式化并把元数据从主namenode节点copy过来,并且这个命令不会把journalnode目录再格式化了!然后用第二个命令启动备namenode进程!
./bin/hdfs namenode –bootstrapStandby
./sbin/hadoop-daemon.sh start namenode
6、在两个namenode节点都执行以下命令
./sbin/hadoop-daemon.sh start zkfc
7、在所有datanode节点都执行以下命令启动datanode
./sbin/hadoop-daemon.sh start datanode
下次启动的时候,就直接执行以下命令就可以全部启动所有进程和服务了:
./sbin/start-dfs.sh
然后访问以下两个地址查看启动的两个namenode的状态:
http://192.168.111.130:50070/dfshealth.jsp
http://192.168.111.132:50070/dfshealth.jsp
停止所有HDFS相关的进程服务,执行以下命令:
./sbin/stop-dfs.sh
在任意一台namenode机器上通过jps命令查找到namenode的进程号,然后通过kill -9的方式杀掉进程,观察另一个namenode节点是否会从状态standby变成active状态。
hd@hd0:/opt/hadoop/apps/hadoop$ jps 1686 JournalNode 1239 QuorumPeerMain 1380 NameNode 2365 Jps 1863 DFSZKFailoverController hd@hd0:/opt/hadoop/apps/hadoop$ kill -9 1380
然后观察原来是standby状态的namenode机器的zkfc日志,若最后一行出现如下日志,则表示切换成功:
2013-12-31 16:14:41,114 INFOorg.apache.hadoop.ha.ZKFailoverController: Successfully transitioned NameNodeat hd0/192.168.111.130:53310 to active state
这时再通过命令启动被kill掉的namenode进程
./sbin/hadoop-daemon.sh start namenode
对应进程的zkfc最后一行日志如下:
2013-12-31 16:14:55,683 INFOorg.apache.hadoop.ha.ZKFailoverController: Successfully transitioned NameNodeat hd2/192.168.111.132:53310 to standby state
可以在两台namenode机器之间来回kill掉namenode进程以检查HDFS的HA配置!