我使用centos7X64最小化安装
CentOS-7-x86_64-Minimal-1708
启动后状态
参考:zookeeper官方文档
1.先进官网选择个版本,(我当前的最新稳定版本是3.6.2)
2.下载并解压
代码语言:javascript复制我们放在/usr/local目录下
cd /usr/local
wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
3.配置zookeeper
3.1.先复制一份配置文件为主配置
代码语言:javascript复制cd /usr/local/apache-zookeeper-3.6.2-bin/conf
cp zoo_sample.cfg zoo.cfg
3.2.创建数据存储目录与日志目录
代码语言:javascript复制cd /usr/local/apache-zookeeper-3.6.2-bin
mkdir data
mkdir log
3.3.修改数据存储和日志目录
代码语言:javascript复制
dataLogDir
表签手动添加,放到dataDir下面
[root@192-168-50-191 apache-zookeeper-3.6.2-bin]# cd /usr/local/apache-zookeeper-3.6.2-bin/conf
[root@192-168-50-191 conf]# vim zoo.cfg
修改数据和日志存储位置
dataDir=/usr/local/apache-zookeeper-3.6.2-bin/data
dataLogDir=/usr/local/apache-zookeeper-3.6.2-bin/log
完整配置文件
# 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/apache-zookeeper-3.6.2-bin/data
dataLogDir=/usr/local/apache-zookeeper-3.6.2-bin/log
# 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
4.配置zookeeper环境变量
zookeeper依赖jvm环境
没装jdk
**的同学,请移步先安装jdk【[centos7 安装 jdk8 的正确姿势](https://cloud.tencent.com/developer/article/1696448)】的文章**
4.1装完jdk的环境变量
代码语言:javascript复制export JAVA_HOME=/usr/local/java/jdk1.8.0_211
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
4.2把zookeepert的环境变量也加进去
代码语言:javascript复制vim /etc/profile
代码语言:javascript复制export JAVA_HOME=/usr/local/java/jdk1.8.0_211
export ZOOKEEPER_HOME=/usr/local/apache-zookeeper-3.6.2-bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$ZOOKEEPER_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME
刷新环境变量生效
source /etc/profile
5.服务端命令启动
代码语言:javascript复制cd /usr/local/apache-zookeeper-3.6.2-bin/bin
./zkServer.sh start
6.客户端连接
代码语言:javascript复制./zkCli.sh
以连上,很ok
7.做成系统服务
7.1制作系统服务文件
代码语言:javascript复制vim /usr/lib/systemd/system/zookeeper.service
输入如下内容
[Unit]
Description=zookeeper
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/apache-zookeeper-3.6.2-bin/bin/zkServer.sh start
ExecReload=/usr/local/apache-zookeeper-3.6.2-bin/bin/zkServer.sh restart
ExecStop=/usr/local/apache-zookeeper-3.6.2-bin/zkServer.sh stop
[Install]
WantedBy=multi-user.target
刷新生效
systemctl daemon-reload
先停掉刚才用命令启动的zookeeper
[root@192-168-50-191 bin]# pwd
/usr/local/apache-zookeeper-3.6.2-bin/bin
[root@192-168-50-191 bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@192-168-50-191 bin]#
7.2用服务启动zookeeper
代码语言:javascript复制systemctl start zookeeper.service
systemctl status zookeeper.service
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl start zookeeper.service
Job for zookeeper.service failed because the control process exited with error code. See "systemctl status zookeeper.service" and "journalctl -xe" for details.
[root@192-168-50-191 bin]# systemctl status zookeeper.service
● zookeeper.service - zookeeper
Loaded: loaded (/usr/lib/systemd/system/zookeeper.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 二 2020-11-24 09:40:10 CST; 12s ago
Process: 42613 ExecStart=/usr/local/apache-zookeeper-3.6.2-bin/bin/zkServer.sh start (code=exited, status=1/FAILURE)
11月 24 09:40:10 192-168-50-191 systemd[1]: Starting zookeeper...
11月 24 09:40:10 192-168-50-191 zkServer.sh[42613]: Error: JAVA_HOME is not set and java could not be found in PATH.
11月 24 09:40:10 192-168-50-191 systemd[1]: zookeeper.service: control process exited, code=exited status=1
11月 24 09:40:10 192-168-50-191 systemd[1]: Failed to start zookeeper.
11月 24 09:40:10 192-168-50-191 systemd[1]: Unit zookeeper.service entered failed state.
11月 24 09:40:10 192-168-50-191 systemd[1]: zookeeper.service failed.
[root@192-168-50-191 bin]#
7.3遇到个小问题,提示我们Error: JAVA_HOME is not set and java could not be found in PATH
代码语言:javascript复制问题不大,只是在服务里没拿到环境变量
Error: JAVA_HOME is not set and java could not be found in PATH.
export
代码语言:javascript复制[root@192-168-50-191 bin]# export
declare -x CLASSPATH=".:/usr/local/java/jdk1.8.0_211/lib/dt.jar:/usr/local/java/jdk1.8.0_211/lib/tools.jar:/usr/local/apache-zookeeper-3.6.2-bin/lib"
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="192-168-50-191"
declare -x JAVA_HOME="/usr/local/java/jdk1.8.0_211"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
。。。。。
7.4拿到我们之前配置的java环境变量declare -x JAVA_HOME="/usr/local/java/jdk1.8.0_211"
,在if $# -gt 1 下面添加JAVA_HOME
代码语言:javascript复制export JAVA_HOME=/usr/local/java/jdk1.8.0_211
代码语言:javascript复制vim zkEnv.sh
代码语言:javascript复制[root@192-168-50-191 bin]# pwd
/usr/local/apache-zookeeper-3.6.2-bin/bin
[root@192-168-50-191 bin]# vim zkEnv.sh
[root@192-168-50-191 bin]#
7.5再次以服务启动,很ok
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl start zookeeper
[root@192-168-50-191 bin]# systemctl status zookeeper
7.6允许开机自启
代码语言:javascript复制systemctl enable zookeeper
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl enable zookeeper
Created symlink from /etc/systemd/system/multi-user.target.wants/zookeeper.service to /usr/lib/systemd/system/zookeeper.service.
[root@192-168-50-191 bin]#
7.7查看启动状态
代码语言:javascript复制systemctl status zookeeper
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl status zookeeper
● zookeeper.service - zookeeper
Loaded: loaded (/usr/lib/systemd/system/zookeeper.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2020-11-24 09:52:07 CST; 4min 43s ago
Main PID: 72469 (java)
CGroup: /system.slice/zookeeper.service
└─72469 /usr/local/java/jdk1.8.0_211/bin/java -Dzookeeper.log.dir=/usr/local/apache-zookeeper-3.6.2-bin/bin/../logs -Dzookee...
11月 24 09:52:06 192-168-50-191 systemd[1]: Starting zookeeper...
11月 24 09:52:06 192-168-50-191 zkServer.sh[72453]: ZooKeeper JMX enabled by default
11月 24 09:52:06 192-168-50-191 zkServer.sh[72453]: Using config: /usr/local/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
11月 24 09:52:07 192-168-50-191 zkServer.sh[72453]: Starting zookeeper ... STARTED
11月 24 09:52:07 192-168-50-191 systemd[1]: Started zookeeper.
[root@192-168-50-191 bin]#
7.8查看是否开机启动
代码语言:javascript复制systemctl is-enabled zookeeper
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl is-enabled zookeeper
enabled
7.9取消开机自启动
代码语言:javascript复制systemctl disable zookeeper.service
代码语言:javascript复制[root@192-168-50-191 bin]# systemctl disable zookeeper.service
Removed symlink /etc/systemd/system/multi-user.target.wants/zookeeper.service.
[root@192-168-50-191 bin]# systemctl is-enabled zookeeper
disabled
[root@192-168-50-191 bin]#
收工,想了一下还是记录一下的好,虽然记录过程比较麻烦,但是有过程鸭