在CentOS上搭建Zookeeper集群需要以下步骤:
环境准备
- 安装Java环境:
sudo yum install java-1.8.0-openjdk-devel
- 下载Zookeeper:
从Apache Zookeeper官方网站下载适合CentOS的版本,例如Zookeeper 3.8.x版本。
wget https://downloads.apache.org/zookeeper/zookeeper-3.8.x/zookeeper-3.8.x.tar.gz
- 解压Zookeeper:
tar -zxvf zookeeper-3.8.x.tar.gz cd zookeeper-3.8.x
配置Zookeeper
- 创建配置文件:
复制
conf/zoo_sample.cfg
为conf/zoo.cfg
并编辑。cp conf/zoo_sample.cfg conf/zoo.cfg vi conf/zoo.cfg
- 配置参数:
tickTime
:基本时间单位(毫秒),例如2000。dataDir
:数据目录,例如/var/lib/zookeeper
。clientPort
:客户端连接端口,例如2181。initLimit
:初始化连接时最长能忍受的心跳时间间隔数,例如10。syncLimit
:Leader与Follower之间发送消息、请求和应答时间长度,例如5。server.x
:为每个服务器分配一个唯一ID和地址,例如:server.1 192.168.1.1:2888:3888 server.2 192.168.1.2:2888:3888 server.3 192.168.1.3:2888:3888
启动Zookeeper
- 启动Zookeeper服务:
在每台服务器上启动Zookeeper服务,指定服务器ID作为命令参数。
./bin/zkServer.sh start server.1 ./bin/zkServer.sh start server.2 ./bin/zkServer.sh start server.3
- 验证集群状态:
使用以下命令查看集群状态。
./bin/zkServer.sh status
设置开机启动
- 创建启动脚本:
创建
/etc/rc.d/init.d/zookeeper
文件并添加以下内容:#!/bin/bash # # /etc/rc.d/init.d/zookeeper # # Startup script for the zookeeper. # # chkconfig: 234 20 80 # description: Zookeeper is a distributed coordination service # for distributed application coordination. # # Source function library. . /etc/rc.d/init.d/functions ZOO_HOME="/usr/local/zookeeper-3.8.x" ZOO_LOG_DIR="/var/log/zookeeper" ZOO_PID_FILE="/var/lib/zookeeper/zookeeper.pid" ZOO_CONFIG_FILE="/usr/local/zookeeper-3.8.x/conf/zoo.cfg" case "$1" in start) echo -n "Starting zookeeper..." $ZOO_HOME/bin/zkServer.sh start server.1 echo ;; stop) echo -n "Shutting down zookeeper..." $ZOO_HOME/bin/zkServer.sh stop server.1 echo ;; restart) $0 stop $0 start ;; status) status -p $ZOO_PID_FILE $ZOO_HOME ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit 0
- 添加可执行权限:
chmod +x /etc/rc.d/init.d/zookeeper
- 注册为系统服务:
chkconfig --add zookeeper
- 设置开机自启动:
chkconfig zookeeper on
注意事项
- 时间同步:确保所有服务器的时间同步,可以使用NTP服务。
- 防火墙配置:配置防火墙以允许Zookeeper使用的端口(默认是2181)通信。
- myid文件:确保所有服务器的
myid
文件配置正确,并且zoo.cfg
文件中的server
配置正确反映了集群成员。
通过以上步骤,你应该能够在CentOS系统上成功搭建并运行Zookeeper集群。