在 CentOS 上 ZooKeeper 频繁掉线(Session Expired / 连接断开 / 节点踢出),通常不是单一原因,而是网络、系统资源、配置、GC、时钟等多方面综合导致。下面按最常见 → 较少见的顺序给你系统性排查思路。
ZooKeeper 对网络极其敏感。
表现
DisconnectedConnection broken for session ...
排查
# 丢包
ping zk-host
mtr zk-host
# 端口连通
telnet zk-host 2181
# 看是否有丢包/重传
netstat -s | grep -i retransmit
常见根因
✅ 建议:
tcp_keepalivesysctl -w net.ipv4.tcp_keepalive_time=60
ZooKeeper 是 会话机制,不是长连接保活。
默认问题
tickTime=2000sessionTimeout 通常 2 * tickTime ~ 20 * tickTime现象
Session 0xXXXX expired
建议配置(生产)
tickTime=2000
minSessionTimeout=4000
maxSessionTimeout=40000
客户端也别乱设 sessionTimeout,别小于 4s。
ZooKeeper 是 JVM 应用,Full GC 一次 = 掉线。
排查
# GC 日志
grep -i "Full GC" zookeeper.out
jstat -gcutil <pid> 1000
典型问题
✅ 建议:
-Xms4g -Xmx4g
-XX:+UseG1GC
ZooKeeper 非常怕 IO 卡顿
重点看
top
iostat -x 1
free -m
常见问题:
✅ 必须:
dataDir 和 dataLogDir 分开sysctl -w vm.swappiness=1
quorum 没达成 → 频繁选主server.1=ip1:2888:3888
server.2=ip2:2888:3888
server.3=ip3:2888:3888
maxClientCnxns=60
太多客户端 → 被踢
ZooKeeper 对时间极度敏感。
ntpstat
timedatectl
✅ 必须:
chrony / ntpdulimit -n
ZK 需要很高:
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
# 1. 网络
ping zk
mtr zk
# 2. 资源
top
iostat -x 1
# 3. JVM
jstat -gcutil <pid> 1000
# 4. 时钟
ntpstat
# 5. 日志
grep -E "Expired|Disconnected|Exception" zookeeper.out
你可以贴出以下信息之一:
zookeeper.out 报错片段zoo.cfg我可以直接告诉你是哪一环出问题。