您的位置:首页 > 其它

zookeeper集群搭建

2017-05-22 12:28 375 查看
简介
Zookeeper 分布式服务框架是Apache Hadoop的一个子项目它主要是用来解决分布式应用中经常遇到的一些数据管理问题如统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等等。zookeeper本身可以单节点状态运行不过它的长处在于通过分布式zookeeper集群一个leader多个follower基于一定的策略来保证zookeeper集群的稳定性和可用性从而实现分布式应用的可靠性。
Zookeeper集群中主要有两个角色leader和follower。
领导者leader,用于负责进行投票的发起和决议,更新系统状态。
学习者learner,包括跟随者follower和观察者observer。
其中follower用于接受客户端请求并想客户端返回结果,在选主过程中参与投票。
而observer可以接受客户端连接,将写请求转发给leader,但observer不参加投票过程,只同步leader的状态,observer的目的是为了扩展系统,提高读取速度。
Zookeeper集群个数一般设置为2n+1奇数个。这里用三节点为例搭建一个zookeeper集群。
一、安装Java
操作之前建议改下hostname
192.168.100.21 zookeeper-001
192.168.100.22 zookeeper-002
192.168.100.23 zookeeper-003
zookeeper依赖java环境首先安装java三台机器分别操作如下
# add-apt-repository ppa:webupd8team/java
# apt-get update
# apt-get install oracle-java8-installer
# java -version //检验Java版本
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
二、安装zookeeper
截止目前17年5月zookeeper最新稳定版本是3.4.10可以选择在官网的download连接上下载安装。因为这里Ubuntu自带系统源的版本是3.4.5我这里对版本号没有特殊要求方便起见我直接使用了系统源安装。
查看当前系统源版本号
root@zookeeper-001:~# aptitude show zookeeperd
Package: zookeeperd
New: yes
State: installed
Automatically installed: no
Version: 3.4.5+dfsg-1
Priority: optional
Section: universe/java
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
3台机器上分别安装
root@zookeeper-001:~# aptitude install zookeeperd
三、修改配置文件
安装完成后默认位于/etc/zookeeper目录下我的配置文件如下把所有zookeeper节点加到配置文件的server里即可。所有节点的配置相同。
root@zookeeper-001:~# cat /etc/zookeeper/conf/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/zkdata
# 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
server.1=zookeeper-001:2888:3888
server.2=zookeeper-002:2888:3888
server.3=zookeeper-003:2888:3888
#
# 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
配置参数说明
tickTime这个时间是作为zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是说每个tickTime时间就会发送一个心跳。
initLimit这个配置项是用来配置zookeeper接受客户端这里所说的客户端不是用户连接zookeeper服务器的客户端,而是zookeeper服务器集群中连接到leader的follower 服务器初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过10个心跳的时间也就是tickTime长度后 zookeeper服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20秒。
syncLimit这个配置项标识leader与follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒。
dataDir:顾名思义就是zookeeper保存数据的目录,默认情况下zookeeper将写数据的日志文件也保存在这个目录里
clientPort这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口接受客户端的访问请求
server.A=B:C:D中的A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址C第一个端口用来集群成员的信息交换,表示这个服务器与集群中的leader服务器交换信息的端口D是在leader挂掉时专门用来进行选举leader所用的端口。

四、集群初始化操作
1、由于之前在zookeeper的配置文件里规定了data目录如果没有的话在启动服务之前需要手动创建
# mkdir -p /data/zkdata/
# chown -R zookeeper. /data/zkdata/
2、创建serverID

zookeeper集群模式下需要配置myid文件,这个文件需要放在dataDir目录下。这个文件里面有一个数据就是A的值该A就是zoo.cfg文件中server.A=B:C:D中的A,在zoo.cfg文件中配置的dataDir路径中创建myid文件。
在分别在zookeeper-001、002、003上执行
# echo 1 > /data/zkdata/myid
# echo 2 > /data/zkdata/myid
# echo 3 > /data/zkdata/myid
以上都配置完成之后分别在三个节点上启动zookeeper服务
# service zookeeper start
五、zookeeper集群状态检测
1、查看当前集群状态可以使用nc命令mode行显示当前是leader还是follower
# echo  state | nc 127.0.0.1 2181



2、也可以用自带的zkCli.sh连接到zookeeper文件系统

# bin/zkCli.sh -server 127.0.0.1:2181
3、这里推荐一个python的zookeeper命令行工具"zk-shell",pip安装即可
# pip install zk-shell
使用帮助如下
usage: zk-shell [-h] [--connect-timeout CONNECT_TIMEOUT] [--run-once RUN_ONCE]
[--run-from-stdin] [--sync-connect] [--readonly]
[--tunnel TUNNEL] [--version]
[hosts [hosts ...]]

positional arguments:
hosts                 ZK hosts to connect

optional arguments:
-h, --help            show this help message and exit
--connect-timeout CONNECT_TIMEOUT
ZK connect timeout
--run-once RUN_ONCE   Run a command non-interactively and exit
--run-from-stdin      Read cmds from stdin, run them and exit
--sync-connect        Connect syncronously.
--readonly            Enable readonly.
--tunnel TUNNEL       Create a ssh tunnel via this host
--version             Display version and exit.
示例,执行'ls /'命令:
root@zookeeper-001:~# zk-shell host 127.0.0.1 --run-once 'ls /'
admin
brokers
cluster
config
consumers
controller
controller_epoch
isr_change_notification
zookeeper
zookeeper官方文档https://zookeeper.apache.org/doc/trunk/
zookeeper下载连接http://mirrors.hust.edu.cn/apache/zookeeper/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu cluster zookeeper