![在 Ubuntu 16.04 上安装 ZooKeeper 的正确方法是什么,无论是独立部署还是多节点部署?](https://linux22.com/image/1006758/%E5%9C%A8%20Ubuntu%2016.04%20%E4%B8%8A%E5%AE%89%E8%A3%85%20ZooKeeper%20%E7%9A%84%E6%AD%A3%E7%A1%AE%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%8C%E6%97%A0%E8%AE%BA%E6%98%AF%E7%8B%AC%E7%AB%8B%E9%83%A8%E7%BD%B2%E8%BF%98%E6%98%AF%E5%A4%9A%E8%8A%82%E7%82%B9%E9%83%A8%E7%BD%B2%EF%BC%9F.png)
首先,请把我当成一个完全的 Linux 新手。我在这里展示的所有作品都是从互联网上的各种来源拼凑起来的。
我正在尝试为开发机器(在 VirtualBox 上运行 Ubuntu 客户操作系统)设置 ZooKeeper 服务,并附带安装步骤,该服务也可以轻松用于生产场景(即只需修改配置文件)。这些是我采取的步骤,问题在底部。
安装 Java:
sudo apt-get update
sudo apt-get install default-jre
创建 zookeeper 系统用户:
sudo adduser --system --no-create-home --disabled-password --disabled-login zookeeper
使用 wget 下载二进制版本:
wget "http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz"
创建适当的目录:
sudo mkdir /opt/zookeeper
sudo mkdir /var/lib/zookeeper
sudo mkdir /var/lib/zookeeper/logs
sudo mkdir /var/run/zookeeper
提取 tar 档案:
sudo tar -xvzf zookeeper-3.4.10.tar.gz --directory /opt/zookeeper --strip-components 1
编辑配置:
sudo cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
sudo nano /opt/zookeeper/conf/zoo.cfg
将 dataDir 配置选项更改为 dataDir=/var/lib/zookeeper
编辑环境脚本:
sudo nano /opt/zookeeper/bin/zkEnv.sh
将此行添加到文件顶部:ZOO_LOG_DIR="/var/lib/zookeeper/logs"
确保权限:
sudo chown -R zookeeper:nogroup /opt/zookeeper
sudo chown -R zookeeper:nogroup /var/lib/zookeeper
sudo chown -R zookeeper:nogroup /var/run/zookeeper
创建 systemd 服务文件:
sudo nano /etc/systemd/system/zookeeper.service
文件内容:
[Unit]
Description=Apache ZooKeeper
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
PIDFile=/var/run/zookeeper/zookeeper.pid
User=zookeeper
Group=nogroup
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
Restart=on-failure
SyslogIdentifier=zookeeper
[Install]
WantedBy=multi-user.target
问题:当我使用“sudo systemctl start zookeeper”启动服务时,服务器启动后立即关闭。上面的设置有什么问题吗?我可以做哪些不同的事情来改进这些步骤(即从安全角度来看)?
我尝试使用“sudo apt-get install zookeeperd”并发现了几个问题,一是目录位置令人困惑,不符合我的口味,二是我无法从另一台机器连接到服务器(连接超时)即使服务器在本地主机和远程机器上对“ruok”telnet 测试响应“imok”(如果我手动调用“./zkServer.sh start”,服务器运行良好)。
编辑:这是我尝试运行服务时的控制台输出:
$ sudo systemctl start zookeeper
$ sudo systemctl status zookeeper
● zookeeper.service - Apache Zookeeper
Loaded: loaded (/etc/systemd/system/zookeeper.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Apr 11 06:47:42 Ubuntu64 systemd[1]: Started Apache Zookeeper.
Apr 11 06:47:42 Ubuntu64 zookeeper[1871]: ZooKeeper JMX enabled by default
Apr 11 06:47:42 Ubuntu64 zookeeper[1871]: Using config: /opt/zookeeper/conf/zoo.cfg
Apr 11 06:47:43 Ubuntu64 zookeeper[1871]: Starting zookeeper ... STARTED
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: ZooKeeper JMX enabled by default
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: Using config: /opt/zookeeper/conf/zoo.cfg
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: Stopping zookeeper ... STOPPED
$ sudo journalctl -u zookeeper.service
-- Logs begin at Rab 2018-04-11 06:45:32 WIB, end at Rab 2018-04-11 06:55:55 WIB. --
Apr 11 06:47:42 Ubuntu64 systemd[1]: Started Apache Zookeeper.
Apr 11 06:47:42 Ubuntu64 zookeeper[1871]: ZooKeeper JMX enabled by default
Apr 11 06:47:42 Ubuntu64 zookeeper[1871]: Using config: /opt/zookeeper/conf/zoo.cfg
Apr 11 06:47:43 Ubuntu64 zookeeper[1871]: Starting zookeeper ... STARTED
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: ZooKeeper JMX enabled by default
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: Using config: /opt/zookeeper/conf/zoo.cfg
Apr 11 06:47:43 Ubuntu64 zookeeper[1891]: Stopping zookeeper ... STOPPED
$
答案1
start
从 到的这个最小改变start-foreground
可能就足够了:
ExecStart=/opt/zookeeper/bin/zkServer.sh start-foreground
答案2
检查 dataDir (zoo.cfg) 的权限,如果用户无法在该目录中写入,则该服务将无法启动