《Linux 环境下,ZooKeeper 安装及运行(单机模式)》一文中总结了单机模式的 zookeeper 安装及运行方法,本文简单总结下如何搭建 zookeeper 集群。
笔者准备的 ZK 版本是 apache-zookeeper-3.6.2-bin.tar.gz,其他版本可以至官方网站 https://zookeeper.apache.org/releases.html 下载。
节点 | IP |
---|---|
node1 | 10.201.42.13 |
node2 | 10.201.42.14 |
node3 | 10.201.42.26 |
安装方法见 《Linux yum 安装 JDK 的方法》或 《Linux 手动安装 JDK 的方法》。
分别在三台服务器上执行如下操作,搭建并配置 zookeeper 集群。
分别将 apache-zookeeper-3.6.2-bin.tar.gz 文件上传到三台服务器的 /home/cpctest 目录下,然后解压:
[cpctest@vm-10-201-42-13 ~]$ pwd
/home/cpctest
[cpctest@vm-10-201-42-13 ~]$ tar zxvf apache-zookeeper-3.6.2-bin.tar.gz
[cpctest@vm-10-201-42-13 ~]$ pwd
/home/cpctest
[cpctest@vm-10-201-42-13 ~]$ mkdir zklogs
[cpctest@vm-10-201-42-13 ~]$ ls
apache-zookeeper-3.6.2-bin apache-zookeeper-3.6.2-bin.tar.gz zklogs
将 zookeeper 的 /conf 路径下的配置文件 zoo_sample.cfg 拷贝为 zoo.cfg,添加如下内容:
# 集群服务器配置,数字1/2/3需要与 myid 文件一致。右边两个端口,2888 表示数据同步和通信端口;3888 表示选举端口
server.1=10.201.42.13:2888:3888
server.2=10.201.42.14:2888:3888
server.3=10.201.42.26:2888:3888
修改 zoo.cfg 中的 zookeeper 数据文件存放目录:
dataDir=/home/cpctest/zklogs
修改完后的 zoo.cfg 文件为
[cpctest@vm-10-201-42-13 conf]$ pwd
/home/cpctest/apache-zookeeper-3.6.2-bin/conf
[cpctest@vm-10-201-42-13 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=/home/cpctest/zklogs
# 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
server.1=10.201.42.13:2888:3888
server.2=10.201.42.14:2888:3888
server.3=10.201.42.26:2888:3888
上述文件中的各个属性含义详见 《Linux 环境下,ZooKeeper 安装及运行(单机模式)》。
[cpctest@vm-10-201-42-13 zklogs]$ pwd
/home/cpctest/zklogs
[cpctest@vm-10-201-42-13 zklogs]$ echo 1 > myid
vi /home/cpctest/apache-zookeeper-3.6.2-bin/bin/zkEnv.sh,找到 ZOO_LOG_DIR 和 ZOO_LOG4J_PROP 位置
if [ "x${ZOO_LOG_DIR}" = "x" ]
then
# 配置zookeeper日志输出存放路径
ZOO_LOG_DIR="/home/cpctest/zklogs/applog/zookeeper"
fi
if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
# 配置日志输出级别,这里把几个级别一并配上
ZOO_LOG4J_PROP="INFO,CONSOLE,POLLINGFILE,TRACEFILE"
fi
# Define some default values that can be overridden by system properties
zookeeper.root.logger=INFO, CONSOLE, POLLINGFILE, TRACEFILE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=ERROR
zookeeper.log.maxfilesize=256MB
zookeeper.log.maxbackupindex=20
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log
log4j.rootLogger=${zookeeper.root.logger}
进入 /bin 路径下,执行如下命令,启动 zookeeper 服务:
[cpctest@vm-10-201-42-13 bin]$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/cpctest/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[cpctest@vm-10-201-42-13 bin]$
其他 zookeeper 操作命令见 《Linux 环境下,ZooKeeper 安装及运行(单机模式)》。
然后用 status 命令检查下状态,如果出现 Mode:leader 或者 Mode:follower 表示搭建成功。
[cpctest@vm-10-201-42-13 bin]$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/cpctest/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[cpctest@vm-10-201-42-14 bin]$ ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/cpctest/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[cpctest@vm-10-201-42-26 bin]$ ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/cpctest/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
在命令行中输入:./zkCli.sh -server 127.0.0.1:2181,即可连接到本机 ZooKeeper 服务器。其他自动实现同步,客户端只需要和一台保持连接即可。出现如下语句表示链接成功:
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]