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=/opt/zookeeper/tmp # the port at which the clients will connect clientPort=2181 # # 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 dataLogDir=/opt/zookeeper/logs server.1=master:2888:3888 server.2=slave1:2888:3888 server.3=slave2:2888:3888 server.4=slave3:2888:3888
[root@master zookeeper]# /opt/zookeeper/bin/zkServer.sh start JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
2018-01-2310:48:35,470[myid:]-INFO[main:QuorumPeerConfig@101]-Readingconfigurationfrom: /opt/zookeeper/bin/../conf/zoo.cfg 2018-01-2310:48:35,484[myid:]-WARN[main:QuorumPeerConfig@290]-Non-optimialconfiguration, consideranoddnumberofservers. 2018-01-2310:48:35,484[myid:]-INFO[main:QuorumPeerConfig@334]-Defaultingtomajorityquorums 2018-01-2310:48:35,512[myid:4]-INFO[main:DatadirCleanupManager@78]-autopurge.snapRetainCountsetto3 2018-01-2310:48:35,513[myid:4]-INFO[main:DatadirCleanupManager@79]-autopurge.purgeIntervalsetto0 2018-01-2310:48:35,513[myid:4]-INFO[main:DatadirCleanupManager@101]-Purgetaskisnotscheduled. 2018-01-2310:48:35,536[myid:4]-INFO[main:QuorumPeerMain@132]-Startingquorumpeer 2018-01-2310:48:35,587[myid:4]-INFO[main:NIOServerCnxnFactory@94]-bindingtoport0.0.0.0/0.0.0.0:2181 2018-01-2310:48:35,611[myid:4]-INFO[main:QuorumPeer@913]-tickTimesetto2000 2018-01-2310:48:35,612[myid:4]-INFO[main:QuorumPeer@933]-minSessionTimeoutsetto-1 2018-01-2310:48:35,612[myid:4]-INFO[main:QuorumPeer@944]-maxSessionTimeoutsetto-1 2018-01-2310:48:35,612[myid:4]-INFO[main:QuorumPeer@959]-initLimitsetto10 2018-01-2310:48:35,639[myid:4]-INFO[main:QuorumPeer@429]-currentEpochnotfound! Creatingwithareasonabledefaultof0. Thisshouldonlyhappenwhen you are upgrading your installation 2018-01-2310:48:35,643 [myid:4] - INFO [main:QuorumPeer@444] - acceptedEpoch not found! Creating with a reasonable default of 0. This should only happen when you are upgrading your installation 2018-01-2310:48:35,652 [myid:4] - INFO [Thread-1:QuorumCnxManager$Listener@486] - My election bind port: 0.0.0.0/0.0.0.0:3888 2018-01-2310:48:35,674 [myid:4] - INFO [QuorumPeer[myid=4]/0:0:0:0:0:0:0:0:2181:QuorumPeer@670] - LOOKING 2018-01-2310:48:35,679 [myid:4] - INFO [QuorumPeer[myid=4]/0:0:0:0:0:0:0:0:2181:FastLeaderElection@740] - New election. My id = 4, proposed zxid=0x0 2018-01-2310:48:35,692 [myid:4] - INFO [slave3/192.168.137.14:3888:QuorumCnxManager$Listener@493] - Received connection request /192.168.137.11:34491 2018-01-2310:48:35,704 [myid:4] - INFO [WorkerReceiver[myid=4]:FastLeaderElection@542] - Notification: 4 (n.leader), 0x0 (n.zxid), 0x1 (n.round), LOOKING (n.state), 4 (n.sid), 0x0 (n.peerEPoch), LOOKING (my state) 2018-01-2310:48:35,706 [myid:4] - WARN [WorkerSender[myid=4]:QuorumCnxManager@368] - Cannot open channel to 2 at election address slave1/192.168.137.12: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:354) at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:327) at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:393) at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:365) at java.lang.Thread.run(Thread.java:748)
[root@master zookeeper]# /opt/zookeeper/bin/zkServer.sh start JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@master zookeeper]# /opt/zookeeper/bin/zkServer.sh status JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Error contacting service. It is probably not running.
到其他节点服务器上都启动zookeeper,过一会儿后每个服务器查看状态:
1 2 3 4 5 6 7
[root@master zookeeper]# /opt/zookeeper/bin/zkServer.sh status JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Mode: follower [root@master zookeeper]# jps 5488 QuorumPeerMain 5539 Jps
如果有Mode和QuorumPeerMain,就说明已经启动成功了。
如果要关闭zookeeper,需要在每个节点上执行:
/opt/zookeeper/bin/zkServer.sh stop
另外如果使用如下命令启动,就会在启动时输出日志信息:
/opt/zookeeper/bin/zkServer.sh start-foreground
批量启动和关闭
一台一台服务器去执行命令有点麻烦,写一个脚本批量执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/bash
#下面变量修改zookeeper安装目录 zooHome=/opt/zookeeper if [ $1 != "" ] then confFile=$zooHome/conf/zoo.cfg slaves=$(cat "$confFile" | sed '/^server/!d;s/^.*=//;s/:.*$//g;/^$/d') for salve in$slaves ; do ssh $salve"$zooHome/bin/zkServer.sh $1" done else echo"parameter empty! parameter:start|stop" fi
将上面脚本保存为zooManager文件,调用执行:
sh zooManager start
sh zooManager stop
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@master opt]# sh zooManager start JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED