操作系统:CentOS Linux release 7.3.1611 (Core)
Zookeeper:zookeeper-3.4.12
IP:10.55.2.81-83
Zookeeper下载地址:http://zookeeper.apache.org/releases.html#download
部署: 1、修改配置
[root@zk-2-82-0001 soft]# tar -xvf zookeeper-3.4.12.tar.gz
[root@zk-2-82-0001 soft]# mv zookeeper-3.4.12 zookeeper
[root@zk-2-82-0001 soft]# mv zookeeper /app
[root@zk-2-82-0001 soft]# cd /app/zookeeper
[root@zk-2-82-0001 zookeeper]# mkdir data
[root@zk-2-82-0001 zookeeper]# mv conf/zoo_sample.cfg conf/zoo.cfg
[root@zk-2-82-0001 zookeeper]# vim conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000 //Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 //配置 Zookeeper 接受客户端(此客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已超过initLimit个tickTime长度后 Zookeeper 服务器还没有收到客户端的返回信息,则表明客户端连接失败。总的时间长度就是 initLimit * tickTime 秒。
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 //配置 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 syncLimit * tickTime 秒
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/app/zookeeper/data //Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里
dataLogDir=/app/zookeeper/logs //若没提供的话则用dataDir。zookeeper的持久化都存储在这两个目录里。dataLogDir里是放到的顺序日志(WAL),指定的目录下有version-2文件夹(下有log.1文件)。而dataDir里放的是内存数据结构的snapshot,便于快速恢复。为了达到性能最大化,一般建议把dataDir和dataLogDir分到不同的磁盘上,以充分利用磁盘顺序写的特性。
# the port at which the clients will connect
clientPort=2181 //Zookeeper服务器监听的端口,以接受客户端的访问请求。
# 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=10.55.2.81:2888:3888
server.2=10.55.2.82:2888:3888
server.3=10.55.2.83:2888:3888
// server.n n是一个数字,表示这个是第几号服务器;“=”号后面是对应几号服务器的IP地址,2888是这个服务器与集群中的 Leader 服务器交换信息的端口;3888表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,此端口就是用来执行选举时服务器相互通信的端口。
每个节点的zoo.cfg配置都一样,直接复制过去就可以。 2、添加myid文件
除了修改 zoo.cfg 配置文件,集群模式下还要配置一个文件 myid,这个文件在zoo.cfg里dataDir指定的目录下,这个文件里面就只有一个数字,这个数字和server.n的n保持一致,Zookeeper 启动时会读取这个文件,拿到里面的数据与 zoo.cfg 里面的配置信息比较从而判断到底是那个 server。
[root@zk-2-82-0001 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /app/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk-2-82-0001 bin]#
[root@zk-2-81 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /app/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk-2-81 bin]#
[root@zk-2-82-0002 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /app/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk-2-82-0002 bin]#
日志
[root@zk-2-82-0001 bin]# tail -500f zookeeper.out
2018-06-21 15:24:59,618 [myid:] - INFO [main:QuorumPeerConfig@136] - Reading configuration from: /app/zookeeper/bin/../conf/zoo.cfg
2018-06-21 15:24:59,626 [myid:] - INFO [main:QuorumPeer$QuorumServer@184] - Resolved hostname: 10.55.2.81 to address: /10.55.2.81
2018-06-21 15:24:59,626 [myid:] - INFO [main:QuorumPeer$QuorumServer@184] - Resolved hostname: 10.55.2.83 to address: /10.55.2.83
2018-06-21 15:24:59,626 [myid:] - INFO [main:QuorumPeer$QuorumServer@184] - Resolved hostname: 10.55.2.82 to address: /10.55.2.82
2018-06-21 15:24:59,626 [myid:] - INFO [main:QuorumPeerConfig@398] - Defaulting to majority quorums
2018-06-21 15:24:59,628 [myid:2] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-06-21 15:24:59,628 [myid:2] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-06-21 15:24:59,628 [myid:2] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-06-21 15:24:59,635 [myid:2] - INFO [main:QuorumPeerMain@130] - Starting quorum peer
2018-06-21 15:24:59,639 [myid:2] - INFO [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2018-06-21 15:24:59,642 [myid:2] - INFO [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
2018-06-21 15:24:59,646 [myid:2] - INFO [main:QuorumPeer@1158] - tickTime set to 2000
2018-06-21 15:24:59,646 [myid:2] - INFO [main:QuorumPeer@1204] - initLimit set to 10 6、ZooKeeper访问