zookeeper安装

下载

官网

配置详解

单机部署

1、解压

1
tar -zxvf zookeeper-3.4.13.tar.gz

2、配置文件

zookeeper默认读取配置文件zoo.cfg但实际上不存在该文件,因此需要将模板配置文件复制更改文件名。

1
2
cd conf/
cp zoo_sample.cfg zoo.cfg

若不创建zoo.cfg配置文件,zookeeper启动报以下异常。

1
2
3
4
5
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.13/bin/../conf/zoo.cfg
grep: /usr/soft/zookeeper-3.4.13/bin/../conf/zoo.cfg: No such file or directory
mkdir: cannot create directory ‘’: No such file or directory
Starting zookeeper ... STARTED

3、启动

1
./bin/zkServer.sh start

控制台输出以下内容则说明启动成功。

1
2
3
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

4、查看状态

1
./bin/zkServer.sh status

standalone表示当前zookeeper为单机环境运行。

1
2
3
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: standalone

5、默认端口

zookeeper默认监听3个端口:

  • 2181:对cline端提供服务
  • 3888:选举leader使用
  • 2888:集群内机器通讯使用(Leader监听此端口)

集群部署

解压

准备3个zookeeper目录

1
2
3
zookeeper-1
zookeeper-2
zookeeper-3

配置文件

分别修改三个zookeeper目录下的配置文件conf/zoo.cfg

zookeeper-1:

1
2
3
4
5
6
dataDir=/usr/soft/zookeeper/zookeeper-1/data
dataLogDir=/usr/soft/zookeeper/zookeeper-1/log
clientPort=2181
server.1=localhost:2288:3388
server.2=localhost:2289:3389
server.3=localhost:2290:3390

zookeeper-2:

1
2
3
4
5
6
dataDir=/usr/soft/zookeeper/zookeeper-2/data
dataLogDir=/usr/soft/zookeeper/zookeeper-2/log
clientPort=2182
server.1=localhost:2288:3388
server.2=localhost:2289:3389
server.3=localhost:2290:3390

zookeeper-3:

1
2
3
4
5
6
dataDir=/usr/soft/zookeeper/zookeeper-3/data
dataLogDir=/usr/soft/zookeeper/zookeeper-3/log
clientPort=2183
server.1=localhost:2288:3388
server.2=localhost:2289:3389
server.3=localhost:2290:3390

其中配置server.A=B:C:D表示zookeeper集群所有机器(包括自己)的配置:

  • A:zookeeper的myid,值域为[1,255]
  • B:服务器ip
  • C:leader选举的端口
  • D:zookeeper之间的通信端口

myid

设置各zookeeper的myid值,在各zookeeper的dataDir目录下创建名为myid的文件,文件里的数据为该zookeeper的myid值,即server.A=B:C:D中的A。一个zookeeper集群中各个zookeeper的myid必须唯一。

zookeeper-1:

1
2
mkdir zookeeper-1/data
echo 1 > zookeeper-1/data/myid

zookeeper-2:

1
2
mkdir zookeeper-2/data
echo 2 > zookeeper-2/data/myid

zookeeper-3:

1
2
mkdir zookeeper-3/data
echo 3 > zookeeper-3/data/myid

启动

1
2
3
./zookeeper-1/bin/zkServer.sh start
./zookeeper-2/bin/zkServer.sh start
./zookeeper-3/bin/zkServer.sh start

查看状态

zookeeper-1:

1
2
3
4
$ ./zookeeper-1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper/zookeeper-1/bin/../conf/zoo.cfg
Mode: follower

zookeeper-2:

1
2
3
4
$ ./zookeeper-2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper/zookeeper-2/bin/../conf/zoo.cfg
Mode: follower

zookeeper-3:

1
2
3
4
$ ./zookeeper-3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/soft/zookeeper/zookeeper-3/bin/../conf/zoo.cfg
Mode: leader

可见zookeeper-3为leader,其余为follower。

>