Zookeeper集群搭建及常用命令

集群搭建

参考如下博文

https://blog.csdn.net/chouya7686/article/details/100661853

https://www.cnblogs.com/zhangshiwen/p/12945170.html

https://www.cnblogs.com/ysocean/p/9860529.html

 

1、下载zookeeper安装包,并解压(这里就直接省略了)

TIPS:我这里下载并解压到了 /usr/local 下

 

2、检查是否安装了JDK,如果没有,需要先安装 JDK

 

3、在zookeeper目录下,新建 ./data 目录

 

4、将 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=/usr/local/zookeeper-3.4.14/data
# 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.0=192.168.10.131:2182:3888
server.1=192.168.10.132:2182:3888
server.2=192.168.10.133:2182:3888

 说明:

server.A=B:C:D

    A:其中 A 是一个数字,表示这个是服务器的编号;

    B:是这个服务器的 ip 地址;

    C:Leader选举的端口;

    D:Zookeeper服务器之间的通信端口。

  我们需要修改的第一个是 dataDir ,在指定的位置处创建好目录。

  第二个需要新增的是 server.A=B:C:D 配置,其中 A 对应下面我们即将介绍的myid 文件。B是集群的各个IP地址,C:D 是端口配置。

注意:

  D的端口之所以没有配成 2181 是因为 clientPort 占用了,如果配置成 2181,会报 Exception when following the leader java.io.EOFException 错误

5、创建 myid 文件

在上一步中配置的 dataDir 下新建一个 myid 文件,文件内容为上一步 server 配置的对应 A 数字,

 

6、配置环境变量

 并执行 source /etc/profile 使环境变量生效

 

7、修改 bin/zkServer.sh ,添加自定义的 log 生成路径

添加如下内容:(新增部分已标红)

if [ $ZK_HOME ] ; then
    ZOO_LOG_DIR="$ZK_HOME/log"
fi

if [ ! -w "$ZOO_LOG_DIR" ] ; then
    mkdir -p "$ZOO_LOG_DIR"
fi

_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"

 

8、分别启动各台服务器的zookeeper

可能出现的错误:

1、防火墙拦截了通信端口

2、zoo.cfg 的server.x 未与配置保持一致

 

启动成功后,可以通过

zkServer.sh status 查看集群状态:

 

 

 

 

 如果关闭了node3服务器上的 zookeeper,会发现剩下的两台会重新选举出一台 leader

 

zookeeper 客户端常用命令

1、启动服务端:

zkServer.sh start

 

2、启动客户端:

zkCli.sh [-server host:port]

 

3、常用命令:

// 查看节点所包含的节点
ls path

// 创建一个新节点
// -e 创建临时节点
// -s 创建有序节点
create [-e] [-s] path [data]

// 查看节点上的值
get path 

// 设置节点上的值
set path data

// 删除节点(只能删除没有下级节点的类型)
delete path

// 删除节点(递归删除下级节点)
rmr path
posted @ 2020-09-08 13:55  lkc9  阅读(764)  评论(0编辑  收藏  举报