1、官网下载zk

https://downloads.apache.org/zookeeper/zookeeper-3.7.0/

2、上传服务器并解压

我的路径:

/usr/local/src/zookeeper/zookeeper-3.7

3、 修改配置文件

进入 conf 文件夹下可以看到有个 zoo_sample.cfg 文件,这是自带的默认配置文件,需要修改一下

cd /usr/local/src/zookeeper/zookeeper-3.7/conf

将这个文件复制一份 命名为 zoo.cfg

cp zoo_sample.cfg zoo.cfg

编辑 zoo.cfg

vim 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/src/zookeeper/zookeeper-3.7/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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

注意上面指定了dataDir,现在这个文件夹还不存在,去手动创建

cd /usr/local/src/zookeeper/zookeeper-3.7
mkdir /data

4、启动zookeeper

在zookeeper目录下的bin文件夹中,使用以下命令启动zookeeper:

./zkServer.sh start

5、检查zookeeper启动情况

./zkServer.sh status

设置开机自启

我们可以将zookeeper作为一个服务,设置其开机自启,这样每次我们打开虚拟机就可以开启zookeeper,彻底解放双手!

1、进入 /etc/init.d 目录

cd /etc/init.d

2、创建文件zookeeper,并添加脚本

vim zookeeper

脚本内容:

#chkconfig: 2345 10 90
#description: service zookeeper
export   JAVA_HOME=/usr/local/src/java/jdk1.8.0_221
export   ZOO_LOG_DIR=/usr/local/src/zookeeper/zookeeper-3.7/logs
ZOOKEEPER_HOME=/usr/local/src/zookeeper/zookeeper-3.7
su    root    ${ZOOKEEPER_HOME}/bin/zkServer.sh      "$1"

注意上面的jdk路径和zookeeper路径 替换成自己安装的路径


3、授予可执行权限

chmod +x /etc/init.d/zookeeper

4、添加到服务中,并查看是否添加成功

chkconfig --add zookeeper
chkconfig --list