centos7集群安装zookeeper-3.5.7
原创 java_world 发表于:2020-02-25 09:21:38
  阅读 :559   收藏   编辑

概念

zookeeper共有3个角色和4个状态

  • 角色:leader,follower,observer

  • 状态:leading,following,observing,looking

looking:当前Server不知道leader是谁,正在搜寻。

leading:当前Server即为选举出来的leader。

following:leader已经选举出来,当前Server与之同步。

observing:observer的行为在大多数情况下与follower完全一致,但是他们不参加选举和投票,而仅仅接受(observing)选举和投票的结果。

zk核心为原子广播,使用Zab协议保证了各个Server之间的同步,Zab协议有两种模式,它们分别是恢复模式(Recovery选主)和广播模式(Broadcast同步)。当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,且大多数Server完成了和leader的状态同步以后,恢复模式就结束了。状态同步保证了leader和Server具有相同的系统状态,为了保证事务的顺序一致性,zookeeper采用了递增的事务id号(zxid)来标识事务

集群部署

zk集群需要最少三台机器,zk节点部署越多,服务的可靠性越高,因为zk集群是以宕机个数过半才会让整个集群宕机建议部署奇数个节点

安装虚拟机,启动3台centos7

IP
host
192.168.171.130zk01
192.168.171.131zk02
192.168.171.132zk03

3台机器配置host

192.168.171.130 zk01
192.168.171.131 zk02
192.168.171.132 zk03

注:zk依赖于jdk、关闭防火墙、关闭seLinux

安装配置

wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz
mv apache-zookeeper-3.5.6-bin zookeeper
cd zookeeper/conf
cp zoo_sample.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.
dataLogDir=/opt/zookeeper/logs
dataDir=/opt/zookeeper/data
# 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=500
# Purge task interval in hours
# Set to "0" to disable auto purge feature
autopurge.purgeInterval=24
admin.serverPort=2821
server.1=zk01:2888:3888
server.2=zk02:2888:3888
server.3=zk03:2888:3888
  • tickTime这个时间是作为zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是说每个tickTime时间就会发送一个心跳。

  • initLimit这个配置项是用来配置zookeeper接受客户端(这里所说的客户端不是用户连接zookeeper服务器的客户端,而是zookeeper服务器集群中连接到leader的follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。

当已经超过10个心跳的时间(也就是tickTime)长度后 zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20秒。

  • syncLimit这个配置项标识leader与follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒。

  • dataDir顾名思义就是zookeeper保存数据的目录,默认情况下zookeeper将写数据的日志文件也保存在这个目录里;

  • clientPort这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口接受客户端的访问请求;

  • server.A=B:C:D

A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址,C第一个端口用来集群成员的信息交换,表示这个服务器与集群中的leader服务器交换信息的端口,D是在leader挂掉时专门用来进行选举leader所用的端口

zookeeper服务默认的端口号为2888和3888

创建zk ServerID标识

修改zoo.cfg配置文件外,zookeeper集群模式下还要配置一个myid文件,这个文件需要放在dataDir目录下

  • ip为130机器下

echo "1" > /opt/zookeeper/data/myid
  • ip为131机器下

echo "2" > /opt/zookeeper/data/myid
  • ip为132机器下

echo "3" > /opt/zookeeper/data/myid

启动

zookeeper/bin/zkServer.sh start

1



2

zkCli测试

zkCli.sh -server zk01:2181

进入后

create /test
ls /

在zk02,zk03也可看到创建test

常用shell命令

# 查看所有支持的命令
help

# 查看目录下有哪些节点。以根目录为例
ls /

# 创建一个节点。
# 加-s表示创建顺序节点,自动在给定的路径后面再加上一个数字串,保证路径不重复。
# 加-e是临时节点 默认持久节点
create /example_path "example_data"

# 查看节点内容
get /example_path

#删除没有子节点的节点
delete /example_path

# 递规删除节点及其所有子节点,相当于 rm -rf 
rmr /example_path

# 退出zkCli
quit