软件环境:
Linux服务器一台、三台、五台(2*n+1台);
Java jdk 1.8;
zookeeper 3.4.6版;
https://archive.apache.org/dist/zookeeper/
复制配置文件
cp zoo_sample.cfg zoo.cfg
配置zoo.cfg文件
# 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=/opt/zookeeper/zkdata
dataLogDir=/opt/zookeeper/zkdatalog
# the port at which the clients will connect
clientPort=2181
# 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
#2888 心跳端口, 3888 选举端口
server.1=192.168.13.128:2888:3888
server.2=192.168.13.130:2888:3888
server.3=192.168.13.131:2888:3888
在文件/opt/zookeeper/zkdata目录下生成myid文件
myid文件是标识本机zookeeper站点的在集群中的位置id
可以使用
递归创建目录
mkdir -p /opt/zookeeper/zkdata
echo "1" > myid
Zookeeper的启动停止状态查看
root@ubuntu-128:/opt/zookeeper/zookeeper-3.4.8/bin# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
root@ubuntu-128:/opt/zookeeper/zookeeper-3.4.8/bin# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
root@ubuntu-128:/opt/zookeeper/zookeeper-3.4.8/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
Mode: follower
root@ubuntu-128:/opt/zookeeper/zookeeper-3.4.8/bin#
Zookeeper的主进程查看
root@ubuntu-128:/opt/zookeeper/zookeeper-3.4.8/bin# jps
4229 Jps
4174 QuorumPeerMain
Zookeeper集群搭建 — 搭建成功标志
在任一台机器运行sh zkServer.sh status出现下面两幅图中任一输出结果,说明你的集群成功搭建。
root@ubutu-130:/opt/zookeeper/zookeeper-3.4.8/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
Mode: leader
root@ubutu-131:/opt/zookeeper/zookeeper-3.4.8/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
Mode: follower
Zookeeper集群搭建— 集群配置参数讲解
•myid文件和server.myid;
•zoo.cfg文件;
•log4j. properties文件;
•zkEnv.sh和zkServer.sh文件
# 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=/opt/zookeeper/zkdata
dataLogDir=/opt/zookeeper/zkdatalog
# the port at which the clients will connect
clientPort=2181
# 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=192.168.13.128:2888:3888
server.2=192.168.13.130:2888:3888
server.3=192.168.13.131:2888:3888
其中2888 是zk集群通信传输使用的,3888是选举使用的,大家互相查找 ,相互监督,互为节点
这里2888 3888不共用一个端口是因为 我希望节点数据通信的时候,选举也可以正常实现,所以是两个端口,不然当你使用2888数据传输的时候,可能会有数据阻塞,导致选举不通畅!!!
b.预装的zoo.cfg下面默认有五个属性,他们分别是:1.tickTime,2.initLimit,3.syncLimit,4.dataDir,5.clientPort
下面我们来对这五个属性做详细的分析
(1)tickTime
——CS通信心跳时间
tick翻译成中文的话就是滴答滴答的意思,连起来就是滴答滴答的时间,寓意心跳间隔,单位是毫秒,系统默认是2000毫秒,也就是间隔两秒心跳一次。
tickTime的意义:客户端与服务器或者服务器与服务器之间维持心跳,也就是每个tickTime时间就会发送一次心跳。通过心跳不仅能够用来监听机器的工作状态,还可以通过心跳来控制Flower跟Leader的通信时间,默认情况下FL的会话时常是心跳间隔的两倍。
# The number of ticks that the initial synchronization phase can take
(2)initLimit
集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
(3)syncLimit
集群中flower服务器(F)跟leader(L)服务器之间的请求和答应最多能容忍的心跳数。
(4)dataDir
该属性对应的目录是用来存放myid信息跟一些版本,日志,跟服务器唯一的ID信息等。
来看注释
# the directory where the snapshot is stored. do not use /tmp for storage, /tmp here is just example sakes.
不要使用/tmp来做存储快照的目录,/tmp这只是一个案例。在集群zookeeper服务在启动的时候回去读取zoo.cfg这个文件,从这个文件中找到这个属性然后获取它的值也就是dataDir 的路径,它会从这个路径下面读取mydi这个文件,从这个文件中获取要启动的当前服务器的地址,当它找不到这个地址的时候就会抛出异常,我们可以去查看状态
[hadoop@hadoop06 data]$ zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
<span style="color:#ff6600;">Error contacting service. It is probably not running.</span>
这是一个很常见的错误,要想解决这个bug,去查看日志就会有很精准的提示:
java.lang.RuntimeException: My id 5 not in the peer list
所以在创建myid的时候给的值是多少,那么在zoo.cfg里面配置集群信息的时候要准确指出!
c.集群信息的配置
在zoo.cfg这个文件中,配置集群信息是存在一定的格式:service.N =YYY: A:B
N:代表服务器编号(也就是myid里面的值)
YYY:服务器地址
A:表示 Flower 跟 Leader的通信端口,简称服务端内部通信的端口(默认2888)
B:表示 是选举端口(默认是3888)
server.1=hadoop05:2888:3888
server.2=hadoop06:2888:3888
server.3=hadoop07:2888:3888
(5)clientPort
客户端连接的接口,客户端连接zookeeper服务器的端口,zookeeper会监听这个端口,接收客户端的请求访问!这个端口默认是2181。
启动服务器,可以使用下面 的命令来来查看端口监听情况