#tar -zxvf /opt/zookeeper-3.4.5.tar.gz -C /usr/local
#mkdir /data/zookeeper-3.4.5
#chmod -R 777 /data/zookeeper-3.4.5
#echo "1">/data/zookeeper-3.4.5/myid
#cat /data/zookeeper-3.4.5/myid
显示1
#cd /usr/local/zookeeper-3.4.5/
#cd /usr/local/zookeeper-3.4.5/conf/
cp zoo_sample.cfg zoo.cfg
vi 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=/data/zookeeper-3.4.5 # the port at which the clients will connect clientPort=2181 # # 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=1 server.1=192.168.1.250:2888:3888 server.2=192.168.1.251:2888:3888 server.3=192.168.1.252:2888:3888
initLimit
ZooKeeper集群模式下包含多个zk进程,其中一个进程为leader,余下的进程为follower。
当follower最初与leader建立连接时,它们之间会传输相当多的数据,尤其是follower的数据落后leader很多。initLimit配置follower与leader之间建立连接后进行同步的最长时间。
syncLimit
配置follower和leader之间发送消息,请求和应答的最大时间长度。
tickTime
tickTime则是上述两个超时配置的基本单位,例如对于initLimit,其配置值为5,说明其超时时间为 2000ms * 5 = 10秒。
server.id=host:port1:port2
其中id为一个数字,表示zk进程的id,这个id也是dataDir目录下myid文件的内容。
host是该zk进程所在的IP地址,port1表示follower和leader交换消息所使用的端口,port2表示选举leader所使用的端口。
dataDir
其配置的含义跟单机模式下的含义类似,不同的是集群模式下还有一个myid文件。myid文件的内容只有一行,且内容只能为1 - 255之间的数字,这个数字亦即上面介绍server.id中的id,表示zk进程的id。
#scp -r /usr/local/zookeeper-3.4.5/ root@node2.jacky.com:/usr/local
#scp -r /usr/local/zookeeper-3.4.5/ root@node3.jacky.com:/usr/local
#scp -r /data/zookeeper-3.4.5/ root@node2.jacky.com:/data
#scp -r /data/zookeeper-3.4.5/ root@node3.jacky.com:/data
然后ssh进入node2.jacky.com的主机,把/data/zookeeper-3.4.5/myid里面的内容改为2
进入node3.jacky.com的主机,把/data/zookeeper-3.4.5/myid里面的内容改为3
设置开机启动
#cd /etc/init.d
#vi zookeeper
#chmod a+x zookeeper
#chkconfig --add zookeeper
#scp -r /etc/init.d/zookeeper root@node2.jacky.com:/etc/init.d
# scp -r /etc/init.d/zookeeper root@node3.jacky.com:/etc/init.d
分别到 node2.jacky.com和node3.jacky.com的机器上执行:#chkconfig --add zookeeper
在3台机器上分别执行:#service zookeeper start
查看zookeeper启动进程的信息
# ps -ef|grep zookeeper
使用以下命令测试连接到一个zookeeper集群:
#/usr/local/zookeeper-3.4.5/bin/zkCli.sh -server 192.168.1.250:2181,192.168.1.251:2181,192.168.1.252:2181
输出以下信息:
Connecting to 192.168.1.250:2181,192.168.1.251:2181,192.168.1.252:2181 2021-10-12 01:39:25,242 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT 2021-10-12 01:39:25,245 [myid:] - INFO [main:Environment@100] - Client environment:host.name=node1.jacky.com 2021-10-12 01:39:25,245 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_171 2021-10-12 01:39:25,245 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2021-10-12 01:39:25,245 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk1.8.0_171/jre 2021-10-12 01:39:25,246 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.5/bin/../build/classes:/usr/local/zookeeper-3.4.5/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/usr/local/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/usr/local/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/usr/local/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.5/bin/../conf:.:/usr/local/java/jdk1.8.0_171/lib:/usr/local/java/jdk1.8.0_171/jre/lib 2021-10-12 01:39:25,246 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2021-10-12 01:39:25,246 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2021-10-12 01:39:25,246 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2021-10-12 01:39:25,246 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2021-10-12 01:39:25,247 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2021-10-12 01:39:25,247 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-1062.el7.x86_64 2021-10-12 01:39:25,247 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2021-10-12 01:39:25,247 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2021-10-12 01:39:25,247 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/etc/rc.d/init.d 2021-10-12 01:39:25,248 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.250:2181,192.168.1.251:2181,192.168.1.252:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a 2021-10-12 01:39:25,312 [myid:] - INFO [main-SendThread(node3.jacky.com:2181):ClientCnxn$SendThread@966] - Opening socket connection to server node3.jacky.com/192.168.1.252:2181. Will not attempt to authenticate using SASL (unknown error) Welcome to ZooKeeper! JLine support is enabled 2021-10-12 01:39:25,462 [myid:] - INFO [main-SendThread(node3.jacky.com:2181):ClientCnxn$SendThread@849] - Socket connection established to node3.jacky.com/192.168.1.252:2181, initiating session [zk: 192.168.1.250:2181,192.168.1.251:2181,192.168.1.252:2181(CONNECTING) 0] 2021-10-12 01:39:25,553 [myid:] - INFO [main-SendThread(node3.jacky.com:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server node3.jacky.com/192.168.1.252:2181, sessionid = 0x37c706be1d20000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: 192.168.1.250:2181,192.168.1.251:2181,192.168.1.252:2181(CONNECTED) 0]
说明zookeeper集群配置成功了