mkdir zk
cd zk
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
tar -zxvf zookeeper-3.4.9.tar.gz
配置ZK环境变量
在/etc/profile 问价末尾追加
export ZOOKEEPER_HOME=/root/zk/zookeeper-3.4.9
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
执行如下命令可使配置生效
source /etc/profile
好了,后面我们的目的就是在一个云实例上启动一个有三个ZK节点的ZK集群
进入ZK的配置目录
[root@xinyeshuaiqi conf]# pwd
/root/zk/zookeeper-3.4.9/conf
[root@xinyeshuaiqi conf]# ls
configuration.xsl log4j.properties zk_my.cfg zoo_0.cfg zoo_1.cfg zoo_2.cfg zoo.cfg zookeeper.out zoo_sample.cfg
从zoo_sample.cfg
(zk配置文件模板)拷贝出三份配置文件 zoo_0.cfg
zoo_1.cfg
zoo_2.cfg
分别作为ZK集群3个节点的配置
zoo_0.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=/root/zk/zkData/data_0
dataLogDir=/root/zk/zkData/logs_0
# the port at which the clients will connect
# 客户端连接端口
clientPort=2180
# 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.
#
# 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.0=127.0.0.1:2287:3387
server.1=127.0.0.1:2288:3388
server.2=127.0.0.1:2289:3389
zoo_1.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=/root/zk/zkData/data_1
dataLogDir=/root/zk/zkData/logs_1
# 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.
#
# 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.0=127.0.0.1:2287:3387
server.1=127.0.0.1:2288:3388
server.2=127.0.0.1:2289:3389
zoo_2.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=/root/zk/zkData/data_2
dataLogDir=/root/zk/zkData/logs_2
# the port at which the clients will connect
clientPort=2182
# 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.
#
# 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.0=127.0.0.1:2287:3387
server.1=127.0.0.1:2288:3388
server.2=127.0.0.1:2289:3389
我们简单看下其中的一些配置项,大多按照默认配置即可
tickTime=2000
这个时间是作为 ZooKeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。单位为毫秒。
initLimit=10
syncLimit=5
这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是5*2000=10 秒。
dataDir=
ZooKeeper 保存数据的目录,用于存放内存数据库快照的文件夹,同时用于集群的myid文件也存在这个文件夹里。默认情况下,ZooKeeper 将写数据的日志文件也保存在这个目录里。
clientPort=2182
这个端口就是客户端连接 ZooKeeper 服务器的端口,ZooKeeper 会监听这个端口,接受客户端的访问请求。
maxClientCnxns=60
最大的客户端连接数,默认为60
server.0=127.0.0.1:2287:3387
我们看到配置集群地址时有2端口号
2287
是与leader通信端口号
3387
是选举leader通信端口号
一会我们需要创建下面路径对应的文件目录
dataDir=/root/zk/zkData/data_0
dataLogDir=/root/zk/zkData/logs_0
[root@xinyeshuaiqi zk]# cd zkData/
[root@xinyeshuaiqi zkData]# ls
data_0 data_1 data_2 logs_0 logs_1 logs_2
[root@xinyeshuaiqi zkData]# pwd
/root/zk/zkData
还需要创建myid文件,填入zk节点id值
echo 0 > /root/zk/zkData/data_0/myid
echo 1 > /root/zk/zkData/data_1/myid
echo 2 > /root/zk/zkData/data_2/myid
ok,可以开始启动zk集群啦
[root@xinyeshuaiqi conf]# zkServer.sh start zoo_0.cfg
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/bin/../conf/zoo_0.cfg
Starting zookeeper ... STARTED
[root@xinyeshuaiqi conf]# zkServer.sh start zoo_1.cfg
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/bin/../conf/zoo_1.cfg
Starting zookeeper ... STARTED
[root@xinyeshuaiqi conf]# zkServer.sh start zoo_2.cfg
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/bin/../conf/zoo_2.cfg
Starting zookeeper ... STARTED
看下集群状态
[root@xinyeshuaiqi conf]# jps
30657 QuorumPeerMain
30917 Jps
21880 jar
30716 QuorumPeerMain
20173 QuorumPeerMain
节点1是leader, 节点0和节点2是follower
我们把节点1停掉,节点2升级成leader
我们重新把节点1起起来,节点1以follower身份进入集群
ZK集群是CP的,所谓CP就是数据对外一致
我们在一个节点塞一个数据,看看其他节点能否读到就行
指定2181端口号连接节点1
[root@xinyeshuaiqi zk]# zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2021-12-03 14:37:05,574 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2021-12-03 14:37:05,577 [myid:] - INFO [main:Environment@100] - Client environment:host.name=xinyeshuaiqi
2021-12-03 14:37:05,577 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201
2021-12-03 14:37:05,581 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2021-12-03 14:37:05,581 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/root/jdk1.8.0_201/jre
2021-12-03 14:37:05,581 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/root/zk/zookeeper-3.4.9/bin/../build/classes:/root/zk/zookeeper-3.4.9/bin/../build/lib/*.jar:/root/zk/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/root/zk/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/root/zk/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/root/zk/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/root/zk/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/root/zk/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/root/zk/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/root/zk/zookeeper-3.4.9/bin/../conf:.:/root/jdk1.8.0_201/lib/dt.jar:/root/jdk1.8.0_201/lib/tools.jar
2021-12-03 14:37:05,581 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2021-12-03 14:37:05,581 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2021-12-03 14:37:05,582 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root/zk
2021-12-03 14:37:05,583 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@421faab1
2021-12-03 14:37:05,607 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2021-12-03 14:37:05,691 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2021-12-03 14:37:05,747 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x17d7e96777d0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
[zk: 127.0.0.1:2181(CONNECTED) 0] ls /
[wmx, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 1]
[zk: 127.0.0.1:2181(CONNECTED) 1]
[zk: 127.0.0.1:2181(CONNECTED) 1] create /xinye "shuaiqi"
Created /xinye
[zk: 127.0.0.1:2181(CONNECTED) 2]
连接其他节点
[root@xinyeshuaiqi zk]# zkCli.sh -server 127.0.0.1:2182
Connecting to 127.0.0.1:2182
2021-12-03 14:40:11,033 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2021-12-03 14:40:11,036 [myid:] - INFO [main:Environment@100] - Client environment:host.name=xinyeshuaiqi
2021-12-03 14:40:11,036 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/root/jdk1.8.0_201/jre
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/root/zk/zookeeper-3.4.9/bin/../build/classes:/root/zk/zookeeper-3.4.9/bin/../build/lib/*.jar:/root/zk/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/root/zk/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/root/zk/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/root/zk/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/root/zk/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/root/zk/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/root/zk/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/root/zk/zookeeper-3.4.9/bin/../conf:.:/root/jdk1.8.0_201/lib/dt.jar:/root/jdk1.8.0_201/lib/tools.jar
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2021-12-03 14:40:11,039 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root/zk
2021-12-03 14:40:11,041 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@421faab1
2021-12-03 14:40:11,066 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2021-12-03 14:40:11,174 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2182, initiating session
2021-12-03 14:40:11,201 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2182, sessionid = 0x27d7e9573c10000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2182(CONNECTED) 0] ls /
[wmx, zookeeper, xinye]
[zk: 127.0.0.1:2182(CONNECTED) 2] get /xinye
shuaiqi
cZxid = 0x400000002
ctime = Fri Dec 03 14:38:02 CST 2021
mZxid = 0x400000002
mtime = Fri Dec 03 14:38:02 CST 2021
pZxid = 0x400000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
可以读到,玩活。
为了方便我们以后快速起起来ZK集群
我们准备个简单的脚本 startZK.sh
#!/bin/sh
echo "start my zookeeper cluster"
for i in 0 1 2
do
zkServer.sh start /root/zk/zookeeper-3.4.9/conf/zoo_$i.cfg
done
[root@xinyeshuaiqi zk]# sh startZK.sh
start my zookeeper cluster
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/conf/zoo_0.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/conf/zoo_1.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /root/zk/zookeeper-3.4.9/conf/zoo_2.cfg
Starting zookeeper ... STARTED
[root@xinyeshuaiqi zk]# jps
2899 QuorumPeerMain
2933 Jps
2856 QuorumPeerMain
2827 QuorumPeerMain
转载请注明:汪明鑫的个人博客 » 10分钟搭建单机伪分布式ZK集群
说点什么
您将是第一位评论人!