1、环境
1.1 三台机器网络互通,并做hosts解析
准备三台及以上ubuntu24.04主机(奇数)
root@zk-node01:~# hostname
zk-node01root@zk-node01:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 u24-server10.0.49.215 zk-node01
10.0.49.216 zk-node02
10.0.49.217 zk-node03# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
1.2 系统
root@zk-node01:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
1.3 Java JDK
root@zk-node01:~# java --version
openjdk 17.0.15 2025-04-15
OpenJDK Runtime Environment (build 17.0.15+6-Ubuntu-0ubuntu124.04)
OpenJDK 64-Bit Server VM (build 17.0.15+6-Ubuntu-0ubuntu124.04, mixed mode, sharing)
2、配置文件
2.1 下载最新稳定版 3.8.4版
地址:https://zookeeper.net.cn/releases.html
root@zk-node01:~# ls
apache-zookeeper-3.8.4-bin.tar.gz
2.2 将文件解压后复制到 /opt/zookeeper 目录
root@zk-node01:~# ls /opt/zookeeper
bin conf docs lib LICENSE.txt logs NOTICE.txt README.md README_packaging.md
2.3 设置conf目录下的配置文件
vim /opt/zookeeper/conf/zoo.cfg
在文件末尾添加以下内容
server.1=zk-node01:2888:3888
server.2=zk-node02:2888:3888
server.3=zk-node03:2888:38884lw.commands.whitelist=*
验证
root@zk-node01:~# cat /opt/zookeeper/conf/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=/var/lib/zookeeper
# 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
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# https://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.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.1=zk-node01:2888:3888
server.2=zk-node02:2888:3888
server.3=zk-node03:2888:38884lw.commands.whitelist=*
2.4 每个主机的data目录下写入myid
如:配置中 dataDir=/var/lib/zookeeper
则在对应目录写入myid,每台机器有不同的ID(如:1,2,3等)。
root@zk-node01:~# echo "1" >/var/lib/zookeeper/myid root@zk-node01:~# cat /var/lib/zookeeper/myid
1
3、启动集群
/opt/zookeeper/bin/zkServer.sh start
#或者
/opt/zookeeper/bin/zkServer.sh restart
4、验证
root@zk-node01:~# /opt/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: followerroot@zk-node02:~# /opt/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leaderroot@zk-node03:~# /opt/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
启用命令行工作
/opt/zookeeper/bin/zkCli.sh
5、参考文档
ZooKeeper:因为协调分布式系统是一家动物园 - ZooKeeper 协调服务