环境准备

zookeeper依赖于java,所以先安装java,可以参考: https://blog.51cto.com/mapengfei/5166517

安装zookeeper

我这里选择最新版是3.8.0, 在这个页面中找到最新的zookeeper版本https://zookeeper.apache.org/releases.html#download

root@kafka1:~# cd /opt
root@kafka1:/opt# wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz
root@kafka1:/opt# tar zxvf apache-zookeeper-3.8.0-bin.tar.gz
root@kafka1:/opt# cd apache-zookeeper-3.8.0-bin/conf/
root@kafka1:/opt/apache-zookeeper-3.8.0-bin/conf# cp zoo_sample.cfg zoo.cfg
root@kafka1:/opt/apache-zookeeper-3.8.0-bin/conf# cd ../bin

## 手动启动方法(如果看效果就先启动,加到系统服务中)
root@kafka1:/opt/apache-zookeeper-3.8.0-bin/bin# bash zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

加成系统服务

root@kafka1:~# cat /lib/systemd/system/zookeeper.services 
[Unit]
Description=zookeeper

[Service]
ExecStart=/opt/apache-zookeeper-3.8.0-bin/bin/zkServer.sh start
ExecStop=/opt/apache-zookeeper-3.8.0-bin/bin/zkServer.sh stop
SyslogIdentifier=zookeeper
RemainAfterExit=true
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

开启并设为开机自启

root@kafka1:~# systemctl start zookeeper
root@kafka1:~# systemctl enable zookeeper

root@kafka1:~# netstat -atnlp | grep 2181
tcp6       0      0 :::2181                 :::*                    LISTEN      66689/java

如果在启动的时候报 JAVA_HOME is not set java could not be found in PATH

但是确认/etc/profile已经加过配置JAVA_HOME了,是因为:Ubuntu的默认shell为dash,把dash改成bash就可以了。
解决方法:把sh zkServer.sh start换成bash zkServer.sh start

root@kafka1:/opt/apache-zookeeper-3.8.0-bin# sh zkServer.sh start
zkServer.sh: 78: /opt/apache-zookeeper-3.8.0-bin/bin/zkEnv.sh: [[: not found
-p: not found
java is /usr/bin/java
Error: JAVA_HOME is not set and java could not be found in PATH.