先准备好 java 环境
为 zookeeper 创建一个单独的用户。sudo useradd zookeeper -m
-m 参数是用于在创建用户的同时创建 Home 目录。 上面的脚本会创建目录 /home/zookeeper
指定 zookeeper 用户的默认 shell 为 bash。sudo usermod --shell /bin/bash zookeeper
设置用户密码 sudo passwd zookeeper
,密码和账户名字一样即可
把用户添加到sudo组 sudo usermod -aG sudo zookeeper
检查下用户是否添加成功 getent group sudo
创建zookeeper的数据目录。sudo mkdir -p /data/zookeeper
设置zookeeper用户为此目录的所有者。sudo chown -R zookeeper:zookeeper /data/zookeeper
去官网复制最新版本的链接,如:https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
进入目录/opt 并执行 sudo wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz 命令下载文件
解压文件。sudo tar -xvf apache-zookeeper-3.7.0-bin.tar.gz
重命名文件 sudo mv apache-zookeeper-3.7.0-bin zookeeper
指定zookeeper用户为此目录的所有者。sudo chown -R zookeeper:zookeeper /opt/zookeeper
cd /opt/zookeeper/conf
sudo cp zoo_sample.cfg zoo.cfg
sudo nano zoo.cfg
,修改数据目录地址配置项为: /data/zookeeper,其他项目保持不变,保存并退出。
切换到zookeeper用户。su zookeeper
执行命令 /opt/zookeeper/bin/zkServer.sh start
启动服务。如果显示 STARTED 说明已经启动成功
连接本地zookeeper服务。/opt/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181
如果显示 CONECTED 则说明连接成功。
输入命令 quit
退出本地连接
输入命令停止zookeeper服务。/opt/zookeeper/bin/zkServer.sh stop
创建启动配置文件。执行命令 sudo nano /etc/systemd/system/zookeeper.service
并复制以下内容到文件中,保存并退出
[Unit] Description=Zookeeper Daemon Documentation=http://zookeeper.apache.org Requires=network.target After=network.target [Service] Type=forking WorkingDirectory=/opt/zookeeper User=zookeeper Group=zookeeper ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg ExecStop=/opt/zookeeper/bin/zkServer.sh stop /opt/zookeeper/conf/zoo.cfg ExecReload=/opt/zookeeper/bin/zkServer.sh restart /opt/zookeeper/conf/zoo.cfg TimeoutSec=30 Restart=on-failure [Install] WantedBy=default.target
sudo systemctl daemon-reload
sudo systemctl start zookeeper
sudo``systemctl enable zookeeper
sudo systemctl status zookeeper