在Debian系统上安装和配置Zookeeper的步骤如下:
安装Zookeeper
- 下载Zookeeper:
- 访问Apache Zookeeper官网下载最新版本的Zookeeper。
- 例如,下载
zookeeper-3.4.14.tar.gz
。
- 解压文件:
tar -zxvf zookeeper-3.4.14.tar.gz -C /usr/local/soft/
- 配置环境变量(可选):
- 编辑
/etc/profile
文件,添加以下内容:
export ZOOKEEPER_HOME=/usr/local/soft/zookeeper-3.4.14 export PATH=$PATH:$ZOOKEEPER_HOME/bin
- 刷新配置:
source /etc/profile
- 启动Zookeeper:
- 进入
bin
目录,启动Zookeeper服务:
cd /usr/local/soft/zookeeper-3.4.14/bin/ ./zkServer.sh start
- 设置开机启动(可选):
- 将Zookeeper添加到开机启动服务:
# 创建服务文件 touch /etc/init.d/zookeeper chmod +x /etc/init.d/zookeeper # 编辑服务文件 vim /etc/init.d/zookeeper
- 添加以下内容:
#!/bin/bash # chkconfig: 234 20 90 # description: Zookeeper # processname: zookeeper case "$1" in start) su - zookeeper -c '/usr/local/soft/zookeeper-3.4.14/bin/zkServer.sh start' ;; stop) su - zookeeper -c '/usr/local/soft/zookeeper-3.4.14/bin/zkServer.sh stop' ;; status) su - zookeeper -c '/usr/local/soft/zookeeper-3.4.14/bin/zkServer.sh status' ;; restart) su - zookeeper -c '/usr/local/soft/zookeeper-3.4.14/bin/zkServer.sh restart' ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac exit 0
- 将Zookeeper服务添加到开机启动项:
chkconfig --add zookeeper
- 查看开机自启项:
chkconfig --list | grep zookeeper
- 重启系统测试服务:
reboot
- 测试服务状态:
service zookeeper status
配置Zookeeper
- 配置文件:
- 进入
conf
目录,复制zoo_sample.cfg
为zoo.cfg
:
cd /usr/local/soft/zookeeper-3.4.14/conf/ cp zoo_sample.cfg zoo.cfg
- 修改配置文件:
- 使用
vim
编辑zoo.cfg
文件,添加或修改以下内容:
# The port at which the clients will connect to the server clientPort=2181 # The directory where the data is stored. dataDir=/usr/local/soft/zookeeper-3.4.14/data # The directory where the transaction log is stored. dataLogDir=/usr/local/soft/zookeeper-3.4.14/logs # The tick time in milliseconds tickTime=2000 # The number of ticks that the initial synchronization phase can take initLimit=5 # The maximum number of ticks that can pass between when a follower starts # and when it is ready to serve requests syncLimit=2 # The ID of this server server.1:2888:3888 server.2:2888:3888 server.3:2888:3888
- 创建并配置
myid
文件:
- 在
dataDir
目录下创建myid
文件,并写入服务器ID:
cd /usr/local/soft/zookeeper-3.4.14/data echo "1" > myid
- 启动Zookeeper集群:
- 分别在每台服务器上执行以下命令启动Zookeeper:
cd /usr/local/soft/zookeeper-3.4.14/bin/ ./zkServer.sh start
- 检查Zookeeper状态:
./zkServer.sh status
以上步骤展示了如何在Debian系统上安装和配置Zookeeper。请根据实际需求调整配置参数,并确保所有服务器的配置一致,以实现集群功能。