centos7.2
zookeeper下载与安装
下载zookeeper传送门
基本介绍
1 zookeeper是一个类似hdfs的树形文件结构,zookeeper可以用来保证数据在(zk)集群之间的数据的事务性一致、
2 zookeeper有watch事件,是一次性触发的,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher
3 zookeeper有三个角色:Learner,Follower,Observer
4 zookeeper应用场景:
统一命名服务(Name Service)
配置管理(Configuration Management)
集群管理(Group Membership)
共享锁(Locks)
队列管理
目标
搭建Zookeeper伪集群
搭建集群说明
Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。
■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境;
■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例;
■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble)
Zookeeper通过复制来实现高可用性,只要集合体中半数以上的机器处于可用状态,它就能够保证服务继续。为什么一定要超过半数呢?这跟Zookeeper的复制策略有关:zookeeper确保对znode 树的每一个修改都会被复制到集合体中超过半数的机器上。
此次搭建说明:
1. 搭建三个节点的Zookeeper集群
2. 下面的配置三个节点配置都是需要改的,后面会给出实例
3. 在搭建伪集群的时候是在同一个服务器上创建三个节点的实例,这时候不能让zookeeper的cliPore端口相同
4. 在启动一个的时候不要去看zookeeper节点是否启动成功,因为zookeeper集群会检测其他的节点是否启动成功(zookeeper策略),如果检测不到就会报错,我们直接全部启动,最后在看启动的状态
5. 启动的时候如果是创建真正的集群,需要确定节点之间端口对外是否可访问,(可以增加端口开放,或者关闭防火墙/白名单)
下载与安装
下载
wget "http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz"
解压
[root@kunzai installPath]# tar -zxvf zookeeper-3.4.12.tar.gz
移动复制创建集群目录
[root@kunzai java]# pwd
/usr/local/java
[root@kunzai java]# mkdir zookeeperColony
[root@kunzai java]# cd zookeeperColony/
[root@kunzai zookeeperColony]# pwd
/usr/local/java/zookeeperColony
[root@kunzai zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-One
[root@kunzai zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-Two
[root@kunzai zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-There
修改基本的配置
[root@kunzai zookeeper-3.4.12-One]# vim /etc/profile
追加下面的数据
export ZOOKEEPER_HOME=/usr/local/java/zookeeperColony/zookeeper-3.4.12-One
export PATH=.:${PATH}:${MAVEN_HOME}/bin:$ZOOKEEPER_HOME/bin
刷新:
source /etc/profile
[root@kunzai conf]# pwd
/usr/local/java/zookeeperColony/zookeeper-3.4.12-One/conf
修改配置文件名称
到zookeeper下修改配置文件
mv zoo_sample.cfg zoo.cfg
修改conf: vi zoo.cfg 修改两处
(1)dataDir=/usr/local/zookeeper/data
(2)最后面添加 (这个是因为伪集群,所以保证端口不一样)
server.0=ip:2887:3887
server.1=ip:2888:3888
server.2=ip:2889:3889
服务器标识配置:
[root@kunzai zookeeper-3.4.12-One]# pwd
/usr/local/java/zookeeperColony/zookeeper-3.4.12-One
创建文件夹:mkdir data
创建文件(在上面创建的data目录下)myid并填写内容为0:vi myid (内容为服务器标识 : 0)
# The number of milliseconds of each tick
#基本事件单元,以毫秒为单位。这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,
# 也就是每隔 tickTime时间就会发送一个心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#这个配置项是用来配置 Zookeeper 接受客户端初始化连接时最长能忍受多少个心跳时间间隔数,
# 当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,
# 那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,
# 最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-One/data
# the port at which the clients will connect
#这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
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=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
节点2:
# The number of milliseconds of each tick
#基本事件单元,以毫秒为单位。这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,
# 也就是每隔 tickTime时间就会发送一个心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#这个配置项是用来配置 Zookeeper 接受客户端初始化连接时最长能忍受多少个心跳时间间隔数,
# 当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,
# 那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,
# 最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-Two/data
# the port at which the clients will connect
#这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
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=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
节点3:
# The number of milliseconds of each tick
#基本事件单元,以毫秒为单位。这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,
# 也就是每隔 tickTime时间就会发送一个心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#这个配置项是用来配置 Zookeeper 接受客户端初始化连接时最长能忍受多少个心跳时间间隔数,
# 当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,
# 那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,
# 最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-There/data
# the port at which the clients will connect
#这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2183
# 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=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
zkCli.sh 进入zookeeper客户端
根据提示命令进行操作:
查找:ls / ls /zookeeper
创建并赋值:create /bhz hadoop
获取:get /bhz
设值:set /bhz baihezhuo
创建: create /bhz/test1 value1
叠删除: rmr /bhz/test1
可以看到zookeeper集群的数据一致性
创建节点有俩种类型:短暂(ephemeral) 持久(persistent)
tickTime: 基本事件单元,以毫秒为单位。这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,
也就是每隔 tickTime时间就会发送一个心跳。
dataDir: 存储内存中数据库快照的位置,顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort: 这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
initLimit: 这个配置项是用来配置 Zookeeper 接受客户端初始化连接时最长能忍受多少个心跳时间间隔数,
当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,
那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
syncLimit: 这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,
最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10 秒
server.A = B:C:D :
A表示这个是第几号服务器,
B 是这个服务器的 ip 地址;
C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;
D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader