下载地址
https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
安装环境
centos7 vmware 共3台机器(zk集群最少3台)
ip | host |
---|---|
192.168.58.129 | hsot1 |
192.168.58.130 | host2 |
192.168.58.131 | host3 |
分别修改3台机器的host
vim /etc/hosts
新增
192.168.58.129 host1
192.168.58.130 host2
192.168.58.131 host3
安装
先在129这台机器上操作
解压
tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz
重命名
mv apache-zookeeper-3.7.0-bin zk3.7
复制一份默认的配置文件
cd zk3.7/conf/
cp zoo_sample.cfg zoo.cfg
修改配置
vim 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=/tmp/zookeeper
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
- 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会监听这个端口接受客户端的访问请求。
- autopurge.snapRetainCount需要保留的文件数目,默认保留3个
- autopurge.purgeInterval 清理频率,单位是小时,需要填写一个1或者更大的数据,默认0表示不开启自动清理功能
修改配置
我们在保持默认文件不变,新增配置server.A=B:C:D
- server.A=B:C:D
A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址,C第一个端口用来集群成员的信息交换,表示这个服务器与集群中的leader服务器交换信息的端口,D是在leader挂掉时专门用来进行选举leader所用的端口
zookeeper服务默认的端口号为2888和3888
新增
server.1=host1:2888:3888
server.2=host2:2888:3888
server.3=host3:2888:3888
修改data存放目录
dataDir=/install/zk3.7/data
新增log存在目录配置
dataLogDir=/install/zk3.7/logs
创建zk ServerID标识
修改zoo.cfg配置文件外,zookeeper集群模式下还要配置一个myid文件,这个文件需要放在dataDir目录下
echo "1" > /install/zk3.7/data/myid
复制配置除了zk serverID标识
130机器
echo "2" > /opt/zookeeper/data/myid
131机器
echo "3" > /opt/zookeeper/data/myid
3台机器启动
zookeeper/bin/zkServer.sh start