systemd(CentOS7)启动zookeeper
1.准备zookeeper用户
groupadd zookeeper
useradd -M -s /bin/false -g zookeeper zookeeper
2.准备工作目录
mkdir -p /zookeeper/{data,conf,logs,src}
wget -P /zookeeper/src https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
tar zxf /zookeeper/src/zookeeper-3.4.13.tar.gz
chown zookeeper.zookeeper -R /zookeeper
chmod 750 -R /zookeeper
ln -s /zookeeper/src/zookeeper-3.4.13 /opt/zookeeper
3.创建zookeeper配置文件:/zookeeper/conf/zoo.cfg
- Standalone模式:
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2181
dataDir=/zookeeper/data
dataLogDir=/zookeeper/data/logs
- Clustered模式:
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2181
dataDir=/zookeeper/data
dataLogDir=/zookeeper/data/logs
server.1=zk1:2888:3888
server.2=zk2:2888:3888
server.3=zk3:2888:3888
Clustered模式下需要创建myid文件,节点对应id:echo "1" > /zookeeper/data/myid
4.编写systemd.service
服务脚本:/etc/systemd/system/zookeeper.service
[Unit]
Description=ZooKeeper Service
After=network.target
After=syslog.target
[Service]
Environment=ZOO_LOG_DIR=/zookeeper/logs
SyslogIdentifier=zookeeper
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start /zookeeper/conf/zoo.cfg
ExecStop=/opt/zookeeper/bin/zkServer.sh stop /zookeeper/conf/zoo.cfg
ExecReload=/opt/zookeeper/bin/zkServer.sh restart /zookeeper/conf/zoo.cfg
[Install]
WantedBy=default.target
重新加载配置信息:systemctl daemon-reload
启动zookeeper:systemctl start zookeeper.service
关掉zookeeper:systemctl stop zookeeper.service
查看进程状态及日志:systemctl status zookeeper.service
开机自启动:systemctl enable zookeeper.service
关闭自启动:systemctl disable zookeeper.service