配置zookeeper集群

1.安装JDK
yum install -y jdk-8u201-linux-x64.rpm
2.配置zookeeper
tar -zxvf zookeeper-3.4.12.tar.gz
zoo.cfg
server.1=node1:2888:3888
server.2=node2:2888:3888
myid
/tmp/zookeeper/myid
3.开启防火墙
firewall-cmd –zone=public –add-port=2888/tcp –permanent
firewall-cmd –zone=public –add-port=2181/tcp –permanent
firewall-cmd –zone=public –add-port=3888/tcp –permanent
firewall-cmd –reload
4.启动zookeeper
./zkServer.sh start
2019-03-01 03:11:27,218 [myid:] – INFO  [main:QuorumPeerConfig@136] – Reading configuration from: /zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
2019-03-01 03:11:27,228 [myid:] – INFO  [main:QuorumPeer$QuorumServer@184] – Resolved hostname: node1 to address: node1/192.168.6.239
2019-03-01 03:11:27,228 [myid:] – INFO  [main:QuorumPeer$QuorumServer@184] – Resolved hostname: node2 to address: node2/192.168.6.238
2019-03-01 03:11:27,229 [myid:] – WARN  [main:QuorumPeerConfig@351] – No server failure will be tolerated. You need at least 3 servers.
2019-03-01 03:11:27,229 [myid:] – INFO  [main:QuorumPeerConfig@398] – Defaulting to majority quorums
2019-03-01 03:11:27,231 [myid:1] – INFO  [main:DatadirCleanupManager@78] – autopurge.snapRetainCount set to 3
2019-03-01 03:11:27,231 [myid:1] – INFO  [main:DatadirCleanupManager@79] – autopurge.purgeInterval set to 0
2019-03-01 03:11:27,231 [myid:1] – INFO  [main:DatadirCleanupManager@101] – Purge task is not scheduled.
2019-03-01 03:11:27,239 [myid:1] – INFO  [main:QuorumPeerMain@130] – Starting quorum peer
2019-03-01 03:11:27,245 [myid:1] – INFO  [main:ServerCnxnFactory@117] – Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2019-03-01 03:11:27,248 [myid:1] – INFO  [main:NIOServerCnxnFactory@89] – binding to port 0.0.0.0/0.0.0.0:2181
2019-03-01 03:11:27,253 [myid:1] – INFO  [main:QuorumPeer@1158] – tickTime set to 2000
2019-03-01 03:11:27,253 [myid:1] – INFO  [main:QuorumPeer@1204] – initLimit set to 10
2019-03-01 03:11:27,254 [myid:1] – INFO  [main:QuorumPeer@1178] – minSessionTimeout set to -1
2019-03-01 03:11:27,254 [myid:1] – INFO  [main:QuorumPeer@1189] – maxSessionTimeout set to -1
2019-03-01 03:11:27,259 [myid:1] – INFO  [main:QuorumPeer@1467] – QuorumPeer communication is not secured!
2019-03-01 03:11:27,264 [myid:1] – INFO  [main:QuorumPeer@1496] – quorum.cnxn.threads.size set to 20
2019-03-01 03:11:27,266 [myid:1] – INFO  [main:FileSnap@86] – Reading snapshot /tmp/zookeeper/version-2/snapshot.200000000
2019-03-01 03:11:27,272 [myid:1] – INFO  [ListenerThread:QuorumCnxManager$Listener@736] – My election bind port: node1/192.168.6.239:3888
2019-03-01 03:11:27,278 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumPeer@909] – LOOKING
2019-03-01 03:11:27,279 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:FastLeaderElection@813] – New election. My id =  1, proposed zxid=0x200000000
2019-03-01 03:11:27,280 [myid:1] – INFO  [WorkerReceiver[myid=1]:FastLeaderElection@595] – Notification: 1 (message format version), 1 (n.leader), 0x200000000 (n.zxid), 0x1 (n.round), LOOKING (n.state), 1 (n.sid), 0x4 (n.peerEpoch) LOOKING (my state)
2019-03-01 03:11:27,283 [myid:1] – INFO  [WorkerSender[myid=1]:QuorumCnxManager@347] – Have smaller server identifier, so dropping the connection: (2, 1)
2019-03-01 03:11:27,284 [myid:1] – INFO  [node1/192.168.6.239:3888:QuorumCnxManager$Listener@743] – Received connection request /192.168.6.238:57158
2019-03-01 03:11:27,287 [myid:1] – INFO  [WorkerReceiver[myid=1]:FastLeaderElection@595] – Notification: 1 (message format version), 2 (n.leader), 0x200000000 (n.zxid), 0x5 (n.round), LOOKING (n.state), 2 (n.sid), 0x4 (n.peerEpoch) LOOKING (my state)
2019-03-01 03:11:27,287 [myid:1] – INFO  [WorkerReceiver[myid=1]:FastLeaderElection@595] – Notification: 1 (message format version), 2 (n.leader), 0x200000000 (n.zxid), 0x5 (n.round), LOOKING (n.state), 1 (n.sid), 0x4 (n.peerEpoch) LOOKING (my state)
2019-03-01 03:11:27,288 [myid:1] – INFO  [WorkerReceiver[myid=1]:FastLeaderElection@595] – Notification: 1 (message format version), 2 (n.leader), 0x200000000 (n.zxid), 0x5 (n.round), LOOKING (n.state), 2 (n.sid), 0x4 (n.peerEpoch) LOOKING (my state)
2019-03-01 03:11:27,492 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumPeer@979] – FOLLOWING
2019-03-01 03:11:27,503 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Learner@86] – TCP NoDelay set to: true
2019-03-01 03:11:27,517 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2019-03-01 03:11:27,517 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:host.name=node1
2019-03-01 03:11:27,517 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.version=1.8.0_201
2019-03-01 03:11:27,518 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.vendor=Oracle Corporation
2019-03-01 03:11:27,518 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.home=/usr/java/jdk1.8.0_201-amd64/jre
2019-03-01 03:11:27,518 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.class.path=/zookeeper/zookeeper-3.4.12/bin/../build/classes:/zookeeper/zookeeper-3.4.12/bin/../build/lib/*.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/zookeeper/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/zookeeper/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/zookeeper/zookeeper-3.4.12/bin/../conf:
2019-03-01 03:11:27,519 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-03-01 03:11:27,519 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.io.tmpdir=/tmp
2019-03-01 03:11:27,519 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:java.compiler=<NA>
2019-03-01 03:11:27,519 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:os.name=Linux
2019-03-01 03:11:27,520 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:os.arch=amd64
2019-03-01 03:11:27,520 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:os.version=3.10.0-957.el7.x86_64
2019-03-01 03:11:27,520 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:user.name=root
2019-03-01 03:11:27,520 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:user.home=/root
2019-03-01 03:11:27,521 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] – Server environment:user.dir=/zookeeper/zookeeper-3.4.12/bin
2019-03-01 03:11:27,525 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:ZooKeeperServer@173] – Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 datadir /tmp/zookeeper/version-2 snapdir /tmp/zookeeper/version-2
2019-03-01 03:11:27,527 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Follower@65] – FOLLOWING – LEADER ELECTION TOOK – 248
2019-03-01 03:11:27,530 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumPeer$QuorumServer@184] – Resolved hostname: node2 to address: node2/192.168.6.238
2019-03-01 03:11:27,547 [myid:1] – INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Learner@332] – Getting a diff from the leader 0x200000000
5.验证
[root@zookeeper239 bin]#  ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
[root@zookeeper238 ~]# /zookeeper/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader
6.测试leader选举
使用两台zookeeper无法验证leader选举功能(部署三台zookeeper服务器后解决)
(1)关闭leader
[root@zookeeper239 bin]#  ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Stopping zookeeper … STOPPED
(2)查看新leader
[root@zookeeper237 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader
2019-03-01 04:03:57,655 [myid:2] – INFO  [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:Learner@336] – Getting a snapshot from leader 0x800000000
2019-03-01 04:03:57,658 [myid:2] – INFO  [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2181:FileTxnSnapLog@296] – Snapshotting: 0x800000000 to /tmp/zookeeper/version-2/snapshot.800000000