集群方案
主机IP 消息端口:通信端口 节点目录/usr/local/下
192.168.1.1 2181 2888:3888 zookeeper
192.168.1.2 2181 2888:3888 zookeeper
192.168.1.3 2181 2888:3888 zookeeper
安装JDK
# 安装JDK
# 配置/etc/profile
vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA=$JAVA_HOME/bin/java
source /etc/profile
以下操作3台机器同时操作
准备3台虚拟机
IP地址为
192.168.1.1
192.168.1.2
192.168.1.3
新建用户
useradd zookeeper
passwd zookeeper
安装依赖
yum install wget
下载ZooKeeper源码
wget http://apache.fayea.com/zookeeper/stable/zookeeper-3.4.12.tar.gz
tar -zxvf zookeeper-3.4.12.tar.gz
mv zookeeper-3.4.12 /usr/local/zookeeper
cd /usr/local/zookeeper/
mkdir data
mkdir log
chown -R zookeeper.zookeeper data
chown -R zookeeper.zookeeper logs
修改配置文件
# 将/conf目录下的zoo_sample.cfg文件拷贝一份, 命名为为zoo.cfg
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs
# 2888 端口号是 zookeeper 服务之间通信的端口。
# 3888 是 zookeeper 与其他应用程序通信的端口。
server.0=192.168.1.1:2888:3888
server.1=192.168.1.2:2888:3888
server.2=192.168.1.3:2888:3888
新建myid文件
cd /usr/local/zookeeper/data
* 192.168.1.1机器: echo 0 >> myid
* 192.168.1.2机器: echo 1 >> myid
* 192.168.1.3机器: echo 2 >> myid
# 这里的0,1,2对应zoo.cfg中server.x处的x
编辑.bash_profile
su - zookeeper
vim .bash_profile
export ZOOKEEPER_HOME=/usr/local/zookeeper
# zookeeper env
export PATH=$ZOOKEEPER_HOME/bin:$PATH
source .bash_profile
zookeeper操作
cd /usr/local/zookeeper/bin
vim zkServer.sh
#在129行增加如下内容,因为脚本里并未读取zoo.cfg中设定的log目录
ZOO_LOG_DIR=/usr/local/zookeeper/logs
# 启动zk
zkServer.sh start
# 查看状态
zkServer.sh status
# 停止zk
zkServer.sh stop
# 查看日志
tail -f /usr/local/zookeeper/logs/zookeeper.out
设置ZooKeeper开机启动
vim /etc/rc.local
su - zookeeper -c '/usr/local/zookeeper/bin/zkServer.sh start'
防火墙过滤
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent
firewall-cmd --reload