一 系统设置

1.1 时间同步

~# apt -y install chrony
~# systemctl start chrony
~# systemctl enable chrony

1.2 服务器规划

角色

IP

 

zk-01

192.168.174.103

 

zk-02

192.168.174.104

 

zk-03

192.168.174.105

 

1.3 软件清单

  • zookeeper:3.7.0

二 安装jdk

~# wget https://download.java.net/java/GA/jdk17.0.1/2a2082e5a09d4267845be086888add4f/12/GPL/openjdk-17.0.1_linux-x64_bin.tar.gz
~# tar xf jdk-17_linux-x64_bin.tar.gz -C /usr/local/
~# ln -sv /usr/local/jdk-17.0.1/ /usr/local/jdk
'/usr/local/jdk' -> '/usr/local/jdk-17.0.1/'
~# cat /etc/profile

export JAVA_HOME=/usr/local/jdk

export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
~# . /etc/profile

~# java -version

openjdk version "17.0.1" 2021-10-19

OpenJDK Runtime Environment (build 17.0.1+12-39)

OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)

三 安装zookeeper

3.1 zk-01节点

3.1.1 下载zookeeper安装包

root@zk-01:~# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0.tar.gz

3.1.2 安装zookeeper

root@zk-01:~# tar xf apache-zookeeper-3.7.0.tar.gz -C /usr/local/
root@zk-01:~# ln -sv /usr/local/apache-zookeeper-3.7.0/ /usr/local/zookeeper
'/usr/local/zookeeper' -> '/usr/local/apache-zookeeper-3.7.0/'

3.1.3 准备zookeeper配置文件

root@zk-01:~# cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
root@zk-01:~# egrep -v "^#|^$" /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/tmp/zookeeper/wal
autopurge.purgeInterval=1
quorumListenOnAllIPs=true
clientPort=2181
server.1=192.168.174.103:2888:3888
server.2=192.168.174.104:2888:3888
server.3=192.168.174.105:2888:3888

3.1.4 添加环境变量

root@zk-01:~# cat /etc/profile.d/zookeeper.sh
export PATH=$PATH:/usr/local/zookeeper/bin
root@zk-01:~# . /etc/profile

3.1.5 创建ServerID标识

root@zk-01:~# mkdir -pv /tmp/zookeeper/
root@zk-01:~# echo 1 > /tmp/zookeeper/myid

3.2 zk-02节点

3.2.1 下载zookeeper安装包

root@zk-02:~# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

3.2.2 安装zookeeper

root@zk-02:~# tar xf apache-zookeeper-3.7.0-bin.tar.gz -C /usr/local/
root@zk-02:~# ln -sv /usr/local/apache-zookeeper-3.7.0-bin/ /usr/local/zookeeper
'/usr/local/zookeeper' -> '/usr/local/apache-zookeeper-3.7.0-bin/'

3.2.3 准备zookeeper配置文件

root@zk-02:~# cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
root@zk-02:~# egrep -v "^#|^$" /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/tmp/zookeeper/wal
autopurge.purgeInterval=1
quorumListenOnAllIPs=true
clientPort=2181
server.1=192.168.174.103:2888:3888
server.2=192.168.174.104:2888:3888
server.3=192.168.174.105:2888:3888

3.2.4 添加环境变量

root@zk-02:~# cat /etc/profile.d/zookeeper.sh
export PATH=$PATH:/usr/local/zookeeper/bin
root@zk-02:~# . /etc/profile

3.2.5 创建ServerID标识

root@zk-02:~# mkdir -pv /tmp/zookeeper/
root@zk-02:~# echo 2 > /tmp/zookeeper/myid

3.3 zk-03节点

3.3.1 下载zookeeper安装包

root@zk-03:~# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0.tar.gz

3.3.2 安装zookeeper

root@zk-03:~# tar xf apache-zookeeper-3.7.0.tar.gz -C /usr/local/
root@zk-03:~# ln -sv /usr/local/apache-zookeeper-3.7.0/ /usr/local/zookeeper
'/usr/local/zookeeper' -> '/usr/local/apache-zookeeper-3.7.0/'

3.3.3 准备zookeeper配置文件

root@zk-03:~# cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
root@zk-03:~# egrep -v "^#|^$" /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/tmp/zookeeper/wal
autopurge.purgeInterval=1
quorumListenOnAllIPs=true
clientPort=2181
server.1=192.168.174.103:2888:3888
server.2=192.168.174.104:2888:3888
server.3=192.168.174.105:2888:3888

3.3.4 添加环境变量

root@zk-03:~# cat /etc/profile.d/zookeeper.sh
export PATH=$PATH:/usr/local/zookeeper/bin
root@zk-03:~# . /etc/profile

3.3.5 创建ServerID标识

root@zk-03:~# mkdir -pv /tmp/zookeeper/
root@zk-03:~# echo 3 > /tmp/zookeeper/myid

四 启动zookeeper集群

root@zk-01:~# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
root@zk-01:~# /usr/local/zookeeper/bin/zkServer.sh start

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED
root@zk-03:~# /usr/local/zookeeper/bin/zkServer.sh start

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

五 查看集群信息

5.1 查看版本信息

root@zk-01:~# /usr/local/zookeeper/bin/zkServer.sh version
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Apache ZooKeeper, version 3.7.0 2021-03-17 09:46 UTC

5.2 查看当前角色

root@zk-01:~# /usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
root@zk-02:~# /usr/local/zookeeper/bin/zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Client port found: 2181. Client address: localhost. Client SSL: false.

Mode: leader
root@zk-03:~# /usr/local/zookeeper/bin/zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg

Client port found: 2181. Client address: localhost. Client SSL: false.

Mode: follower

六 客户端连接zkserver

6.1 命令行zkcli.sh 连接zkserver 

点击查看代码

root@zk-01:~# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.174.103:2181
Connecting to 192.168.174.103:2181
2021-11-24 05:56:48,558 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2021-11-24 05:56:48,560 [myid:] - INFO [main:Environment@98] - Client environment:host.name=192.168.174.103
2021-11-24 05:56:48,560 [myid:] - INFO [main:Environment@98] - Client environment:java.version=17.0.1
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/usr/local/jdk-17.0.1
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.7.0.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/usr/local/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.30.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/usr/local/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.59.Final.jar:/usr/local/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/jline-2.14.6.jar:/usr/local/zookeeper/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.38.v20210224.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.5.1.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.4.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.12.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/local/jdk/lib:/usr/local/jdk/jre/lib:/usr/local/jdk/lib/tools.jar
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-11-24 05:56:48,561 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=<NA>
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:os.version=5.4.0-90-generic
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/root
2021-11-24 05:56:48,562 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=27MB
2021-11-24 05:56:48,565 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=256MB
2021-11-24 05:56:48,565 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=34MB
2021-11-24 05:56:48,568 [myid:] - INFO [main:ZooKeeper@637] - Initiating client connection, connectString=192.168.174.103:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@17f6480
2021-11-24 05:56:48,575 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-11-24 05:56:48,581 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-11-24 05:56:48,588 [myid:] - INFO [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
2021-11-24 05:56:48,593 [myid:192.168.174.103:2181] - INFO [main-SendThread(192.168.174.103:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server 192.168.174.103/192.168.174.103:2181.
2021-11-24 05:56:48,593 [myid:192.168.174.103:2181] - INFO [main-SendThread(192.168.174.103:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-11-24 05:56:48,599 [myid:192.168.174.103:2181] - INFO [main-SendThread(192.168.174.103:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /192.168.174.103:41280, server: 192.168.174.103/192.168.174.103:2181
Welcome to ZooKeeper!
JLine support is enabled
2021-11-24 05:56:48,622 [myid:192.168.174.103:2181] - INFO [main-SendThread(192.168.174.103:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server 192.168.174.103/192.168.174.103:2181, session id = 0x10000c2237a0001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null

[zk: 192.168.174.103:2181(CONNECTED) 0]

6.2  查看帮助信息

[zk: 192.168.174.103:2181(CONNECTED) 0] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b|-N|-B val path
stat [-w] path
sync path
version
whoami
Command not found: Command not found help

6.3 使用示例

[zk: 192.168.174.103:2181(CONNECTED) 4] ls /
[zookeeper]
[zk: 192.168.174.103:2181(CONNECTED) 5] create /zk_test my_data
Created /zk_test
[zk: 192.168.174.103:2181(CONNECTED) 6] ls /
[zk_test, zookeeper]
[zk: 192.168.174.103:2181(CONNECTED) 7] get /zk_test
my_data
[zk: 192.168.174.103:2181(CONNECTED) 8] set /zk_test wgs
[zk: 192.168.174.103:2181(CONNECTED) 9] get /zk_test
wgs
[zk: 192.168.174.103:2181(CONNECTED) 11] delete /zk_test

七 其它设置

7.1 禁止自动创建datadir目录

当运行zkServer.sh 时,可以通过将环境变量ZOO_DATADIR_AUTOCREATE_DISABLE设置为 1来禁用自动创建。当直接从类文件运行 ZooKeeper 服务器时,这可以通过在 java 命令行上设置zookeeper.datadir.autocreate=false来实现,即-Dzookeeper.datadir .autocreate=false