在Linux系统下部署Apache ZooKeeper的步骤如下:
更新系统:确保系统已更新。
sudo apt-get update
sudo apt-get install -y openjdk-11-jdk
sudo yum update
sudo yum install -y java-11-openjdk
下载并解压ZooKeeper:
wget命令下载:wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
tar -xvf apache-zookeeper-3.7.0-bin.tar.gz
cd apache-zookeeper-3.7.0-bin
创建数据目录:
sudo mkdir /opt/zookeeper/data
编辑配置文件:
conf目录,复制模板配置文件zoo_sample.cfg为zoo.cfg:cd /opt/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
zoo.cfg文件,修改以下参数:tickTime=2000
dataDir=/opt/zookeeper/data
clientPort=2181
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
server.1=192.168.1.101:2888:3888
server.2=192.168.1.102:2888:3888
server.3=192.168.1.103:2888:3888
server.x中的x需与每个节点的myid文件一致。创建myid文件:
mkdir -p /opt/zookeeper/data
echo 1 > /opt/zookeeper/data/myid
启动服务:
cd /opt/zookeeper/bin
./zkServer.sh start
验证启动状态:
netstat -an | grep 2181
./zkServer.sh status
创建systemd服务文件:
sudo nano /etc/systemd/system/zookeeper.service
[Unit]
Description=Apache ZooKeeper
After=network.target
[Service]
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable zookeeper
sudo systemctl start zookeeper
/opt/zookeeper/bin/zkCli.sh -server 192.168.1.101:2181