192.168.8.111、192.168.8.112、192.168.8.113
1、下载zookeeper-3.4.5.tar.gz复制到/usr/local/ , 3台机器相同操作
2、解压 tar -zxvf zookeeper-3.4.5.tar.gz , 3台机器相同操作
3、cd /usr/local/zookeeper-3.4.5/conf
复制配置文件重新命名:cp zoo_sample.cfg zoo.cfg , 3台机器相同操作
4、创建数据、日志文件夹
, 3台机器相同操作
/usr/local/zookeeper-3.4.5/data
/usr/local/zookeeper-3.4.5/logs
5、修改配置文件
3台机器配置文件相同
# 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=/usr/local/zookeeper-3.4.5/data
dataLogDir=/usr/local/zookeeper-3.4.5/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=192.168.8.111:2888:3888
server.2=192.168.8.112:2888:3888
server.3=192.168.8.113:2888:3888
server.1标识本台机器,通过myid文件指定id=1;ip:机器之间通信端口(默认为2888):leader选举端口(默认为3888)
其中,2888端口号是zookeeper服务之间通信的端口,而3888是zookeeper与其他应用程序通信的端口。而zookeeper是在hosts中已映射了本机的ip。
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒。
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
6、创建myid
每台机器myid中的值是不同的。
第1台机器
cd /usr/local/zookeeper-3.4.5/data
创建文件:touch myid
然后向文件中写入值'1'或者
cd /usr/local/zookeeper-3.4.5/data
echo '1' > myid(创建文件同时写入值)
第2台机器 中值 为2 , 第3台机器 中值 为3 ,
7、在/etc/profile文件中设置PATH
修改profile文件:
vim /etc/profile
export ZOOKEEPER_HOME=/root/zookeeper-3.4.5
PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
8、启动服务
启动并测试zookeeper
在所有服务器中执行:zookeeper-3.4.5/bin/zkServer.sh start
停止zookeeper进程:zookeeper-3.4.5/bin/zkServer.sh stop
查看状态:
bin/zkServer.sh status