一、集群搭建要求

本实例搭建一个3台服务器组成的zookeeper集群,准备两台Ubuntu12.04,一台CentOS 6.4,jre要求Java 1.6+
 
二、安装zookeeper
1、下载 zookeeper(最好使用stable版本,最新稳定版 zookeeper-3.4.5.tar.gz
官方安装文档: deployment
 
2、解压
tar zxvf zookeeper-3.4.5.tar.gz
将解压后的zookeeper-3.4.5文件放在系统的/opt目录下中,即/opt/zookeeper-3.4.5
 
3、进入zookeeper-3.4.5/conf目录下,拷贝zoo_sample.cfg重命名为“zoo.cfg”: cp zoo_sample.cfg  zoo.cfg
 
4、分别修改三台机器的zoo.cfg配置文件,保证zoo.cfg的server.xxx内容一致:
# 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=/tmp/zookeeper
dataDir=/opt/zookeeper-3.4.5/zookeeperdir/zookeeper-data
dataLogDir=/opt/zookeeper-3.4.5/zookeeperdir/logs

# the port at which the clients will connect
clientPort=2181
#
# 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=172.27.22.21:2888:3888
server.2=172.27.22.98:2888:3888
server.3=172.27.22.99:2888:3888
其中,2888端口号是zookeeper服务之间通信的端口,3888端口号是zookeeper与其他应用程序通信的端口。
initLimit:这个配置项是用来配置 Zookeeper 接受 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器,初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败,总的时间长度就是 10*2000=20 秒。
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址或hostname;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
 
5、创建dataDir参数指定的目录(这里指的是“ /opt/zookeeper-3.4.5/zookeeperdir/zookeeper-data”),并在目录下创建文件,命名为“myid”。
 
6、编辑“myid”文件,并在对应的IP的机器上输入分别对应的编号。如在zookeeper上,“myid”文件内容就是1,实例配置如下:
  • 登录 172.27.22.21, vim vim zookeeperdir/zookeeper-data/myid,输入1,保存(server.1=172.27.22.21:2888:3888的server.1的id为1,下同)
  • 登录 172.27.22.98, vim vim zookeeperdir/zookeeper-data/myid,输入2
  • 登录 172.27.22.99, vim vim zookeeperdir/zookeeper-data/myid,输入3
至此,在三台集群服务器上配置完毕。
 
7、三台机器上,分别在/etc/profile文件中设置PATH变量环境
修改profile文件:sudo vi /etc/profile
export ZOOKEEPER_HOME= /opt/zookeeper-3.4.5
export PATH=$ZOOKEEPER_HOME/bin:$PATH
保存,执行 sudo source /etc/profile 使其生效
 
8、安装完毕
 
三、启动并测试zookeeper
1、在所有服务器中执行:zookeeper-3.4.5/bin/zkServer.sh start
2、配置好java后,需要重启source /etc/profile使java/bin下的jps生效,查看进程命令:jps
namenode上显示为:
root@ubuntu:/opt/zookeeper-3.4.5# jps
18232 Jps
1871 Bootstrap
17760 core
26848
QuorumPeerMain
17659 nimbus
31622 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
其中,QuorumPeerMain是zookeeper进程,启动正常。
 
3、查看状态:
zookeeper-3.4.5/bin/zkServer.sh status
JMX enabled by default
Using config: /opt/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode:
follower
 
上面是172.27.22.21的状态,172.27.22.98状态如下:
JMX enabled by default
Using config: /opt/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode:
leader
可见172.27.22.98的Mode为leader,其它172.27.22.21和172.27.22.99两台机器Mode为follower
 
4、启动客户端脚本:/opt/zookeeper-3.4.5/bin/zkCli.sh -server 172.27.22.21:2181
WatchedEvent state:SyncConnected type:None path:null
[zk: zookeeper:2181(CONNECTED) 0]
[zk: zookeeper:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
        connect host:port
        get path [watch]
        ls path [watch]
        set path data [version]
        rmr path
        delquota [-n|-b] path
        quit 
        printwatches on|off
        create [-s] [-e] path data acl
        stat path [watch]
        close 
        ls2 path [watch]
        history 
        listquota path
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
[zk: zookeeper:2181(CONNECTED) 1] ls /
[hbase, zookeeper]
 
四、 执行测试
4.1)在一台机器如server.2上进行一个写操作:
bin/zkCli.sh -server 172.27.22.98:2181
[zk: 127.0.0.1:2181(CONNECTED) 1] create /mytest test
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[mytest, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 5] get /mytest
test
cZxid = 0x100000002
ctime = Mon Jul 19 03:30:20 EDT 2010
mZxid = 0x100000002
mtime = Mon Jul 19 03:30:20 EDT 2010
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

4.2 在其他机器172.27.22.21上查询:
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[mytest, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /mytest
[]
[zk: 127.0.0.1:2181(CONNECTED) 3] get /mytest
test
cZxid = 0x100000002
ctime = Mon Jul 19 03:30:20 EDT 2010
mZxid = 0x100000002
mtime = Mon Jul 19 03:30:20 EDT 2010
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

$bin/zkServer.sh status
JMX enabled by default
Using config: /local/software/zookeeper/zookeeper-3.3.1/bin/../conf/zoo.cfg
2010-07-19 04:09:49,862 - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@250] - Accepted socket connection from /127.0.0.1:42234
2010-07-19 04:09:49,864 - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1236] - Processing stat command from /127.0.0.1:42234
2010-07-19 04:09:49,866 - INFO  [Thread-20:NIOServerCnxn$StatCommand@1152] - Stat command output
2010-07-19 04:09:49,866 - INFO  [Thread-20:NIOServerCnxn@1434] - Closed socket connection for client /127.0.0.1:42234 (no session established for client)
Mode: follower

五、停止zookeeper进程
/opt/zookeeper-3.4.5/bin/zkServer.sh stop
 
参考: