zookeeper集群搭建

文章目录

zookeeper集群搭建

一、环境

  • 三台主机
hostname ip
zk01 192.168.80.10
zk02 192.168.80.11
zk03 192.168.80.12

二、搭建zookeeper集群

2.1 准备java环境

            Linux配置JDK环境

2.2 下载解压zookeeper

wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.6.3-bin.tar.gz
tar zxvf zookeeper-3.6.3-bin.tar.gz
mv apache-zookeeper-3.6.3-bin zookeeper-3.6
mv zookeeper-3.6 /usr/local/
cd /usr/local/zookeeper-3.6/

2.3 创建数据目录和日志目录

cd /usr/local/zookeeper-3.6/
mkdir data
mkdir logs

2.4 更改配置文件

cd conf/
cp zoo_sample.cfg zoo.cfg
vim 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.6/data
dataLogDir=/usr/local/zookeeper-3.6/logs
# the port at which the clients will connect
clientPortAdress=192.168.80.10
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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.0=192.168.80.10:2888:3888
server.1=192.168.80.11:2888:3888
server.2=192.168.80.12:2888:3888

配置说明:clientPortAdress=主机ip
server.0 其中0是myid

2.5 输入myid

cd /usr/local/zookeeper-3.6/
echo "0" > data/myid               //0要和配置文件里面的0对应起来
							       //zk02输入命令就是   echo "1" > data/myid   zk03同理 

2.6 配置zookeeper环境变量

vim /etc/profile
//末尾添加
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.6
export PATH=$ZOOKEEPER_HOME/bin:$PATH

source /etc/profile

三、启动zookeeper集群

3.1 常用命令

zkServer.sh start
zkServer.sh stop
zkServer.sh start-foreground
zkServer.sh status

3.2 日志

日志在 /usr/local/zookeeper-3.6/logs/ 目录下

3.3 启动

zkServer.sh start
zkServer.sh status

zkServer.sh status 可以查看主机状态:follower或者leader

3.4 测试

可以关闭一台leader主机,然后zookeeper会从剩余的两台follower中选举一台主机位leader。

zkServer.sh stop

3.5 详细日志

2021-10-20 18:35:00,872 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Follower@143] - Disconnected from leader (with address: /192.168.80.11:2888). Was connected for 2347196ms. Sync state: true
2021-10-20 18:35:00,872 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Follower@292] - shutdown Follower
2021-10-20 18:35:00,872 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@811] - shutting down
2021-10-20 18:35:00,872 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestThrottler@244] - Shutting down
2021-10-20 18:35:00,875 [myid:2] - INFO  [RequestThrottler:RequestThrottler@205] - Draining request throttler queue
2021-10-20 18:35:00,875 [myid:2] - INFO  [RequestThrottler:RequestThrottler@181] - RequestThrottler shutdown. Dropped 0 requests
2021-10-20 18:35:00,876 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:FOLLOWING; n.sid:0, n.state:LOOKING, n.leader:0, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,877 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:FOLLOWING; n.sid:0, n.state:LOOKING, n.leader:0, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,878 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:FOLLOWING; n.sid:0, n.state:LOOKING, n.leader:0, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,879 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:FOLLOWING; n.sid:0, n.state:LOOKING, n.leader:0, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,880 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:FOLLOWING; n.sid:0, n.state:LOOKING, n.leader:0, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,899 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):FollowerRequestProcessor@148] - Shutting down
2021-10-20 18:35:00,899 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):CommitProcessor@617] - Shutting down
2021-10-20 18:35:00,899 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):FinalRequestProcessor@661] - shutdown of request processor complete
2021-10-20 18:35:00,900 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@864] - Peer state changed: looking
2021-10-20 18:35:00,900 [myid:2] - WARN  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@1513] - PeerState set to LOOKING
2021-10-20 18:35:00,900 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@1383] - LOOKING
2021-10-20 18:35:00,900 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):FastLeaderElection@944] - New election. My id = 2, proposed zxid=0x300000000
2021-10-20 18:35:00,901 [myid:2] - INFO  [FollowerRequestProcessor:2:FollowerRequestProcessor@112] - FollowerRequestProcessor exited loop!
2021-10-20 18:35:00,901 [myid:2] - INFO  [CommitProcessor:2:CommitProcessor@406] - CommitProcessor exited loop!
2021-10-20 18:35:00,908 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:LOOKING; n.sid:2, n.state:LOOKING, n.leader:2, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x300000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:00,908 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:LOOKING; n.sid:0, n.state:LOOKING, n.leader:2, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x300000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:01,109 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@858] - Peer state changed: leading
2021-10-20 18:35:01,110 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@1477] - LEADING
2021-10-20 18:35:01,113 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):LearnerMaster@47] - zookeeper.leader.maxConcurrentSnapSyncs = 10
2021-10-20 18:35:01,114 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):LearnerMaster@50] - zookeeper.leader.maxConcurrentDiffSyncs = 100
2021-10-20 18:35:01,114 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@84] - TCP NoDelay set to: true
2021-10-20 18:35:01,114 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@105] - zookeeper.leader.ackLoggingFrequency = 1000
2021-10-20 18:35:01,114 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@1354] - zookeeper.leader.maxTimeToWaitForEpoch = -1ms
2021-10-20 18:35:01,115 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@1273] - minSessionTimeout set to 4000
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@1282] - maxSessionTimeout set to 40000
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ResponseCache@45] - Response cache size is initialized with value 400.
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ResponseCache@45] - Response cache size is initialized with value 400.
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@109] - zookeeper.pathStats.slotCapacity = 60
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@110] - zookeeper.pathStats.slotDuration = 15
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@111] - zookeeper.pathStats.maxDepth = 6
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@112] - zookeeper.pathStats.initialDelay = 5
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@113] - zookeeper.pathStats.delay = 5
2021-10-20 18:35:01,116 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):RequestPathMetricsCollector@114] - zookeeper.pathStats.enabled = false
2021-10-20 18:35:01,117 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@1498] - The max bytes for all large requests are set to 104857600
2021-10-20 18:35:01,117 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@1512] - The large request threshold is set to -1
2021-10-20 18:35:01,117 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@339] - Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 clientPortListenBacklog -1 datadir /usr/local/zookeeper-3.6/logs/version-2 snapdir /usr/local/zookeeper-3.6/data/version-2
2021-10-20 18:35:01,123 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@581] - LEADING - LEADER ELECTION TOOK - 223 MS
2021-10-20 18:35:01,125 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@864] - Peer state changed: leading - discovery
2021-10-20 18:35:01,126 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):FileTxnSnapLog@470] - Snapshotting: 0x300000000 to /usr/local/zookeeper-3.6/data/version-2/snapshot.300000000
2021-10-20 18:35:01,126 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ZooKeeperServer@529] - Snapshot taken in 1 ms
2021-10-20 18:35:01,222 [myid:2] - INFO  [LearnerHandler-/192.168.80.10:53406:LearnerHandler@504] - Follower sid: 0 : info : 192.168.80.10:2888:3888:participant
2021-10-20 18:35:01,240 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@1820] - Dynamic reconfig is disabled, we don't store the last seen config.
2021-10-20 18:35:01,949 [myid:2] - INFO  [LearnerHandler-/192.168.80.10:53406:ZKDatabase@345] - On disk txn sync enabled with snapshotSizeFactor 0.33
2021-10-20 18:35:01,949 [myid:2] - INFO  [LearnerHandler-/192.168.80.10:53406:LearnerHandler@807] - Synchronizing with Learner sid: 0 maxCommittedLog=0x0 minCommittedLog=0x0 lastProcessedZxid=0x300000000 peerLastZxid=0x100000000
2021-10-20 18:35:01,951 [myid:2] - INFO  [LearnerHandler-/192.168.80.10:53406:LearnerHandler@566] - Sending snapshot last zxid of peer is 0x100000000, zxid of leader is 0x400000000, send zxid of db as 0x300000000, 1 concurrent snapshot sync, snapshot sync was exempt from throttle
2021-10-20 18:35:01,954 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@864] - Peer state changed: leading - synchronization
2021-10-20 18:35:01,957 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@1504] - Have quorum of supporters, sids: [[0, 2]]; starting up and setting last processed zxid: 0x400000000
2021-10-20 18:35:01,957 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):Leader@1526] - Dynamic reconfig feature is disabled, skip designatedLeader calculation and reconfig processing.
2021-10-20 18:35:01,959 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):CommitProcessor@476] - Configuring CommitProcessor with readBatchSize -1 commitBatchSize 1
2021-10-20 18:35:01,959 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):CommitProcessor@438] - Configuring CommitProcessor with 1 worker threads.
2021-10-20 18:35:01,963 [myid:2] - INFO  [ProcessThread(sid:2 cport:-1)::PrepRequestProcessor@136] - PrepRequestProcessor (sid:2) started, reconfigEnabled=false
2021-10-20 18:35:01,963 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):ContainerManager@83] - Using checkIntervalMs=60000 maxPerMinute=10000 maxNeverUsedIntervalMs=0
2021-10-20 18:35:01,965 [myid:2] - INFO  [QuorumPeer[myid=2](plain=[0:0:0:0:0:0:0:0]:2181)(secure=disabled):QuorumPeer@864] - Peer state changed: leading - broadcast
2021-10-20 18:35:02,124 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:LEADING; n.sid:1, n.state:LEADING, n.leader:1, n.round:0x3, n.peerEpoch:0x3, n.zxid:0x100000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:03,122 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection$Messenger$WorkerReceiver@389] - Notification: my state:LEADING; n.sid:1, n.state:LOOKING, n.leader:1, n.round:0x4, n.peerEpoch:0x3, n.zxid:0x300000000, message format version:0x2, n.config version:0x0
2021-10-20 18:35:03,139 [myid:2] - INFO  [LearnerHandler-/192.168.80.11:55826:LearnerHandler@504] - Follower sid: 1 : info : 192.168.80.11:2888:3888:participant
2021-10-20 18:35:03,144 [myid:2] - INFO  [LearnerHandler-/192.168.80.11:55826:ZKDatabase@345] - On disk txn sync enabled with snapshotSizeFactor 0.33
2021-10-20 18:35:03,144 [myid:2] - INFO  [LearnerHandler-/192.168.80.11:55826:LearnerHandler@807] - Synchronizing with Learner sid: 1 maxCommittedLog=0x0 minCommittedLog=0x0 lastProcessedZxid=0x400000000 peerLastZxid=0x300000000
2021-10-20 18:35:03,145 [myid:2] - INFO  [LearnerHandler-/192.168.80.11:55826:LearnerHandler@566] - Sending snapshot last zxid of peer is 0x300000000, zxid of leader is 0x400000000, send zxid of db as 0x400000000, 1 concurrent snapshot sync, snapshot sync was exempt from throttle
2021-10-20 19:19:13,698 [myid:2] - INFO  [NIOWorkerThread-2:NIOServerCnxn@507] - Processing srvr command from /127.0.0.1:34368
上一篇:systemctl命令管理nginx


下一篇:【Dart 教程系列第 35 篇】Dart 之 contains 判断字符串是否包含指定元素