zookeeper作用: 主从,选举,配置中心,数据监听…简单来说就是一个存储数据的地方,每个数据变更,能通知到对应的监听者。
zookeeper部署上
内存 一般4g 就够(视zk的DataTree来)。磁盘由于 Zookeeper 的数据写入磁盘(由于事务性),建议使用 SSD。
操作系统上(一般关闭交换区)
Zookeeper 的性能会很明显受到交换分区的影响。建议部署 Zookeeper 的服务器关闭交换分区功能或是通过内核参数调整,减少对于交换分区的使用。
不建议 Zookeeper 和 Kafka Broker 部署在同一台服务器,除非做好资源隔离或是机器有明显的资源富余。
zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper/data
dataLogDir=/tmp/zookeeper/datalog
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
# Setting this to 0 entirely removes the limit on concurrent connections.
maxClientCnxns=120
#
# 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=24
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
# Disable AdminServer
admin.enableServer=false
#server.1=zoo1:2888:3888
#server.2=zoo2:2888:3888
#server.3=zoo3:2888:3888
配置说明,需要根据实际情况修改
tickTime配置的单位是毫秒。这个配置是 Zookeeper 的基础操作的长度。心跳或是各种超时的时长都是基于tickTime计算得出。比如会话超时设置,minSessionTimeout默认值是 2 倍tickTime,maxSessionTimeout默认值是 20 倍tickTime。
clientPort是 Zookeeper 进程的网络监听端口,一般用端口号 2181。如果服务器有多块网卡,可以通过clientPortAddress指明 Zookeeper 进程监听的 IP 地址。可以不配置clientPortAddress,默认的是绑定所有的网卡和相应的 IP 地址。建议对于生产环境下多网卡的服务器进行配置。
initLimit和syncLimit是分别用来配置 Zookeeper 初始化和同步(允许最大不同步)时长。也是基于tickTime计算得出。这里推荐的配置是initLimit=5,Zookeeper 最大 10 秒(5 * 2000 毫秒)完成和 leader 节点的初始化。syncLimit=2是说允许集群中的节点最长有 4 秒的不同步。如果 Zookeeper 集群节点都在一个 VLAN 下,这样的配置是足够的。如果 Zookeeper 集群要跨交换机(网络延迟相对较高),要重新评估这两项配置。
maxClientCnxns是最大的客户连接数。对于Kafka来说,就是最大有多少 Broker 可以连到Zookeeper节点上。默认值是60。配置成0是对客户端连接无限制。强烈建议根据实际情况(比如 Kafka broker 的数量),配置成一个固定值,以防止因为太多的客户端连接从而用尽资源。
dataDir是 Zookeeper 用来存放内存 snapshot 的路径。强烈建议dataDir要配置到 SSD 硬盘上以提升性能。如果不配置dataLogDir, 默认事务日志会写到dataDir路径下。这样会带来写 Snapshot 和写事务日志的 IO 操作竞争。这里建议,明确配置dataLogDir,把事务日志写到另外的硬盘上以减少 IO 操作竞争。
默认情况下,Zookeeper 不会清理 Snapshot,这样会造成硬盘上 Snapshot 文件越来越多。对于 kafka 的场景,建议打开自动清理功能。配置成autopurge.snapRetainCount=3和autopurge.purgeInterval=24。意思是每 24 小时自动执行清理 Snapshot 任务,保留最新的三份 Snapshot。
Zookeeper 3.5.0 后引入了 admin server,默认打开。建议配置admin.enableServer=false关闭 admin server。
集群大小建议3或是5节点起步,一般 3 节点(允许 1 节点宕机)在可用性上就足够了。以后可以实际集群压力再进行扩展。不建议一开始就上特别多节点的 Zookeeper 集群。