zookeeper集群搭建

原创
2017/10/20 18:06
阅读数 136

概要

整体的搭建十分简单,只需要区别一些各个实例的dataDirclientPort配置信息即可。

整体结构:

➜  ~ tree -L 1 zookeepers
zookeepers
├── data
├── zk.sh
├── zookeeper-3.4.9-2181
├── zookeeper-3.4.9-2182
└── zookeeper-3.4.9-2183

4 directories, 1 file

配置

当前使用的zookeeper版本为3.4.9,好像默认只能读取conf/zoo.cfg配置文件,无法在运行时指定。所以,直接将zk复制三份,分别配置。

zookeeper-3.4.9-2181/conf/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=/Users/lpe234/zookeepers/data/2181
# 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=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

另外两个的配置文件类似,只需要修改一下 dataDirclientPort,另外的server.[myid]配置是个重点。

dataDir的文件结构如下:

➜  ~ tree -L 2 zookeepers/data
zookeepers/data
├── 2181
│   ├── myid
│   ├── version-2
│   └── zookeeper_server.pid
├── 2182
│   ├── myid
│   ├── version-2
│   └── zookeeper_server.pid
└── 2183
    ├── myid
    ├── version-2
    └── zookeeper_server.pid

6 directories, 6 files

myid

也就是cfg文件中,server.[myid]的配置。即是每个server的唯一ID。

# 依次生成即可, 这样才能与cfg配置的信息相对应起来
➜  zookeepers echo 1 > data/2181/myid  
➜  zookeepers echo 2 > data/2182/myid  
➜  zookeepers echo 3 > data/2183/myid  

运行

依次执行启动命令即可。

➜  ~ zookeeper-3.4.9/bin/zkServer.sh -h
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeeper-3.4.9/bin/../conf/zoo.cfg
Usage: zookeeper-3.4.9/bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}

启动脚本

zk.sh

#!/usr/bin/env bash

# set -e

case "${1}" in
    start)
        echo "start"
        cd /Users/lpe234/zookeepers/zookeeper-3.4.9-2181 && bin/zkServer.sh start
        cd /Users/lpe234/zookeepers/zookeeper-3.4.9-2182 && bin/zkServer.sh start
        cd /Users/lpe234/zookeepers/zookeeper-3.4.9-2183 && bin/zkServer.sh start
        ;;
    stop)
        echo "stop"
        zookeeper-3.4.9-2181/bin/zkServer.sh stop
        zookeeper-3.4.9-2182/bin/zkServer.sh stop
        zookeeper-3.4.9-2183/bin/zkServer.sh stop
        ;;
    status)
        zookeeper-3.4.9-2181/bin/zkServer.sh status
        zookeeper-3.4.9-2182/bin/zkServer.sh status
        zookeeper-3.4.9-2183/bin/zkServer.sh status
        ;;
    *)
        echo "only support {start|status|stop}"
        ;;
esac

查看运行情况

➜  zookeepers ./zk.sh start
start
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2181/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2182/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2183/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

➜  zookeepers ./zk.sh status
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2181/bin/../conf/zoo.cfg
Mode: follower
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2182/bin/../conf/zoo.cfg
Mode: leader
ZooKeeper JMX enabled by default
Using config: /Users/lpe234/zookeepers/zookeeper-3.4.9-2183/bin/../conf/zoo.cfg
Mode: follower
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部