环境准备:

1
2
3
172.16.10.145 test-zookeeper1
172.16.10.149 test-zookeeper2
172.16.10.150 test-zookeeper3

JDK

1
2
3
4
java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

下载安装包

https://archive.apache.org/dist/zookeeper/

安装

解压安装包

1
2
3
4
cd /opt
tar axvf zookeeper-3.4.10.tar.gz
#创建软连,方法更新
ln -s zookeeper-3.4.10 zookeeper

创建logdata目录

1
mkdir -p /opt/zookeeper/{data,logs}

准备配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cat > /opt/zookeeper/conf/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper/data
dataLogDir=/opt/zookeeper/logs
clientPort=2181

#server.x=
server.1=172.16.10.145:2888:3888
server.2=172.16.10.149:2888:3888
server.3=172.16.10.150:2888:3888
EOF

创建myid文件

1
echo "X" > /opt/zookeeper/data/myid

X代表zoo.cfg配置里的server.后边的编号。

启动服务

1
2
3
4
$ /opt/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apps/zookeeper-b/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看服务状态

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

客户端

1
/opt/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181

默认端口作用

  • 2181:客户端连接
  • 2888:集群内部通讯(leader监听此端口)
  • 3888:集群选举leader使用

集群角色

  • Leader
  • Follower
  • Observer 观察者

一个ZooKeeper集群同一时刻只会有一个Leader,其他都是Follower或Observer。

参考

https://zookeeper.apache.org/doc/current/zookeeperStarted.html