logo头像
Snippet 博客主题

Zookeeper集群搭建

1. 准备工作

  1. 安装JDK
  2. Zookeeper安装包下载,这里我选择3.5.6进行下载
  3. 准备三台机器,分别是192.168.5.1,192.168.5.100,192.168.5.101
  4. 将zookeeper的安装包上传到这三台机器相同的目录上(方便管理)

2. 开始搭建

1)配置集群

在三台机器依次进行如下操作:

  1. 解压Zookeeper,并创建data目录,另外将conf下的zoo_sample.cfg文件复制并改名为zoo.cfg

    1
    2
    3
    4
    tar -xzvf apache-zookeeper-3.5.6-bin.tar.gz
    cd apache-zookeeper-3.5.6-bin/
    mkdir data
    cp conf/zoo_sample.cfg conf/zoo.cfg
  2. 在三台机器上配置zoo.cfg集群环境

    1
    vi conf/zoo.cf

    修改如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    # 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.
    # 这里改成data目录,用于存放数据,用相对路径
    dataDir=../data
    # the port at which the clients will connect
    # 默认端口为2181可以不改,如果在一台机器搭建伪集群则需要修改
    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
    # 在每一个 zookeeper的zoo.cfg配置客户端访问端口(clientPort)和集群服务器IP列表。
    # 解释:server.服务器ID=服务器 IP 地址:服务器之间通信端口:服务器之间投票选举端口
    # 集群服务器IP列表如下
    server.1=192.168.5.100:2881:3881
    server.2=192.168.5.101:2881:3881
    server.3=192.168.5.1:2881:3881
  3. 根据上述的配置,在每个 zookeeper的data 目录下创建一个 myid 文件,内容分别是 1、2、3 。这个文件就是记录每个服务器的 ID。比如这里服务1、2、3分别代表192.168.5.100、192.168.5.101、192.168.5.1这三台机器。

    1
    2
    3
    4
    5
    6
    # 在192.168.5.100执行
    touch data/myid && echo 1 > data/myid
    # 在192.168.5.101执行
    touch data/myid && echo 2 > data/myid
    # 在192.168.5.1执行
    touch data/myid && echo 3 > data/myid

2)启动集群

启动集群就是分别启动每个实例,即在三台启动Zookeeper:

1
./bin/zkServer.sh start

启动成功后会显示STARTED,如下:

1
2
3
4
[litong@localhost apache-zookeeper-3.5.6-bin]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/apache-zookeeper-3.5.6-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

3)集群验证

启动成功后,我们可以在三台机器上输入以下命令即可查询集群状态:

1
./bin/zkServer.sh status

192.168.5.100机器的输出结果:

1
2
3
4
5
[root@localhost apache-zookeeper-3.5.6-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/apache-zookeeper-3.5.6-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

192.168.5.101机器的输出结果:

1
2
3
4
5
[root@localhost apache-zookeeper-3.5.6-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/apache-zookeeper-3.5.6-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader

192.168.5.1机器的输出结果:

1
2
3
4
5
litong@LT:/media/litong/软件/programs/tools/apache-zookeeper-3.5.6-bin/bin$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /media/litong/软件/programs/tools/apache-zookeeper-3.5.6-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

从三台机器的状态可以查看,192.168.5.101机器上显示Mode: leader,是主节点(master),其他两台机器上显示Mode: follower,则是两个slave。

3. 参考文档

​ 1. Zookeeper主页

​ 2. Zookeeper文档

支付宝打赏 微信打赏

请作者喝杯咖啡吧