Zookeeper集群及配置
原理
ZooKeeper是以Fast Paxos算法为基础的,Paxos 算法存在活锁的问题,即当有多个proposer交错提交时,有可能互相排斥导致没有一个proposer能提交成功,而Fast Paxos作了一些优化,通过选举产生一个leader (领导者),只有leader才能提交proposer,具体算法可见Fast Paxos。因此,要想弄懂ZooKeeper首先得对Fast Paxos有所了解。
ZooKeeper的基本运转流程:
1、选举Leader。
2、同步数据。
3、选举Leader过程中算法有很多,但要达到的选举标准是一致的。
4、Leader要具有最高的执行ID,类似root权限。
5、集群中大多数的机器得到响应并follow选出的Leader。
本文将zookeeper在同一台服务器上做一个伪集群部署(实际生产环境不会这样部署),zookeeper有个特性就是节点一般是部署单数(3,5,7,9。。。)具体访问量来权衡,节点down掉一半才算zookeeper服务down掉。
1、下载地址
http://apache.fayea.com/zookeeper/
2、解压安装
1、使用cd命令进入/usr/local/application/zookeeper目录,并上传下载好的zookeeper-3.4.6.tar.gz至安装目录,分别解压移动至zookeeper2181、zookeeper2182、zookeeper2183。
tar -zxvf zookeeper-3.4.6.tar.gz mv zookeeper-3.4.6 zookeeper2181 tar -zxvf zookeeper-3.4.6.tar.gz mv zookeeper-3.4.6 zookeeper2182 tar -zxvf zookeeper-3.4.6.tar.gz mv zookeeper-3.4.6 zookeeper2183 rm -rf zookeeper-3.4.6.tar.gz
3、修改配置
1、修改zookeeper2181/conf/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.
dataDir=/usr/local/application/zookeeper/zookeeper2181/data
dataLogDir=/usr/local/application/zookeeper/zookeeper2181/logs
# 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 server.1=192.168.1.11:4000:5000 server.2=192.168.1.11:4000:5000 server.3=192.168.1.11:4000:5000
2、修改zookeeper2182/conf/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. dataDir=/usr/local/application/zookeeper/zookeeper2182/data
dataLogDir=/usr/local/application/zookeeper/zookeeper2182/logs # the port at which the clients will connect clientPort=2182 # 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 server.1=192.168.1.11:4000:5000 server.2=192.168.1.11:4000:5000 server.3=192.168.1.11:4000:5000
3、修改zookeeper2183/conf/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.
dataDir=/usr/local/application/zookeeper/zookeeper2181/data
dataLogDir=/usr/local/application/zookeeper/zookeeper2181/logs
# the port at which the clients will connect clientPort=2183 # 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 server.1=192.168.1.11:4000:5000 server.2=192.168.1.11:4000:5000 server.3=192.168.1.11:4000:5000
4、修改zookeeper2181配置,依次执行下面命令
cd zookeeper2181
mkdir data logs
cd data
vi myid(首先输入字母i进入编辑模式,修改myid内容1,这个1与zoo.cfg文件里的server.1对应(具体服务器是多少就输入多少))
5、修改zookeeper2182配置,依次执行下面命令
cd zookeeper2182
mkdir data logs
cd data
vi myid(首先输入字母i进入编辑模式,修改myid内容2,这个2与zoo.cfg文件里的server.2对应(具体服务器是多少就输入多少))
6、修改zookeeper2183配置,依次执行下面命令
cd zookeeper2183
mkdir data logs
cd data
vi myid(首先输入字母i进入编辑模式,修改myid内容3,这个3与zoo.cfg文件里的server.3对应(具体服务器是多少就输入多少))
4、启动服务
1、启动zookeeper2181服务,进入bin目录,输入命令:./zkServer.sh start
[root@localhost zookeeper2181]# cd bin [root@localhost bin]# ./zkServer.sh start JMX enabled by default Using config: /usr/local/application/zookeeper/zookeeper2181/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost bin]#
2、启动zookeeper2182服务,进入bin目录,输入命令:./zkServer.sh start
[root@localhost zookeeper2182]# cd bin
[root@localhost bin]# ./zkServer.sh start
JMX enabled by default
Using config: /usr/local/application/zookeeper/zookeeper2182/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]#
3、启动zookeeper2183服务,进入bin目录,输入命令:./zkServer.sh start
[root@localhost zookeeper2183]# cd bin
[root@localhost bin]# ./zkServer.sh start
JMX enabled by default
Using config: /usr/local/application/zookeeper/zookeeper2183/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]#
4、查看服务,进入bin目录使用命令:./zkServer.sh status,发现自动选举了leader。
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/application/zookeeper/zookeeper2183/bin/../conf/zoo.cfg
Mode: leader
[root@localhost bin]# cd /usr/local/application/zookeeper/zookeeper2181/bin
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/application/zookeeper/zookeeper2181/bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]# cd /usr/local/application/zookeeper/zookeeper2182/bin
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/application/zookeeper/zookeeper2182/bin/../conf/zoo.cfg
Mode: follower
5、常用命令
启动服务:./zkServer.sh start (需要进入bin目录)
停止服务:./zkServer.sh stop (需要进入bin目录)
重启服务:./zkServer.sh restart (需要进入bin目录)
查看状态:./zkServer.sh status (需要进入bin目录)