Zookeeper集群模式安装和配置
主要参考了网上的教程,把我自己安装过程写下来,方便自己,也方便别人。
1、下载zookeeper-3.4.10.tar.gz:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/ ,我下载的版本是3.4.10。
2、解压缩zookeeper-3.4.10.tar.gz:
tar -zxvf zookeeper-3.4.10.tar.gz
我的解压目录为:/usr,减压后/usr/ zookeeper-3.4.10即为安装目录:
3、修改zookeeper配置文件:
进入zookeeper配置文件目录,复制zoo_sample.cfg 文件的并命名为为 zoo.cfg
cd /usr/zookeeper-3.4.10/conf
cp zoo_sample.cfg zoo.cfg
4、用 vim 打开 zoo.cfg 文件并修改其内容为如下:
# The number of milliseconds of each tick
#zookeeper 定义的基准时间间隔,单位:毫秒
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 anacknowledgement
syncLimit=5
# the directory where the snapshot isstored.
# do not use /tmp for storage, /tmp here isjust
# example sakes.
#dataDir=/tmp/zookeeper
# 数据文件夹
dataDir=/usr/zookeeper-3.4.10/data
# 日志文件夹
dataLogDir=/usr/zookeeper-3.4.10/data/logs
# the port at which the clients willconnect
# 客户端访问zookeeper 的端口号
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle moreclients
#配置集群服务器
server.1=master:2888:3888
server.2=slaver1:2888:3888
server.3=slaver2:2888:3888
maxClientCnxns=60
#
# Be sure to read the maintenance sectionof the
# administrator guide before turning onautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain indataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable autopurge feature
#autopurge.purgeInterval=1
保存并关闭 zoo.cfg 文件。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
5、创建zoo.cfg配置文件中的data目录和logs目录
cd /usr/zookeeper-3.4.10/
mkdir data
cd data
mkdir logs
6、在data目录下创建myid文件
在dataDir目录下创建一个myid文件,然后分别在myid文件中按照zoo.cfg文件的server.A中A的数值,在不同机器上的该文件中填写相应的值。
7、用 vim 打开 /etc/ 目录下的配置文件 profile:
vim /etc/profile
并在其尾部追加如下内容:
export ZOOKEEPER_HOME=/usr/zookeeper-3.4.10/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使 /etc/ 目录下的 profile 文件即可生效:
source /etc/profile
8、按照上述方法同样在另外2台机器上面配置或者直接将该机器上面的文件拷贝到另外的2台机器上面 ,我这里采用的是复制
scp -r /usr/zookeeper-3.4.10slaver1:/usr/zookeeper-3.4.10
scp -r /usr/zookeeper-3.4.10slaver2:/usr/zookeeper-3.4.10
9、修改另外2台服务器myid文件中的值。
10、启动zookeeper 服务:
zkServer.sh start
如打印如下信息则表明启动成功:
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
13、查询zookeeper 状态:
zkServer.sh status
14、关闭zookeeper 服务:
zkServer.sh stop
如打印如下信息则表明成功关闭:
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
15、重启zookeeper 服务:
zkServer.sh restart
如打印如下信息则表明重启成功:
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.10/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED