前置准备
三台主机名分别为master,slave1,slave2的虚拟机 添加主机名与IP地址的映射(配置hosts文件) 安装jdk 下载好zookeeper压缩包 创建zookeeper目录
mkdir -p /usr/zookeeper && cp zookeeper-3.4.10.tar.gz /usr/zookeeper && cd /usr/zookeeper
解压
tar -zxvf zookeeper-3.4.10.tar.gz -C /usr/zookeeper
删除压缩包
rm -rf /usr/zookeeper/zookeeper-3.4.10.tar.gz
配置修改
创建数据和日志存储文件夹
cd /usr/zookeeper/zookeeper-3.4.10
mkdir zkdata
mkdir zkdatalog
将文件夹传给slave1和slave2
scp -r /usr/zookeeper root@slave1:/usr/
scp -r /usr/zookeeper root@slave2:/usr/
创建myid标识主机服务器序号(3台不一样)
cd /usr/zookeeper/zookeeper-3.4.10/zkdata
vim myid
1
vim myid
2
vim myid
3
zoo.cfg文件
cd /usr/zookeeper/zookeeper-3.4.10/conf/
将该目录下面的zoo_sample.cfg复制为zoo.cfg,并修改
mv zoo_sample.cfg zoo.cfg
vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/zookeeper/zookeeper-3.4.10/zkdata
clientPort=2181
dataLogDir=/usr/zookeeper/zookeeper-3.4.10/zkdatalog
server.1=master:2888:3888
server.2=slave1:2888:3888
server.3=slave2:2888:3888
配置参数解析
tickTime为通信心跳数,Zookeeper服务器与客户端维持心跳的间隔,单位毫秒 initLimit 为Leader与Follower之间初始连接时能容忍的最多心跳数 syncLimit为Leader与Follower之间的最大响应时间单位,假如响应超过syncLimit * tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer dataDir为数据目录路径 clientPort为监听客户端连接的端口 dataLogDir为日志存储路径 以server.1=master:2888:3888为例,1代表一号服务器,master代表ip地址,2888是这个服务器与集群中的Leader服务器交换信息的端口,3888是当leader挂了之后,follower中重新选举leader所用的进行通信的端口
环境变量
vi /etc/profile
export ZOOKEEPER_HOME=/usr/zookeeper/zookeeper-3.4.10
PATH=$PATH:$ZOOKEEPER_HOME/bin
脚本(master上执行)
mkdir -p /usr/zookeeper && cp zookeeper-3.4.10.tar.gz /usr/zookeeper
cd /usr/zookeeper
tar zxvf zookeeper-3.4.10.tar.gz -C /usr/zookeeper
rm -rf /usr/zookeeper/zookeeper-3.4.10.tar.gz
cd /usr/zookeeper/zookeeper-3.4.10/conf/
mv zoo_sample.cfg zoo.cfg
echo "tickTime=2000" > zoo.cfg
echo "initLimit=10" >> zoo.cfg
echo "syncLimit=5" >> zoo.cfg
echo "dataDir=/usr/zookeeper/zookeeper-3.4.10/zkdata" >> zoo.cfg
echo "clientPort=2181" >> zoo.cfg
echo "dataLogDir=/usr/zookeeper/zookeeper-3.4.10/zkdatalog" >> zoo.cfg
echo "server.1=master:2888:3888" >> zoo.cfg
echo "server.2=slave1:2888:3888" >> zoo.cfg
echo "server.3=slave2:2888:3888" >> zoo.cfg
cd /usr/zookeeper/zookeeper-3.4.10 && mkdir zkdata zkdatalog
cd zkdata
echo '2' > myid
scp -prq /usr/zookeepe? root@slave1:/usr
echo '3' > myid
scp -prq /usr/zookeepe? root@slave2:/usr
echo '1' > myid
echo 'export ZOOKEEPER_HOME=/usr/zookeeper/zookeeper-3.4.10' >> /etc/profile
echo 'export PATH=$PATH:$ZOOKEEPER_HOME/bin' >> /etc/profile
source /etc/profile
scp /etc/profile root@slave1:/etc/profile
scp /etc/profile root@slave2:/etc/profile
zkServer.sh start
ssh slave1 "source /etc/profile;zkServer.sh start"
ssh slave2 "source /etc/profile;zkServer.sh start"
zkServer.sh status
|