配置ZooKeeper
一个分布式运行的HBase依赖于一个ZooKeeper集群。所有节点和客户端都必须能够访问ZooKeeper。在默认的情况下HBase会管理一个ZooKeeper集群。这个集群会随着HBase的启动而启动。当然,也可以自己管理一个ZooKeeper集群,但需要配置HBase。需要修改conf/hbase-env.sh里面的HBASE_MANAGES_ZK 来切换。这个值默认是“真”的,作用是使启动HBase的时候同时启动ZooKeeper。
当HBase管理ZooKeeper的时候,可以通过修改zoo.cfg来配置ZooKeeper,一个更加简单的方法是在conf/hbase-site.xml里修改ZooKeeper的配置。ZooKeeper的配置是作为property写在 hbase-site.xml里面的,名字是hbase.zookeeper.property。例如,clientPort配置在xml中的名字是hbase.zookeeper.property.clientPort,所有默认值都是HBase决定的,包括ZooKeeper,可以查找hbase.zookeeper.property前缀,找到关于ZooKeeper的配置。
对于ZooKeepr的配置,至少要在hbase-site.xml中列出ZooKeepr的ensembleservers,具体的字段是hbase.zookeeper.quorum。该这个字段的默认值是localhost,这个值对于分布式应用显然是不可行的(远程连接无法使用)。
运行一个ZooKeeper也是可以的,但是在生产环境中,最好部署3、5、7个节点。部署的节点越多,可靠性就越高,当然只能部署奇数个。需要给每个ZooKeeper 1GB左右的内存,如果可能,最好有独立的磁盘(独立磁盘可以确保ZooKeeper是高性能的)。如果集群负载很重,不要把ZooKeeper和RegionServer运行在同一台机器上。就像DataNodes和TaskTrackers一样。
例如,HBase管理着的ZooKeeper集群节点rs{1,2,3,4,5}.example.com,监听2222端口(默认是2181),并确保conf/hbase-env.sh文件中HBASE_MANAGE_ZK的值是true,再编辑conf/hbase-site.xml,设置hbase.zookeeper.property.clientPort和hbase.zookeeper.quorum。还可以通过设置hbase.zookeeper.property.dataDir的属性来更改ZooKeeper保存数据的目录地址。默认值是/tmp,这里在重启时会被操作系统删掉,可以把它修改到/user/local/zookeeper。
<configuration>
...
#监听2222端口(默认是2181)
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2222</value>
<description>Property from ZooKeeper's config zoo.cfg.
The port at which the clients will connect.
</description>
</property>
#设置hbase.zookeeper.quorum
<property>
<name>hbase.zookeeper.quorum</name>
<value>rs1.example.com,rs2.example.com,rs3.example.com,rs4.example.com,rs5.
example.com</value>
<description>Comma separated list of servers in the ZooKeeper Quorum.
For example, "host1.mydomain.com,host2.mydomain.com,host3.mydomain.com".
By default this is set to localhost for local and pseudo-distributed modes
of operation. For a fully-distributed setup, this should be set to a full
list of ZooKeeper quorum servers. If HBASE_MANAGES_ZK is set in hbase-env.sh
this is the list of servers which we will start/stop ZooKeeper on.
</description>
</property>
#设置hbase.zookeeper.property.clientPort
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/usr/local/zookeeper</value>
<description>Property from ZooKeeper's config zoo.cfg.
The directory where the snapshot is stored.
</description>
</property>
...
</configuration>
学员评价