上一篇介绍了单机版zookeeper安装,这种情况一般用于开发测试。如果是生产环境建议用分布式集群部署,防止单点故障,增加zookeeper服务的高可用。
【环境介绍】
三台机器:192.168.126.160,192.168.126.161,192.168.126.162
操作系统:centos linux
以上就是本次集群的机器情况。下面进行安装和配置:
一、安装和配置
1)解压文件(先在一台机器上操作)
tar -zxvf zookeeper-3.4.6.tar.gz
2)重命名配置文件zoo_sample.cfg并修改其内容如下:
mv zoo_sample.cfg zoo.cfg
vim zoo.cn
内容为:
tickTime=2000
dataDir=/home/mysql/zookeeper-3.4.6/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=192.168.126.160:2888:3888
server.2=192.168.126.161:2888:3888
server.3=192.168.126.162:2888:3888
tickTime:该参数用来定义心跳的间隔时间,zookeeper的客户端和服务端之间也有和web开发里类似的session的概念,而zookeeper里最小的session过期时间就是tickTime的两倍。单位是毫秒
dataDir: 存放内存数据库的快照,用户恢复数据,如果不指定logdatadir,那么默认log也会存在这里。
clientPort:供客户端程序连接的端口,如果在单机部署多台server,那么端口需要不同。
server.x:用于集群中发现彼此,这里的1,2,3需要跟data/myid里数字对应,后面会有说明。2888表示集群中互联的端口,3888用于选主的端口。
initLimit:参数设定了允许所有跟随者与领导者进行连接并同步的时间,如果在设定的时间段内,半数以上的跟随者未能完成同步,领导者便会宣布放弃领导地位,进行另一次的领导选举。如果zk集群环境数量确实很大,同步数据的时间会变长,因此这种情况下可以适当调大该参数。默认为10
syncLimit:参数设定了允许一个跟随者与一个领导者进行同步的时间,如果在设定的时间段内,跟随者未完成同步,它将会被集群丢弃。所有关联到这个跟随者的客户端将连接到另外一个跟随着。
然后,把修改好的zookeeper安装文件传输到其他两台机器上:
scp -r zookeeper-3.4.6 192.168.126.161:/home/
3)设置myid
在我们配置的dataDir指定的目录下面,创建一个myid文件,里面内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.X中X为什么数字,则myid文件中就输入这个数字,例如:
192.168.126.160> cd /zookeeper-3.4.6/data && touch myid && echo "1" >myid
192.168.126.161> cd /zookeeper-3.4.6/data && touch myid && echo "2" >myid
192.168.126.162> cd /zookeeper-3.4.6/data && touch myid && echo "3" >myid
到这里,整个集群就配置好了。
二、启动
192.168.126.160> cd /zookeeper-3.4.6/bin
192.168.126.160 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
192.168.126.161> cd /zookeeper-3.4.6/bin
192.168.126.161 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
192.168.126.162> cd /zookeeper-3.4.6/bin
192.168.126.162 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
安装正确,以上三台都启动了,组成了一个zookeeper集群,即使其中一台down机了,其他两台还可以继续提供服务。
三、命令
1、查看集群机器状态:
192.168.126.160> ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
192.168.126.161> ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
192.168.126.162> ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
126.161机器是leader,其他都是小弟角色。
2、通过客户端脚本连接到集群:
现在整个集群对外是一个整体,连接到任意一台机器上看到的视图都是一样的。
192.168.126.162> ./zkCli.sh -server 192.168.126.160:2181
Connecting to 192.168.126.160:2181
2016-11-25 16:40:47,036 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2016-11-25 16:40:47,038 [myid:] - INFO [main:Environment@100] - Client environment:host.name=darren.org
2016-11-25 16:40:47,038 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_67
2016-11-25 16:40:47,039 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.7.0_67-cloudera/jre
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/mysql/zookeeper-3.4.6/bin/../build/classes:/home/mysql/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/mysql/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/mysql/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/mysql/zookeeper-3.4.6/bin/../conf:.:/usr/java/jdk1.7.0_67-cloudera/lib/dt.jar:/usr/java/jdk1.7.0_67-cloudera/lib/tools.jar:/usr/java/jdk1.7.0_67-cloudera/jre/lib/rt.jar
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-504.23.4.el6.x86_64
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2016-11-25 16:40:47,040 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/mysql/zookeeper-3.4.6/bin
2016-11-25 16:40:47,041 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.126.160:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@71fdf17c
Welcome to ZooKeeper!
2016-11-25 16:40:47,073 [myid:] - INFO [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 192.168.126.160/192.168.126.160:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2016-11-25 16:40:47,076 [myid:] - INFO [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@852] - Socket connection established to 192.168.126.160/192.168
.126.160:2181, initiating session [zk: 192.168.126.160:2181(CONNECTING) 0] 2016-11-25 16:40:47,100 [myid:] - INFO [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 192.168.126.160/192.168.126.160:2181, sessionid = 0x1589a9a25170000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: 192.168.126.160:2181(CONNECTED) 0]
查看数据:
[zk: 192.168.126.160:2181(CONNECTED) 0] ls /
[zookeeper]
用ls /命令查看目录情况,发现只有一个zookeeper目录。
我们到安装目录的/data文件夹下看看都有什么文件?
-rw-r--r-- 1 root root 1 Nov 25 16:29 acceptedEpoch
-rw-r--r-- 1 root root 1 Nov 25 16:29 currentEpoch
-rw-r--r-- 1 root root 67108880 Nov 25 16:40 log.100000001
-rw-r--r-- 1 root root 296 Nov 25 16:29 snapshot.0
【解释】:
snapshot.0:就是我们存放数据在内存中的快照,zookeeper服务刚启动时需要用它来恢复数据
log.100000001:日志文件,默认存放在数据目录中。
到此为止,整个zookeeper集群环境的搭建配置就完成了,挺简单的吧。