1.官方下载地址:http://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
当时官方3.5版本的包有问题所以用的3.4的,最新正式版地址:http://mirror.bit.edu.cn/apache/zookeeper/stable/
2.解压安装:
[root@centos60 tmp]# tar zxvf zookeeper-3.4.14.tar.gz -C /usr/local/
[root@centos60 tmp]# cd /usr/local/zookeeper-3.4.14/
[root@centos60 zookeeper-3.4.14]# ls
bin dist-maven lib pom.xml src zookeeper-3.4.14.jar.md5 zookeeper-contrib zookeeper-jute
build.xml ivysettings.xml LICENSE.txt README.md zookeeper-3.4.14.jar zookeeper-3.4.14.jar.sha1 zookeeper-docs zookeeper-recipes
conf ivy.xml NOTICE.txt README_packaging.txt zookeeper-3.4.14.jar.asc zookeeper-client zookeeper-it zookeeper-server
[root@centos60 zookeeper-3.4.14]# cd conf/
[root@centos60 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[root@centos60 conf]# cp zoo_sample.cfg zoo.cfg
[root@centos60 conf]# vi zoo.cfg
# The number of milliseconds of each tick
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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-3.4.14/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=centos60:2888:3888
server.2=centos62:2888:3888
server.3=centos64:2888:3888
修改点:
dataDir=/usr/local/zookeeper-3.4.14/data
及文件末尾添加:
server.1=centos60:2888:3888
server.2=centos62:2888:3888
server.3=centos64:2888:3888
3.创建myid文件:
[root@centos60 conf]# cd ..
[root@centos60 zookeeper-3.4.14]# mkdir data
[root@centos60 zookeeper-3.4.14]# cd data/
[root@centos60 data]# touch myid
[root@centos60 data]# echo 1 >> myid
[root@centos60 data]# cat myid
1
[root@centos60 data]# cd ../../
scp到其它2节点,修改myid的值:
[root@centos60 local]# scp -r zookeeper-3.4.14 root@centos62:/usr/local/zookeeper-3.4.14/
[root@centos60 local]# scp -r zookeeper-3.4.14 root@centos64:/usr/local/zookeeper-3.4.14/
[root@centos62 ~]# cd /usr/local/zookeeper-3.4.14/data
[root@centos62 data]# vi myid
2
[root@centos64 ~]# cd /usr/local/zookeeper-3.4.14/data/
[root@centos64 data]# vi myid
3
4.启动zookeeper,3个节点都要执行:
[root@centos60 bin]# pwd
/usr/local/zookeeper-3.4.14/bin
[root@centos60 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@centos60 bin]# jps
172550 QuorumPeerMain
172838 Jps
若有报错:
[root@centos60 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@centos60 bin]# cat zookeeper.out
2019-07-29 20:23:42,049 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 3 at election address centos64/192.168.56.64:3888
java.net.ConnectException: Connection refused (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:558)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:610)
at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:838)
at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:958)
发现是60防火墙的iptables没有关闭,关闭防火墙:
[root@centos60 bin]# systemctl stop firewalld.service
[root@centos60 bin]# sudo service iptables stop
Redirecting to /bin/systemctl stop iptables.service
[root@centos60 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower
正常情况下有一台节点会是leader,其他2节点是follower。