This is article 27 in the Big Data series. Detailed explanation of each parameter in ZooKeeper cluster configuration file zoo.cfg, and demonstrate complete startup and verification process.
Complete illustrated version: CSDN Original | Juejin
Experimental Environment
| Host | Spec | Role (After Startup) |
|---|---|---|
| h121.wzk.icu | 2C4G | Follower |
| h122.wzk.icu | 2C4G | Leader |
| h123.wzk.icu | 2C2G | Follower |
zoo.cfg Complete Configuration Analysis
# ---- Data Storage ----
# Directory for storing memory snapshots
dataDir=/opt/servers/apache-zookeeper-3.8.4-bin/data
# Directory for storing transaction logs (WAL), recommend separate from dataDir to avoid I/O contention
dataLogDir=/opt/servers/apache-zookeeper-3.8.4-bin/logs
# ---- Network ----
# Client connection port, default 2181
clientPort=2181
# ---- Time Parameters ----
# ZooKeeper base time unit (milliseconds), Session timeout, heartbeat all based on this
tickTime=2000
# Follower initial connection Leader timeout = initLimit × tickTime = 10 seconds
initLimit=5
# Follower and Leader sync heartbeat timeout = syncLimit × tickTime = 4 seconds
# Follower not responding within this time will be kicked out of cluster
syncLimit=2
# ---- Auto Cleanup ----
# Number of snapshots and logs to retain (keep at least 3)
autopurge.snapRetainCount=3
# Auto cleanup interval (hours), 0 means no cleanup, 1 means cleanup every hour
autopurge.purgeInterval=1
# ---- Cluster Nodes ----
# Format: server.<myid>=<host>:<quorumPort>:<electionPort>
# quorum port (2888): Follower syncs data with Leader
# election port (3888): Leader election communication
server.1=h121.wzk.icu:2888:3888
server.2=h122.wzk.icu:2888:3888
server.3=h123.wzk.icu:2888:3888
Parameter Explanation Points
tickTime: Base for all timeout calculations. Client Session timeout defaults to 2 × tickTime to 20 × tickTime (negotiated by client).
initLimit vs syncLimit:
initLimit: Maximum wait time for Follower to first connect to Leader and complete data sync. Increase when data volume is large.syncLimit: Normal operation Follower and Leader heartbeat detection timeout. Follower will be removed from quorum on timeout.
Dual Port Design:
2888(quorum port): Data sync, only Follower → Leader direction3888(election port): Used in Leader election phase, basically idle after election completes
myid File Configuration
myid is the unique identifier for ZooKeeper to recognize cluster member identity, must correspond one-to-one with server.<id> in zoo.cfg.
# Confirm dataDir directory exists
ls /opt/servers/apache-zookeeper-3.8.4-bin/data/
# h121 node
echo 1 > /opt/servers/apache-zookeeper-3.8.4-bin/data/myid
# h122 node
echo 2 > /opt/servers/apache-zookeeper-3.8.4-bin/data/myid
# h123 node
echo 3 > /opt/servers/apache-zookeeper-3.8.4-bin/data/myid
Note:
myidfile can only have one number, no trailing empty lines, otherwise startup will error.
Cluster Startup Process
Execute on each of three machines (order doesn’t affect final result, but recommend starting one by one for easier troubleshooting):
/opt/servers/apache-zookeeper-3.8.4-bin/bin/zkServer.sh start
Status changes during startup:
- After first node starts, only 1 node, cannot form quorum, stays in
LOOKINGstate - After second node starts, 2 nodes exceed half (3/2+1=2), triggers Leader election, generates Leader
- After third node starts, joins existing cluster as Follower
Verify Cluster Status
# View each node role
zkServer.sh status
Expected output example:
# h121
Mode: follower
# h122
Mode: leader
# h123
Mode: follower
View startup logs:
tail -f /opt/servers/apache-zookeeper-3.8.4-bin/logs/zookeeper-root-server-h121.out
When LEADING or FOLLOWING appears in logs, election is complete.
Client Connection Test
# Connect to any node
/opt/servers/apache-zookeeper-3.8.4-bin/bin/zkCli.sh -server h121.wzk.icu:2181
# Verify root node
ls /
# Expected output
[zookeeper]
Firewall Port Configuration
Cluster nodes need to open these ports (distinguish between client ports and cluster internal ports):
| Port | Purpose |
|---|---|
| 2181 | Client connection |
| 2888 | Follower and Leader data sync |
| 3888 | Leader election communication |
Common Issues Troubleshooting
Q: Node stays in LOOKING state after startup
- Check if
myidfile content is correct - Check if each node’s
zoo.cfgis exactly the same (server.x configuration) - Check if firewall opens 2888/3888 ports
Q: Log reports Cannot open channel to X at election address
- Hostname resolution failed between nodes, check
/etc/hostsconfiguration
Summary
Core of zoo.cfg is three groups of parameters: storage paths (dataDir/dataLogDir), time parameters (tickTime/initLimit/syncLimit), cluster topology (server.x). With unique myid file, ZooKeeper can complete automatic election and provide services.