zookeeper 安装 配置集群

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

[root@znode01 src]# tar -xzvf zookeeper-3.5.2-alpha.tar.gz 
[root@znode01 src]# ls
 zookeeper-3.5.2-alpha  zookeeper-3.5.2-alpha.tar.gz
[root@znode01 zookeeper-3.5.2-alpha]# ls
bin          ivysettings.xml       recipes
build.xml    ivy.xml               src
CHANGES.txt  lib                   zookeeper-3.5.2-alpha.jar
conf         LICENSE.txt           zookeeper-3.5.2-alpha.jar.asc
contrib      NOTICE.txt            zookeeper-3.5.2-alpha.jar.md5
dist-maven   README_packaging.txt  zookeeper-3.5.2-alpha.jar.sha1
docs         README.txt
[root@znode01 zookeeper-3.5.2-alpha]# cd conf/
[root@znode01 conf]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@znode01 conf]# mv zoo_sample.cfg zoo.cfg
[root@znode01 conf]# ls
configuration.xsl  log4j.properties  zoo.cfg

修改配置文件:

[root@znode01 conf]# cat zoo.cfg 
# The number of milliseconds of each tick
tickTime=2000 #Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳。tickTime以毫秒为单位。
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10 #集群中的follower服务器与leader之间初始连接时能容忍的最多心跳数(ticTime的数量)
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5 #集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多必跳数(tickTime的数量)
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes. 
dataDir=/tmp/zookeeper #Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也何存在这个目录里。
# the port at which the clients will connect
clientPort=2181 #客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口,接受客户端的访问请求。
# 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
#服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
#server.N=YYY:A:B
server.1=192.168.1.104:2888:3888
server.2=192.168.1.103:2888:3888
server.3=192.168.1.108:2888:3888

zk设置为奇数:

zookeeper有这样一个特性:集群中只要有过半的机器是正常工作的,那么整个集群对外就是可用的。也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1没有过半,所以2个zookeeper的死亡容忍度为0;同理,要是有3个zookeeper,一个死了,还剩下2个正常的,过半了,所以3个zookeeper的容忍度为1;同理你多列举几个:2-->0;3-->1;4-->1;5-->2;6-->2会发现一个规律,2n和2n-1的容忍度是一样的,都是n-1,所以为了更加高效,何必增加那个不必要的zookeeper!!!!!

[root@znode01 conf]# mkdir /tmp/zookeeper
[root@znode01 conf]# touch /tmp/zookeeper/myid;echo 1> /tmp/zookeeper/myid
#创建一个myid文件,里面的内容是server.N中的N(server.2里面的内容为2)

将配好的zookeeper发到别外的两个节点上:

[root@znode01 local]# scp -r -P22022 zookeeper-3.5.2-alpha 192.168.1.108:/usr/local/
[root@znode01 local]# scp -r -P22022 zookeeper-3.5.2-alpha 192.168.1.103:/usr/local/

注意其它的节点的myid内容分别人2,3。查看个个结点的myid

[root@znode01 zookeeper-3.5.2-alpha]# cat /tmp/zookeeper/myid 
1
[root@znode02 zookeeper-3.5.2-alpha]# cat /tmp/zookeeper/myid 
2
[root@znode03 zookeeper-3.5.2-alpha]# cat /tmp/zookeeper/myid 
3

启动zookeeper:

[root@znode01 bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@znode02 zookeeper-3.5.2-alpha]# ./bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@znode03 zookeeper-3.5.2-alpha]# ./bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

之前的zoo.cnf做一下小的修改如下:

[root@znode01 zookeeper-3.5.2-alpha]# cat conf/zoo.cfg
# The number of milliseconds of each tick
#Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳。tickTime以毫秒为单位。
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
#集群中的follower服务器与leader之间初始连接时能容忍的最多心跳数(ticTime的数量)
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
#集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多必跳数(tickTime的数量)
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes. 
#Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也何存在这个目录里。
dataDir=/tmp/zookeeper
# the port at which the clients will connect
#客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口,接受客户端的访问请求。
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
#服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
#server.N=YYY:A:B
server.1=192.168.1.104:2888:3888
server.2=192.168.1.103:2888:3888
server.3=192.168.1.108:2888:3888

下面看一下三个结点的过行情况:

[root@znode01 zookeeper-3.5.2-alpha]# ./bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@znode02 zookeeper-3.5.2-alpha]# ./bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
[root@znode03 zookeeper-3.5.2-alpha]# ./bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

这里要注意防火墙是不是把相应的端口打开!!!!

原文地址:https://www.cnblogs.com/bass6/p/6352110.html