当前位置:   article > 正文

zookeeper动态扩缩容(无需重启)

zookeeper动态扩缩容(无需重启)

目录

一、启动一个zk

二、扩容一个zk

三、缩容一个zk

四、重新配置集群的节点


前言:

zookeeper动态扩/缩容的reconfig命令旨在不需要重启zookeeper中任何一个节点的情况下,对整个zookeeper集群进行动态扩/缩容。

zookeeper客户端支持的命令:

  1. [zk: localhost:2181(CONNECTED) 5] help
  2. ZooKeeper -server host:port -client-configuration properties-file cmd args
  3. addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
  4. addauth scheme auth
  5. close
  6. # 查看配置
  7. config [-c] [-w] [-s]
  8. connect host:port
  9. create [-s] [-e] [-c] [-t ttl] path [data] [acl]
  10. delete [-v version] path
  11. deleteall path [-b batch size]
  12. delquota [-n|-b|-N|-B] path
  13. get [-s] [-w] path
  14. getAcl [-s] path
  15. getAllChildrenNumber path
  16. getEphemerals path
  17. history
  18. listquota path
  19. ls [-s] [-w] [-R] path
  20. printwatches on|off
  21. quit
  22. # 动态配置
  23. reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
  24. redo cmdno
  25. removewatches path [-c|-d|-a] [-l]
  26. set [-s] [-v version] path data
  27. setAcl [-s] [-v version] [-R] path acl
  28. setquota -n|-b|-N|-B val path
  29. stat [-w] path
  30. sync path
  31. version
  32. whoami

在实际验证中发现一些细节问题,先记录如下:

1、如果整个zk集群只有一个zk节点(即serverId只有它本身),这时默认运行的是单机模式,如果需要运行在集群模式下,需要加入standaloneEnabled=false的配置。

2、使用zkCli客户端提供的reconfig命令,需要修改/zookeper/config节点,普通账号对该节点上的数据只有ACL中的查看权限,有如下两种方法可以解决:

①关闭ACL,配置文件加入 skipACL=yes

②生成超级管理员账号:

Linux可直接执行如下命名:

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),配置如下:

  1. initLimit=10
  2. syncLimit=5
  3. skipACL=yes
  4. # 加上这个配置 可通过 echo stat|nc ip:port 命令查看实例角色
  5. 4lw.commands.whitelist=*
  6. clientPort=2181
  7. tickTime=2000
  8. dataDir=/home/zookeeper/zookeeper1/data
  9. dataLogDir=/home/zookeeper/zookeeper1/log
  10. # 关闭 单机模式
  11. standaloneEnabled=false
  12. # 支持reconfig
  13. reconfigEnabled=true
  14. admin.enableServer=false
  15. server.0=127.0.0.1:2888:3888

启动后,执行

  1. [root@localhost zookeeper1]# echo stat|nc 127.0.0.1 2181
  2. Zookeeper version: 3.8.1-74db005175a4ec545697012f9069cb9dcc8cdda7, built on 2023-01-25 16:31 UTC
  3. Clients:
  4. /127.0.0.1:42266[1](queued=0,recved=224,sent=224)
  5. /127.0.0.1:42288[0](queued=0,recved=1,sent=0)
  6. Latency min/avg/max: 0/1.4933/42
  7. Received: 226
  8. Sent: 225
  9. Connections: 2
  10. Outstanding: 0
  11. Zxid: 0x90000000a
  12. Mode: leader
  13. Node count: 6
  14. Proposal sizes last/min/max: 239/36/239

发现zk 0 的角色为leader,符合预期

二、扩容一个zk

拷贝一份,myid修改为1,配置文件修改如下:

  1. initLimit=10
  2. syncLimit=5
  3. skipACL=yes
  4. # 加上这个配置 可通过 echo stat|nc ip:port 命令查看实例角色
  5. 4lw.commands.whitelist=*
  6. clientPort=2182
  7. tickTime=2000
  8. dataDir=/home/zookeeper/zookeeper2/data
  9. dataLogDir=/home/zookeeper/zookeeper2/log
  10. # 关闭 单机模式
  11. standaloneEnabled=false
  12. # 支持reconfig
  13. reconfigEnabled=true
  14. admin.enableServer=false
  15. server.0=127.0.0.1:2888:3888
  16. server.1=127.0.0.1:2889:3889

注意: 这里扩容的实例的配置需要加上目标集群leader的server.id配置,如果只配置server.1=127.0.0.1:2889:3889,启动后,它是个leader,这时我们在2181的客户端连接上执行reconfig -add XXX命令时,会提示:

  1. [zk: localhost:2181(CONNECTED) 0] reconfig -add server.1=127.0.0.1:2889:3889:participant;2182
  2. 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)

  1. [zk: localhost:2181(CONNECTED) 3] reconfig -add server.1=127.0.0.1:2889:3889:participant;2182
  2. Committed new configuration:
  3. server.0=127.0.0.1:2888:3888:participant
  4. server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
  5. # 这是一个新的配置文件版本号标识,会生成zoo.cfg.dynamic.version=900000005文件
  6. version=900000005
  1. [root@localhost zookeeper1]# cat conf/zoo.cfg.dynamic.900000005
  2. server.0=127.0.0.1:2888:3888:participant
  3. server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
  1. [root@localhost zookeeper2]# ./bin/zkServer.sh status
  2. ZooKeeper JMX enabled by default
  3. Using config: /home/zookeeper/zookeeper2/bin/../conf/zoo.cfg
  4. Client port not found in static config file. Looking in dynamic config file.
  5. Client port found: 2182. Client address: 0.0.0.0. Client SSL: false.
  6. Mode: follower

发现扩容后的实例角色为follower,符合预期,连上客户端查看配置:

  1. [root@localhost zookeeper2]# ./bin/zkCli.sh -server 127.0.0.1:2182
  2. ...
  3. [zk: 127.0.0.1:2182(CONNECTED) 0] config
  4. server.0=127.0.0.1:2888:3888:participant
  5. server.1=127.0.0.1:2889:3889:participant;0.0.0.0:2182
  6. version=900000005

三、缩容一个zk

现在我们将第二步扩容的zk,拿掉,连上2181的客户端,并执行命令

  1. [zk: localhost:2181(CONNECTED) 7] reconfig -remove 1
  2. Committed new configuration:
  3. server.0=127.0.0.1:2888:3888:participant
  4. version=90000000d
  1. [root@localhost zookeeper1]# cat conf/zoo.cfg.dynamic.90000000d
  2. server.0=127.0.0.1:2888:3888:participant

查看下2182的配置和角色

  1. [root@localhost zookeeper2]# cat conf/zoo.cfg
  2. initLimit=10
  3. syncLimit=5
  4. skipACL=yes
  5. 4lw.commands.whitelist=*
  6. tickTime=2000
  7. dataDir=/home/zookeeper/zookeeper2/data
  8. dataLogDir=/home/zookeeper/zookeeper2/log
  9. reconfigEnabled=true
  10. admin.enableServer=false
  11. standaloneEnabled=false
  12. dynamicConfigFile=/home/zookeeper/zookeeper2/conf/zoo.cfg.dynamic.90000000d
  13. [root@localhost zookeeper2]# cat conf/zoo.cfg.dynamic.90000000d
  14. server.0=127.0.0.1:2888:3888:participant
  1. [root@localhost zookeeper2]# echo stat|nc 127.0.0.1 2182
  2. Zookeeper version: 3.8.1-74db005175a4ec545697012f9069cb9dcc8cdda7, built on 2023-01-25 16:31 UTC
  3. Clients:
  4. /127.0.0.1:37140[0](queued=0,recved=1,sent=0)
  5. Latency min/avg/max: 0/0.7989/30
  6. Received: 176
  7. Sent: 175
  8. Connections: 1
  9. Outstanding: 0
  10. Zxid: 0x90000000d
  11. Mode: follower
  12. Node count: 6

发现还是follower,只不过已经脱离了集群;

四、重新配置集群的节点

可通过如下命令进行整个zk集群的配置更新

reconfig -members serverID=host:port1:port2;port3[,...]

其它的一些注意事项和命令说明可以翻阅官网的reconfig章节~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/151293
推荐阅读
相关标签
  

闽ICP备14008679号