1.下载
http://zookeeper.apache.org/releases.html
当前stable版是zookeeper-3.4.10
2.解压
$ tar –zxvf zookeeper-3.4.10.tar.gz
3.进入到 /usr/local/java/zookeeper/conf 目录中:
cd zookeeper-3.4.10/conf/
4.复制 zoo_sample.cfg 文件的并命名为为 zoo.cfg:
$ cp zoo_sample.cfg zoo.cfg
5.用 vim 打开 zoo.cfg 文件并修改其内容为如下:
# The number of milliseconds of each tick
# zookeeper 定义的基准时间间隔,单位:毫秒
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
# 数据文件夹
dataDir=/usr/local/java/zookeeper-3.4.10/data
# 日志文件夹
dataLogDir=/usr/local/java/zookeeper-3.4.10/logs
# the port at which the clients will connect
# 客户端访问 zookeeper 的端口号
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# zookeeper集群配置
#server.1=192.168.2.2:2888:38888
# 2888端口号为服务之间通讯的端口
# 3888端口号是zookeeper与其他应用程序通信的端口
6.用 vim 打开 /etc/ 目录下的配置文件 profile:
vim /etc/profile
并在其尾部追加如下内容:
#idea - zookeeper-3.4.9 config start - 2017-12-09
export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.4.9/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
#idea - zookeeper-3.4.9 config end - 2017-12-09
使 /etc/ 目录下的 profile 文件即可生效:
$ source /etc/profile
7.启动 zookeeper 服务:
$ zkServer.sh start
如打印如下信息则表明启动成功:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
8.查询 zookeeper 状态:
$ zkServer.sh status
# 或者
$ ps -ef|grep zookeeper
9、关闭 zookeeper 服务:
$ zkServer.sh stop
如打印如下信息则表明成功关闭:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
10、重启 zookeeper 服务:
$ zkServer.sh restart
如打印如下信息则表明重启成功:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
11、集群配置:
12、设置zookeeper开机自启动
1)直接修改/etc/rc.d/rc.local文件
在/etc/rc.d/rc.local文件中需要输入两行,其中export JAVA_HOME=/usr/java/jdk1.8.0_112是必须要有的,否则开机启动不成功,大家根据自己JDK安装的位置自行更改。另一行/usr/local/zookeeper-3.4.5/bin/zkServer.sh start则是我们zookeeper的启动命令。配置好之后,重启虚拟机,会发现已经可以开机自启了。
[root@zookeeper ~]# vim /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
export JAVA_HOME=/usr/java/jdk1.8.0_112
/usr/local/zookeeper-3.4.5/bin/zkServer.sh start
2)把zookeeper做成服务
进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本
[root@zookeeper ~]# cd /etc/rc.d/init.d/
[root@zookeeper init.d]# pwd
/etc/rc.d/init.d
[root@zookeeper init.d]# touch zookeeper
给脚本添加执行权限
[root@zookeeper init.d]# chmod +x zookeeper
使用命令vim zookeeper进行编辑,在脚本中输入如下内容,其中同上面注意事项一样要添加export JAVA_HOME=//usr/java/jdk1.8.0_112这一行,否则无法正常启动。
[root@zookeeper init.d]# vim zookeeper
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=//usr/java/jdk1.8.0_112
case $1 in
start) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh start;;
stop) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh stop;;
status) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh status;;
restart) su /usr/local/zookeeper-3.4.5/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
使用service zookeeper start/stop命令来尝试启动关闭zookeeper,使用service zookeeper status查看zookeeper状态。
先来看启动及状态
[root@zookeeper init.d]# service zookeeper start
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper init.d]# service zookeeper status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: standalone
[root@zookeeper init.d]#
接着看关闭及状态
[root@zookeeper init.d]# service zookeeper stop
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper init.d]# service zookeeper status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.5/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@zookeeper init.d]#
添加到开机自启
[root@zookeeper init.d]# chkconfig --add zookeeper
[root@zookeeper init.d]# chkconfig zookeeper on
添加完之后,我们使用chkconfig --list来查看开机自启的服务中是否已经有我们的zookeeper了,如下所示,可以看到在最后一行便是我们的zookeeper服务了。
[root@zookeeper init.d]# chkconfig --list
zookeeper 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭