Apache ZooKeeper 单机、集群部署文档


简介:

Apache ZooKeeper 是一个分布式应用的高性能协调服务,功能包括:配置维护、统一命名、状态同步、集群管理、仲裁选举等。

下载地址:http://apache.fayea.com/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz

官方文档:http://zookeeper.apache.org/doc/r3.4.10/zookeeperStarted.html

一、安装 ZooKeeper

shell > java -version
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)

# JAVA 是必不可少的

shell > cd /usr/local/src
shell > wget http://apache.fayea.com/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz

shell > tar zxf zookeeper-3.4.10.tar.gz -C ../

shell > chown -R root.root /usr/local/zookeeper-3.4.10

二、配置、启动、客户端、停止 ZooKeeper

1、生成配置文件

shell > cd /usr/local/zookeeper-3.4.10

shell > cp conf/zoo_sample.cfg conf/zoo.cfg

2、启动 ZooKeeper

shell > sh bin/zkServer.sh start

# 查看进程ID
shell > jps
11174 QuorumPeerMain

# 端口监听状态
shell > netstat -lnpt | grep 2181
tcp        0      0 :::2181                     :::*                        LISTEN      11174/java

# ZooKeeper 启动模式 standalone
shell > sh bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: standalone

3、客户端连接

shell > sh bin/zkCli.sh -server localhost:2181
# 打印根目录
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
# 层级目录
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[quota]
# 显示帮助信息
[zk: localhost:2181(CONNECTED) 2] ?
ZooKeeper -server host:port cmd args
    stat path [watch]
    set path data [version]
    ls path [watch]
    delquota [-n|-b] path
    ls2 path [watch]
    setAcl path acl
    setquota -n|-b val path
    history 
    redo cmdno
    printwatches on|off
    delete path [version]
    sync path
    listquota path
    rmr path
    get path [watch]
    create [-s] [-e] path data acl
    addauth scheme auth
    quit 
    getAcl path
    close 
    connect host:port

# ZooKeeper 维护着一个类似文件系统的内存结构
# 连接时不指定 -server 默认连接本机 2181 端口
# 当输入 ZooKeeper 无法识别的指令时,打印帮助信息

4、停止 ZooKeeper

shell > sh bin/zkServer.sh stop

三、ZooKeeper 集群部署

192.168.1.27 datanode01.hadoop
192.168.1.28 datanode02.hadoop
192.168.1.29 datanode03.hadoop

# 部署 ZooKeeper 集群,建议 3 台及以上奇数主机 ( 涉及到仲裁问题 )

# JAVA、下载、解压、修改属主、组

1、修改配置文件

shell > vim conf/zoo.cfg
# 心跳间隔 毫秒
tickTime=2000
# 初始化时,允许的超时心跳间隔次数
initLimit=10
# Leader 与 Follower 同步数据允许的超时心跳间隔次数
syncLimit=5
# 监听端口
clientPort=2181
# 数据目录
dataDir=/data/zookeeper/data
# 日志目录
dataLogDir=/data/zookeeper/logs
# ZooKeeper Server 地址,通信端口、选举端口
server.1=192.168.1.27:2888:3888
server.2=192.168.1.28:2888:3888
server.3=192.168.1.29:2888:3888

# 每台都要有哦~

2、创建目录

shell > mkdir -p /data/zookeeper/{data,logs}

# 每台都要有哦~

3、生成节点标识文件

shell > echo 1 > /data/zookeeper/data/myid

shell > echo 2 > /data/zookeeper/data/myid

shell > echo 3 > /data/zookeeper/data/myid

# 分别在三台服务器上执行,需要跟配置文件中的 server.1\2\3 对应

4、启动 ZooKeeper

shell > ansible datanode -m shell -a '/usr/local/zookeeper-3.4.10/bin/zkServer.sh start'
datanode02.hadoop | SUCCESS | rc=0 >>
Starting zookeeper ... STARTEDZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg

datanode03.hadoop | SUCCESS | rc=0 >>
Starting zookeeper ... STARTEDZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg

datanode01.hadoop | SUCCESS | rc=0 >>
Starting zookeeper ... STARTEDZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg

# 全部成功启动

shell > ansible datanode -m shell -a '/usr/local/zookeeper-3.4.10/bin/zkServer.sh status'
datanode03.hadoop | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader

datanode02.hadoop | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower

datanode01.hadoop | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower

# 查看状态,datanode03.hadoop 为 leader ,其余两台为 follower

5、验证选举

shell > sh bin/zkServer.sh stop

# 关闭 datanode03.hadoop 服务器上的 ZooKeeper,原 leader

shell > ansible datanode -m shell -a '/usr/local/zookeeper-3.4.10/bin/zkServer.sh status'
datanode03.hadoop | FAILED | rc=1 >>
Error contacting service. It is probably not running.ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg

datanode02.hadoop | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader

datanode01.hadoop | SUCCESS | rc=0 >>
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower

# 再次查看状态,datanode02.hadoop 升级为 leader,datanode01.hadoop 仍为 follower

# 注意:三台 ZooKeeper Server 组成的集群,当两台故障时,整个集群失败 ( 剩余的一台无法继续提供服务 )

6、客户端连接

shell > sh bin/zkCli.sh -server 192.168.1.27:2181,192.168.1.28:2181,192.168.1.29:2181

# 注意:
# 客户端连接集群,只写一个地址时,当这台 Server 宕机,则客户端连接失败
# 同时写多个地址( 全写 )时,除集群失败外,不影响客户端连接
# 写多个地址时,以 , 分割,, 两边不能有空格

shell > sh bin/zkCli.sh -server 192.168.1.27:2181
[zk: 192.168.1.27:2181(CONNECTED) 0] ls /
[zookeeper]

# 客户端只连接 1.27,显示只有一个默认的 znode

shell > sh bin/zkCli.sh -server 192.168.1.28:2181
[zk: 192.168.1.28:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: 192.168.1.28:2181(CONNECTED) 1] create /zk 'mydata'
Created /zk
[zk: 192.168.1.28:2181(CONNECTED) 2] ls /
[zk, zookeeper]
[zk: 192.168.1.28:2181(CONNECTED) 3] get /zk
mydata
cZxid = 0x400000005
ctime = Thu May 04 18:50:06 CST 2017
mZxid = 0x400000005
mtime = Thu May 04 18:50:06 CST 2017
pZxid = 0x400000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

# 新客户端连接 1.28,创建一个 znode

shell > 
[zk: 192.168.1.27:2181(CONNECTED) 1] ls /
[zk, zookeeper]
[zk: 192.168.1.27:2181(CONNECTED) 2] get /zk
mydata
cZxid = 0x400000005
ctime = Thu May 04 18:50:06 CST 2017
mZxid = 0x400000005
mtime = Thu May 04 18:50:06 CST 2017
pZxid = 0x400000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

# 刚才连接的 1.27,可以显示、获取这个新建的 znode

shell >
[zk: 192.168.1.27:2181(CONNECTED) 3] set /zk 'share'
cZxid = 0x400000005
ctime = Thu May 04 18:50:06 CST 2017
mZxid = 0x400000006
mtime = Thu May 04 18:57:48 CST 2017
pZxid = 0x400000005
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

# 给这个 znode 重新设置一个值

shell > 
[zk: 192.168.1.28:2181(CONNECTED) 4] get /zk       
share
cZxid = 0x400000005
ctime = Thu May 04 18:50:06 CST 2017
mZxid = 0x400000006
mtime = Thu May 04 18:57:48 CST 2017
pZxid = 0x400000005
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

# 连接 1.28 的客户端,也获取到了更新后的值

shell > 
[zk: 192.168.1.28:2181(CONNECTED) 4] delete /zk
[zk: 192.168.1.28:2181(CONNECTED) 5] ls /

# 删除创建的 znode

# 实验表明:
# ZooKeeper 是任意读写的!

posted @ 2017-05-04 19:12  WangXiaoQiang  阅读(756)  评论(0编辑  收藏  举报