下载
下载地址:https://downloads.apache.org/zookeeper/
下载时,文件名带bin的是已经编译安装好的,请下载带有bin的压缩文件。
安装
将压缩包上传到服务器,然后解压缩。
tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz -C /opt/
进入解压后的目录,复制并重命名一份简单的配置文件
cd /opt/zookeeper-3.8.0/conf
cp zoo_sample.cfg zoo.cfg
修改配置文件内容
[root@hadoop101 conf]# cat 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/zookeeper/data
dataLogDir=/data/zookeeper/logs
# the port at which the clients will connect
clientPort=10086
# 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.
#
# https://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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#servers
server.1=192.168.163.101:10087:10088
server.2=192.168.163.102:10087:10088
server.3=192.168.163.103:10087:10088
#open commands
4lw.commands.whitelist=*
主要配置解释
配置项 | 值与释义 |
---|---|
dataDir | ZK数据存放位置,可以任意指定 |
dataLogDir | ZK数据操作的日志,可以任意指定。这个不是ZK的启动日志,是对数据操作的记录日志 |
clientPort | 连接ZK所使用的端口 |
server | 每一个server代表一个ZK,后面的数字需要是myid的值,等号后面填写IP和两个随意指定的端口,这两个端口用于ZK集群内部通信,外部无法连接。 |
4lw.commands.whitelist | 白名单的命令,如果不限制外部命令则填* |
这里我以安装三节点为例,如果只安装单节点的ZK,则配置文件末尾的#servers
段无需配置。
配置文件最后的4lw.commands.whitelist
是为了开放命令权限,方便外部访问和执行命令,比如从外部检测zk的版本号等,如果不需要则可以不配置。
最后在dataDir
路径下创建一个myid文件,如果不做集群配置,这一步可以省略。
touch /data/zookeeper/data/myid
echo '1' > /data/zookeeper/data/myid
另外两台主机如法炮制,上传压缩包——>解压——>修改配置文件——>创建myid文件,或者直接从第一台主机上把所有东西都scp过去,无论怎么做都行,但一定记得不同主机的myid配置是不同的,101的myid一定是1,102的一定是2,这不是因为101是在第一个,而是因为配置文件中101主机是server.1
,如果配置文件改成server.99
,则101主机的myid要改成99。
最后在各个主机上,以任意顺序执行启动命令即可
/opt/zookeeper-3.8.0/bin/zkServer.sh start
远程查看节点状态
使用下面的nc
命令查看ZK节点状态,如果没有配置4lw.commands.whitelist
,则这一步会报错。
echo stat | nc 192.168.163.101 10086
如果没有nc命令,则下载yum install nc -y
如上图,该命令会展示ZK的版本号与节点的运行mode,可以看到这是一个follower节点。
使用
连接ZK
/opt/zookeeper-3.8.0/bin/zkCli.sh -server 192.168.163.101:10086
查看节点
[zk: 192.168.163.101:10086(CONNECTED) 0] ls /
[zookeeper]
创建节点并赋值
[zk: 192.168.163.101:10086(CONNECTED) 1] create /ovo
Created /ovo
[zk: 192.168.163.101:10086(CONNECTED) 3] create /ovo/north "I 最讨厌起网名了,总是被占用。"
Created /ovo/north
获取节点的值
[zk: 192.168.163.101:10086(CONNECTED) 9] get /ovo/north
I 最讨厌起网名了,总是被占用。
修改节点的值(也可用于赋值)
[zk: 192.168.163.101:10086(CONNECTED) 10] set /ovo/north "New Player"
[zk: 192.168.163.101:10086(CONNECTED) 11] get /ovo/north
New Player
删除节点
[zk: 192.168.163.101:10086(CONNECTED) 12] delete /ovo/north
无法删除非空的节点
[zk: 192.168.163.101:10086(CONNECTED) 13] create /ovo/north
Created /ovo/north
[zk: 192.168.163.101:10086(CONNECTED) 14] delete /ovo
Node not empty: /ovo