上传路径:/opt/software/
解压路径:/opt/module/
#数据目录:/data/zookeeper/data/
#3台机器创建存储目录:
sudo mkdir -p /data/zookeeper/data
#日志目录:/data/log/zookeeper
#3台机器创建日志目录:
sudo mkdir -p /data/log/zookeeper
sudo chown -R bigdata:bigdata /data/zookeeper/
解压一份 zookeeper 安装包,修改其配置文件 zoo.cfg,内容如下。之后将目录分发到其他服务器上
sudo chown -R bigdata:bigdata /opt/module/apache-zookeeper-3.6.4-bin/
cd /opt/module/apache-zookeeper-3.6.4-bin/conf
cp zoo_sample.cfg zoo.cfg
dataDir=/data/zookeeper/data/
dataLogDir=/data/log/zookeeper/
clientPort=2181
tickTime=5000
initLimit=10
syncLimit=10
maxClientCnxns=256
minSessionTimeout=86400000
maxSessionTimeout=86400000
autopurge.purgeInterval=24
autopurge.snapRetainCount=5
quorum.auth.enableSasl=false
quorum.cnxn.threads.size=20
server.2=node2:3181:4181
server.3=node3:3181:4181
server.4=node4:3181:4181
添加java环境变量
vim /opt/module/apache-zookeeper-3.6.4-bin/bin/zkEnv.sh
JAVA_HOME="/usr/java/jdk1.8.0_212"
分别在3个节点的 dataDir 目录下新建 myid 文件,并写入对应的节点标识。Zookeeper 集群通过 myid 文件识别集群节点,并通过上文配置的节点通信端口和选举端口来进行节点通信,选举出 Leader 节点。
创建并写入节点标识到 myid 文件:
node2服务器
echo "2" > /data/zookeeper/data/myid
node3服务器
echo "3" > /data/zookeeper/data/myid
node4服务器
echo "4" > /data/zookeeper/data/myid
修改log4j.properties的zookeeper.log.dir=. 发现不生效
需修改bin/zkEnv.sh
#ZOO_LOG_DIR="$ZOOKEEPER_PREFIX/logs"
ZOO_LOG_DIR="/data/log/zookeeper"
分别在3台主机上,执行如下命令启动服务:
/opt/module/apache-zookeeper-3.6.4-bin/bin/zkServer.sh start
启动后使用zkServer.sh status查看集群各个节点状态,其中有一个节点是leader,有两个节点是follower,证明zookeeper集群是部署成功的
在node2添加文件 zookeeper.service
sudo vim /usr/lib/systemd/system/zookeeper.service
[Unit]
Description=Zookeeper
After=network.target remote-fs.target
[Service]
Type=forking
ExecStart=/opt/module/apache-zookeeper-3.6.4-bin/bin/zkServer.sh start
ExecStop=/opt/module/apache-zookeeper-3.6.4-bin/bin/zkServer.sh stop
ExecReload=/opt/module/apache-zookeeper-3.6.4-bin/bin/zkServer.sh restart
User=bigdata
Group=bigdata
Restart=always
SuccessExitStatus=0 143
[Install]
WantedBy=default.target
分发到其他节点
启动、关闭验证是否配置成功
sudo systemctl start zookeeper
sudo systemctl stop zookeeper
sudo systemctl status zookeeper
sudo systemctl enable zookeeper
sudo vim /opt/bash/zookeeper.sh
#!/bin/bash
# zookeeper节点列表
# 在这里修改为自己的实际节点IP地址
nodes=(node2 node3 node4)
# 循环遍历所有节点执行相同的命令
cmd=$1
if [ "$cmd" != "" ];then
if [[ "start" == "$cmd" ]] || [[ "stop" == "$cmd" ]] || [[ "restart" == "$cmd" ]] || [[ "status" == "$cmd" ]] ;then
for host in ${nodes[@]} ; do
echo "=================== $host =================="
ssh $host "sudo systemctl $cmd zookeeper"
echo "$cmd Zookeeper on $host success..."
done
else
echo " 输入的参数不对"
echo " start 启动zookeeper集群"
echo " stop 停止zookeeper集群"
echo " restart 重启zookeeper集群"
echo " status 查看zookeeper集群"
fi
else
echo "请传入一个参数(start|stop|restart|status)"
fi
添加执行权限
chmod +x /opt/bash/zookeeper.sh
脚本使用方式
/opt/bash/zookeeper.sh start/stop/status/restart