集群规划
准备工作 集群安装Zookeeper 基本命令
服务器命令客户端命令
操作系统:Centos7 集群规划
准备三台虚拟机,共同组成 Zookeeper 集群
| 服务器 | 编号 | IP |
|---|---|---|
| node01 | 1 | 192.168.80.129 |
| node02 | 2 | 192.168.80.130 |
| node03 | 3 | 192.168.80.131 |
1、安装 JDK
由于 zookeeper 的运行需要 Java 运行环境,所以需要首先安装 JDK,参考之前的文章 https://blog.csdn.net/gongm24/article/details/115874005。
2、使用服务器名称进行通信
编辑 /etc/hosts 文件,在最后添加如下内容
192.168.80.129 node01 192.168.80.130 node02 192.168.80.131 node03
3、关闭防火墙
systemctl stop firewalld systemctl disable firewalld集群安装
1、下载
下载地址:https://zookeeper.apache.org/releases.html
本文选择 apache-zookeeper-3.6.3-bin.tar.gz 版本
2、通过 rz 命令上传至服务器
3、解压缩并移动至指令目录
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz mv apache-zookeeper-3.6.3-bin /usr/local/src
4、创建软连接,方便后续操作
ls -n /usr/local/src/apache-zookeeper-3.6.3-bin /usr/local/src/zookeeper
进入安装目录,查看文件结构
5、配置
conf 是存放配置文件的目录,其中 zoo_sample.cfg 是配置模板文件,将其复制并重命名为 zoo.cfg
cp conf/zoo_sample.cfg conf/zoo.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=/tmp/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 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true
配置项解析:
| 配置项 | 描述 |
|---|---|
| tickTime | 通信心跳时间,单位:毫秒,Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每隔 tickTime 时间就会发送一个心跳 |
| initLimit | 初始通信时限,集群中的 Follower 服务器与 Leader 服务器之间初始连接时能容忍的最多心跳数(tickTime 的数量) |
| syncLimit | 同步通信时限,集群中的 Follower 服务器与 Leader 服务器之间请求和应答之间能容忍的最多心跳数(tickTime 的数量) |
| dataDir | 数据文件目录,默认情况下,写数据的日志文件也保存在这个目录下 |
| clientPort | 客户端连接端口 |
修改 dataDir 为 /usr/local/src/zookeeper/data
除了以上几个配置项,集群信息也在这里配置,配置格式为
server.{服务器编号}={服务器地址}:{LF通信端口}:{选举端口}
按本文的规划在文件最后添加如下内容
server.1=node01:2888:3888 server.2=node02:2888:3888 server.3=node03:2888:3888
6、创建 myid 文件
进行 ${dataDir} 目录,创建 myid 文件,写入服务器编号,与上面的集群配置的服务器编号对应
echo 1 > myid
7、配置环境变量
编辑 /etc/profile 文件,在文件最后添加如下内容并保存
export ZK_HOME=/usr/local/src/zookeeper export PATH=$PATH:$ZK_HOME/bin
使用配置文件生效
source /etc/profile
8、配置另外两台机器
通过 scp 命令将 zookeeper 复制至另外两台机器
scp -r /usr/local/src/zookeeper node02:/usr/local/src scp -r /usr/local/src/zookeeper node03:/usr/local/src
修改两台机器的 myid 文件,分别对应自己的服务器编号,
node02 修改为 2node03 修改为 3
安装完成,可以启动服务了。
Zookeeper 基本命令 服务器命令启动服务
zkServer.sh start
停止服务
zkServer.sh stop
重启服务
zkServer.sh restart
查看服务状态
zkServer.sh status客户端命令
客户端连接
zkCli.sh
查看
ls /
创建节点
create /test Hello
-s 代表顺序节点, -e 代表临时节点
修改节点数据
set /test Hero
获取节点数据
get /test
查看节点状态
stat /test
删除节点
delete /test



