天天看点

搭建 zookeeper 集群 (真实集群+伪集群)一、下载 zookeeper二、配置 zoo.cfg三、创建 myid 文件四、启动zookeeper集群tips: zookeeper节点为什么是奇数?

一、下载 zookeeper

官网下载地址: http://mirror.bit.edu.cn/apache/zookeeper/.

二、配置 zoo.cfg

解压zookeeper,进入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.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# cluster config
server.1=192.168.120.1:2888:3888
server.2=192.168.120.2:2888:3888
server.3=192.168.120.3:2888:3888
           
上面的 dataDir、server 是我们需要修改的内容:

  1、tickTime:基本事件单元,这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳;最小 的session过期时间为2倍tickTime

  2、dataDir:存储内存中数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。注意:应该谨慎的选择日志存放的位置,使用专用的日志存储设备能够大大提高系统的性能,如果将日志存储在比较繁忙的存储设备上,那么将会很大程度上影像系统性能。

  3、client:监听客户端连接的端口。

  4、initLimit:允许follower连接并同步到Leader的初始化连接时间,以tickTime为单位。当初始化连接时间超过该值,则表示连接失败。

  5、syncLimit:表示Leader与Follower之间发送消息时,请求和应答时间长度。如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。

  6、server.A=B:C:D

    A:其中 A 是一个数字,表示这个是服务器的编号;

    B:是这个服务器的 ip 地址;

    C:Leader选举的端口;

    D:Zookeeper服务器之间的通信端口

  我们需要修改的第一个是 dataDir ,在指定的位置处创建好目录。

  第二个需要新增的是 server.A=B:C:D 配置,其中 A 对应下面我们即将介绍的myid 文件。B是集群的各个IP地址,C:D 是端口配置。
           

如果你是在一台电脑上,搭建伪集群则需要创建三个zookeeper,并分别配置 zoo.cfg

# zookeeper1 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.
dataDir=/tmp/zookeeper1
# the port at which the clients will connect
clientPort=2181
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
           
# zookeeper2 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.
dataDir=/tmp/zookeeper2
# the port at which the clients will connect
clientPort=2182
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
           
# zookeeper3 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.
dataDir=/tmp/zookeeper3
# the port at which the clients will connect
clientPort=2183
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
           

三、创建 myid 文件

在 上一步 dataDir 指定的目录下,创建 myid 文件。

然后在该文件添加上一步 server 配置的对应 A 数字。

比如我们上面的配置:

dataDir=/usr/local/software/zookeeper-3.3.6/data

然后下面配置是:

server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
           

那么就必须在第一台机器的的 /tmp/zookeeper 目录下创建 myid 文件,然后在该文件中写上 1 即可。

后面的机器依次在相应目录创建myid文件,写上相应配置数字即可。

四、启动zookeeper集群

zkServer.sh start
           
Windows环境下,分别打开zookeeper1,zookeeper2,zookeeper3目录,找到bin目录下的 zkServer.cmd,双击即可开启,不可关闭
           

zookeeper集群就已经搭建好了

tips: zookeeper节点为什么是奇数?

1.zookeeper集群的写操作,由leader节点负责,它会把通知所有节进行写入操作,只有收到半数以上节点的成功反馈,才算成功。如果是部署2个节点的话,那就必须都成功。

2.zookeeper的选举策略也是需要半数以上的节点同意才能当选leader,如果是偶数节点可能导致票数相同的情况

3.只有当半数以上的节点存活时 zookeeper集群才能对外服务,维持正常状态,如果是2个节点,只要其中一个挂掉,那么剩下的1个并不满足半数以上规则

Leader选举算法采用了Paxos协议;Paxos核心思想:当多数Server写成功,则任务数据写 成功。也就是说: 如果有3个Server,则两个写成功即可; 如果有4或5个Server,则三个写成功即可。 所以一般为奇数

选举是(myid,zxid)来判断的,两个机器可以形成集群,但是并没有高可用性,一个挂掉后单独一个机器就不能选出master了

以上内容参考: YSOcean-zookeeper 集群搭建、 伪集群环境、 部署奇数节点的原因.

继续阅读