zookeeper110、zookeeper111、zookeeper112 三台机器组成zookeeper集群服务端
|
Zookeeper110 |
Zookeeper111 |
Zookeeper112 |
QuorumPeerMain |
√ |
√ |
√ |
官网下载压缩包
http://zookeeper.apache.org/
解压到/opt/module/目录
$ tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module/
在/opt/module/apache-zookeeper-3.5.7-bin目录下创建zkData
$ mkdir -p zkData
在/opt/module/apache-zookeeper-3.5.7-bin/zkData目录下创建myid
$ touch myid
$ vi myid
在文件中添加与sever对应的编号
1
重命名/opt/module/apache-zookeeper-3.5.7-bin/conf这个目录下的zoo_sample.cfg为zoo.cfg
$ mv zoo_sample.cfg zoo.cfg
修改zoo.cfg文件
修改数据存储路径配置
dataDir=/opt/module/apache-zookeeper-3.5.7-bin/zkData
增加如下配置
######cluster######
server.1=zookeeper110:2888:3888
server.2=zookeeper111:2888:3888
server.3=zookeeper112:2888:3888
server.后面的数字是服务器编号,对应的是3.3中配置的myid,每台机器都有自己各自的myid
zookeeper110是服务器地址(笔者自己配置的hosts文件)
2888是zookeeper集群之间的数据通讯端口(Follower与Leader服务器交换信息的端口,其中包含副本等数据)
3888是zookeeper集群之间的选举Leader所用的端口
同步/opt/module/apache-zookeeper-3.5.7-bin 到其他机器
$ xsync apache-zookeeper-3.5.7-bin/
修改各机器的myid(参考3.3)
(笔者这里修改其他机器上的myid内容为2、3)
$ bin/zkServer.sh start
$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
zookeeper没有提供群起的脚本,可以根据自己的需要自行编写,方便后续使用,笔者这里提供自己的脚本供参考。
在bin目录下创建zookeeper群起脚本文件
$ touch zkServer-all.sh
$ vi zkServer-all.sh
填写以下内容
#!/bin/bash
case $1 in
"start"){
for i in zookeeper110 zookeeper111 zookeeper112
do
echo "----------start $i----------"
ssh $i "/opt/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh start"
done
};;
"stop"){
for i in zookeeper110 zookeeper111 zookeeper112
do
echo "----------stop $i----------"
ssh $i "/opt/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh stop"
done
};;
"status"){
for i in zookeeper110 zookeeper111 zookeeper112
do
echo "----------status $i----------"
ssh $i "/opt/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh status"
done
};;
esac
$ bin/zkServer-all.sh start
$ bin/zkServer-all.sh stop
启动后的zookeeper进程
7685 QuorumPeerMain