前提准备3台centos7.0虚拟机
c7003:192.168.70.103
c7004:192.168.70.104
c7005:192.168.70.105
并在三台虚拟机上配置hosts为
192.168.70.103 c7003
192.168.70.104 c7004
192.168.70.105 c7005
架构图如下:
注意:follower负责响应读请求,leader负责提交写请求
1、下载zookeeper并解压
登录到c7003并进入/opt目录下执行如下命令
[vagrant@c7003 opt]$ wget http://apache.fayea.com/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
[vagrant@c7003 opt]$ tar -zxvf zookeeper-3.4.10.tar.gz
[vagrant@c7003 opt]$ chmod +wxr zookeeper-3.4.10
2、修改zookeeper的配置文件,并建立数据目录和日志目录
[vagrant@c7003 opt]$ cd zookeeper-3.4.10
[vagrant@c7003 zookeeper-3.4.10]$ mkdir data
[vagrant@c7003 zookeeper-3.4.10]$ mkdir logs
[vagrant@c7003 zookeeper-3.4.10]$ vi conf/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-3.4.10/data
dataLogDir=/opt/zookeeper-3.4.10/logs
server.1=c7003:2888:3888
server.2=c7004:2888:3888
server.3=c7005:2888:3888
# 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
[vagrant@c7003 zookeeper-3.4.10]$ cd data
[vagrant@c7003 data]$ vi myid
1
3、复制c7003的zookeeper-3.4.10到c7004和c7005上
[vagrant@c7003 opt]$ scp zookeeper-3.4.10 vagrant@c7004:/opt/zookeeper-3.4.10
[vagrant@c7003 opt]$ scp zookeeper-3.4.10 vagrant@c7005:/opt/zookeeper-3.4.10
4、分别修改c7004和c7005上myid的值为2和3
[vagrant@c7004 zookeeper-3.4.10]$ vi data/myid
2
[vagrant@c7005 zookeeper-3.4.10]$ vi data/myid
3
5、分别启动c7003、c7004、c7005上的zookeeper
[vagrant@c7003 zookeeper-3.4.10]$ bin/zkServer.sh start
[vagrant@c7004 zookeeper-3.4.10]$ bin/zkServer.sh start
[vagrant@c7005 zookeeper-3.4.10]$ bin/zkServer.sh start
6、查看zookeeper的状态
[vagrant@c7003 zookeeper-3.4.10]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[vagrant@c7004 zookeeper-3.4.10]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[vagrant@c7005 zookeeper-3.4.10]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
7、验证zookeeper集群
[vagrant@c7003 zookeeper-3.4.10]$ bin/zkCli.sh -server c7003:2181
Connecting to c7003:2181
2017-04-02 03:06:12,251 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-04-02 03:06:12,257 [myid:] - INFO [main:Environment@100] - Client environment:host.name=c7003.ambari.apache.org
2017-04-02 03:06:12,257 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_121
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/opt/jdk1.8.0_121/jre
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.10/bin/../build/classes:/opt/zookeeper-3.4.10/bin/../build/lib/*.jar:/opt/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/opt/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.10/bin/../conf:
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-04-02 03:06:12,260 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-04-02 03:06:12,261 [myid:] - INFO [main:Environment@100] - Client environment:os.version=4.1.12-32.el7uek.x86_64
2017-04-02 03:06:12,261 [myid:] - INFO [main:Environment@100] - Client environment:user.name=vagrant
2017-04-02 03:06:12,261 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/vagrant
2017-04-02 03:06:12,261 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/zookeeper-3.4.10
2017-04-02 03:06:12,262 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=c7003:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e
Welcome to ZooKeeper!
ls /
[zookeeper, zk_test]
至此:zookeeper集群安装完毕!
PS:
1、由于zk运行一段时间后,会产生大量的日志文件,把磁盘空间占满,导致整个机器进程都不能活动了,所以需要定期清理这些日志文件,方法如下:
1)、写一个脚本文件cleanup.sh内容如下:
java -cp zookeeper.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-log4j12-1.6.1.jar:lib/log4j-1.2.15.jar:conf org.apache.zookeeper.server.PurgeTxnLog <dataDir> <snapDir> -n <count>
其中:
dataDir:即上面配置的dataDir的目录
snapDir:即上面配置的dataLogDir的目录
count:保留前几个日志文件,默认为3
2)、通过crontab写定时任务,来完成定时清理日志的需求
crontab -e 0 0 * * /opt/zookeeper-3.4.10/bin/cleanup.sh
3)、在其它2台机器做同样操作