zookeeper集群搭建和常用命令(笔记)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30276961/article/details/83108007

1.下载zookeeper安装包

linux的套路是,一般都会把安装文件放到/opt下
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz

2.配置zoo.cfg

解压,mv重命名一下
然后进到
/opt/zookeeper/conf
把zoo_sample.cfg重命名为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=/var/zookeeper
# 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.1=172.16.30.100:2888:3888
server.2=172.16.30.101:2888:3888
server.3=172.16.30.102:2888:3888

主要修改加粗部分,配置三个节点

3.启动节点

zookeeper是基于java开发的,所以运行zookeeper需要java环境。安装好jdk之后,在/etc/bashrc里加入java环境

export JAVA_HOME=/home/hadoop/env/jdk1.7.0_80
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

然后通过./bin/zkServer.sh start启动zookeeper节点。
启动之后,可以通过telnet 验证服务

telnet 172.16.30.100:2181
Trying 172.16.30.100...
Connected to 172.16.30.100.
Escape character is '^]'.

如上,说明zookeeper节点服务已经启动,键入stat,如果返回下面内容,说明集群已经正常工作

Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
Clients:
 /172.16.30.100:39281[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/0
Received: 2
Sent: 1
Connections: 1
Outstanding: 0
Zxid: 0x100000040
Mode: follower
Node count: 19
Connection closed by foreign host.

正常情况下,当集群里超过半数节点启动,zookeeper集群才会正常执行,允许外部连接。

4.常用命令

通过zkCli.sh连接zookeeper服务

./bin/zkCli.sh -timeout 5000 -server 172.16.30.101:2181
WatchedEvent state:SyncConnected type:None path:null
[zk: 172.16.30.101:2181(CONNECTED) 0]

如上所示,连接成功,等待执行命令。

键入h,回车

ZooKeeper -server host:port cmd args
        connect host:port
        get path [watch]
        ls path [watch]
        set path data [version]
        rmr path
        delquota [-n|-b] path
        quit 
        printwatches on|off
        create [-s] [-e] path data acl
        stat path [watch]
        close 
        ls2 path [watch]
        history 
        listquota path
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
可以看到一堆命令

connect …是连接到其他节点
get path 查看当前路径znode的信息(包含data)
ls path 查看当前路径的子znode
set path… 设置当前路径znode数据
rmr path… 递归删除当前路径节点及所有子节点
delquota… 删除策略,-n代表子节点数量,-b代表数据长度
quit 退出
create 创建节点,-s代表持久,-e代表临时
stat path… 查看某路径节点状态
close 关闭连接
ls2 path… 查看当前路径子节点和当前节点状态
history 列出历史执行命令清单
listquota… 查看当前路径节点策略情况
delete path删除当前路径节点
setquota 设置路径节点策略

猜你喜欢

转载自blog.csdn.net/sinat_30276961/article/details/83108007