三台CentOS 7系统
JDK 1.8
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
tar -zxvf zookeeper-3.4.13.tar.gz
cd zookeeper-3.4.13/conf
cp zoo_sample.cfg zoo.cfg
创建数据文件和日志文件目录
mkdir -p /var/lib/zookeeper/data
mkdir -p /var/lib/zookeeper/logs
vi zoo.cfg
修改下面的参数
dataDir=/var/lib/zookeeper/data #数据文件目录
dataLogDir=/var/lib/zookeeper/logs #日志文件目录
clientPort=2181 #端口
集群服务器都要进行上述1、2、3步操作
vi zoo.cfg
如果集群里服务器的机器名是zk1、zk2、zk3,那么配置文件可能是这样的(确保每台服务器的2888、3888端口没有被占用,机器名也可以用ip,server后面跟的数字为服务器的ID):
server.1=zk1:2888:3888
server.2=zk2:2888:3888
server.3=zk3:2888:3888
每个服务器还要在数据文件目录(/var/lib/zookeeper/data)中创建一个myid文件,用于指明自己的 ID
每个服务器分别在hosts文件中配置zk1、zk2、zk3
在每台服务器上执行下面的命令启动zookeeper:
zookeeper-3.4.13/bin/zkServer.sh start
zookeeper-3.4.13/bin/zkServer.sh status
应有一台主机提示如下信息
JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower
其余主机提示下面的信息则证明安装成功
JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader
当查看ZooKeeper状态时显示Error contacting service. It is probably not running.,可以查看zookeeper.out日志。
查看zookeeper.out日志可以看到是哪些机器连不上,可能是网络、ip、端口、配置文件、myid文件的问题。
如果有如下异常可以忽略,因为集群环境中某些子节点还没有启动 zookeeper。
- WARN [WorkerSender[myid=3]:QuorumCnxManager@400] - Cannot open channel to 1 at election address CLTQ-132-173/172.24.132.173:3888
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:381)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:354)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:452)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:433)
at java.lang.Thread.run(Thread.java:745)