目录
前言:
zookeeper动态扩/缩容的reconfig命令旨在不需要重启zookeeper中任何一个节点的情况下,对整个zookeeper集群进行动态扩/缩容。
zookeeper客户端支持的命令:
bash
[zk: localhost:2181(CONNECTED) 5] 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
在实际验证中发现一些细节问题,先记录如下:
1、如果整个zk集群只有一个zk节点(即serverId只有它本身),这时默认运行的是单机模式,如果需要运行在集群模式下,需要加入standaloneEnabled=false的配置。
2、使用zkCli客户端提供的reconfig命令,需要修改/zookeper/config节点,普通账号对该节点上的数据只有ACL中的查看权限,有如下两种方法可以解决:
①关闭ACL,配置文件加入 skipACL=yes
②生成超级管理员账号:
Linux可直接执行如下命名:
bash
echo -n cjian:111111 | openssl dgst -binary -sha1 | openssl base64
cjian:111111 为用户名和密码
Dzookeeper.DigestAuthenticationProvider.superDigest=cjian:ST25maIOy8WImjpR7Nf2/D2wFXQ=
使用zkCli连接上后,执行addauth digest cjian:111111
之后就可以修改/zookeper/config节点的数据了,即可执行reconfig命令
3、要使用reconfig命令需要配置reconfigEnabled=true
实操一下:
下载zookeeper不做赘述~
一、启动一个zk
配置第一个zk(myid为0),配置如下:
bash
initLimit=10
syncLimit=5
skipACL=yes
# 加上这个配置 可通过 echo stat|nc ip:port 命令查看实例角色
4lw.commands.whitelist=*
clientPort=2181
tickTime=2000
dataDir=/home/zookeeper/zookeeper1/data
dataLogDir=/home/zookeeper/zookeeper1/log
# 关闭 单机模式
standaloneEnabled=false
# 支持reconfig
reconfigEnabled=true
admin.enableServer=false
server.0=127.0.0.1:2888:3888
启动后,执行
bash
[root@localhost zookeeper1]# echo stat|nc 127.0.0.1 2181
Zookeeper version: 3.8.1-74db005175a4ec545697012f9069cb9dcc8cdda7, built on 2023-01-25 16:31 UTC
Clients:
/127.0.0.1:42266[1](queued=0,recved=224,sent=224)
/127.0.0.1:42288[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/1.4933/42
Received: 226
Sent: 225
Connections: 2
Outstanding: 0
Zxid: 0x90000000a
Mode: leader
Node count: 6
Proposal sizes last/min/max: 239/36/239
发现zk 0 的角色为leader,符合预期
二、扩容一个zk
拷贝一份,myid修改为1,配置文件修改如下:
XML
initLimit=10
syncLimit=5
skipACL=yes
# 加上这个配置 可通过 echo stat|nc ip:port 命令查看实例角色
4lw.commands.whitelist=*
clientPort=2182
tickTime=2000
dataDir=/home/zookeeper/zookeeper2/data
dataLogDir=/home/zookeeper/zookeeper2/log
# 关闭 单机模式
standaloneEnabled=false
# 支持reconfig
reconfigEnabled=true
admin.enableServer=false
server.0=127.0.0.1:2888:3888
server.1=127.0.0.1:2889:3889
注意: 这里扩容的实例的配置需要加上目标集群leader的server.id配置,如果只配置server.1=127.0.0.1:2889:3889,启动后,它是个leader,这时我们在2181的客户端连接上执行reconfig -add XXX命令时,会提示:
bash
[zk: localhost:2181(CONNECTED) 0] reconfig -add server.1=127.0.0.1:2889:3889:participant;2182
No quorum of new config is connected and up-to-date with the leader of last commmitted config - try invoking reconfiguration after new servers are connected and synced
正常配置并启动完成后,在2181的客户端连接上执行add命令:
(这里连上2182的客户端执行也是一样的,该请求zk内部会转发给leader)
bash
[zk: localhost:2181(CONNECTED) 3] reconfig -add server.1=127.0.0.1:2889:3889:participant;2182
Committed new configuration:
server.0=127.0.0.1:2888:3888:participant
server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
# 这是一个新的配置文件版本号标识,会生成zoo.cfg.dynamic.version=900000005文件
version=900000005
bash
[root@localhost zookeeper1]# cat conf/zoo.cfg.dynamic.900000005
server.0=127.0.0.1:2888:3888:participant
server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
bash
[root@localhost zookeeper2]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/zookeeper/zookeeper2/bin/../conf/zoo.cfg
Client port not found in static config file. Looking in dynamic config file.
Client port found: 2182. Client address: 0.0.0.0. Client SSL: false.
Mode: follower
发现扩容后的实例角色为follower,符合预期,连上客户端查看配置:
bash
[root@localhost zookeeper2]# ./bin/zkCli.sh -server 127.0.0.1:2182
...
[zk: 127.0.0.1:2182(CONNECTED) 0] config
server.0=127.0.0.1:2888:3888:participant
server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
version=900000005
三、缩容一个zk
现在我们将第二步扩容的zk,拿掉,连上2181的客户端,并执行命令
bash
[zk: localhost:2181(CONNECTED) 7] reconfig -remove 1
Committed new configuration:
server.0=127.0.0.1:2888:3888:participant
version=90000000d
bash
[root@localhost zookeeper1]# cat conf/zoo.cfg.dynamic.90000000d
server.0=127.0.0.1:2888:3888:participant
查看下2182的配置换和角色
bash
[root@localhost zookeeper2]# cat conf/zoo.cfg
initLimit=10
syncLimit=5
skipACL=yes
4lw.commands.whitelist=*
tickTime=2000
dataDir=/home/zookeeper/zookeeper2/data
dataLogDir=/home/zookeeper/zookeeper2/log
reconfigEnabled=true
admin.enableServer=false
standaloneEnabled=false
dynamicConfigFile=/home/zookeeper/zookeeper2/conf/zoo.cfg.dynamic.90000000d
[root@localhost zookeeper2]# cat conf/zoo.cfg.dynamic.90000000d
server.0=127.0.0.1:2888:3888:participant
bash
[root@localhost zookeeper2]# echo stat|nc 127.0.0.1 2182
Zookeeper version: 3.8.1-74db005175a4ec545697012f9069cb9dcc8cdda7, built on 2023-01-25 16:31 UTC
Clients:
/127.0.0.1:37140[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0.7989/30
Received: 176
Sent: 175
Connections: 1
Outstanding: 0
Zxid: 0x90000000d
Mode: follower
Node count: 6
发现还是follower,只不过已经脱离了集群;
四、重新配置集群的节点
可通过如下命令进行整个zk集群的配置更新
reconfig -members serverID=host:port1:port2;port3[,...]
其它的一些注意事项和命令说明可以翻阅官网的reconfig章节~